element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Serial won't work!
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 14 replies
  • Subscribers 402 subscribers
  • Views 1187 views
  • Users 0 members are here
  • help
  • work
  • heating
  • code
  • please
  • serial
  • won't
  • arduino
Related

Serial won't work!

Former Member
Former Member over 11 years ago

I'v bin trying to making a heating auto heating system that turns the knob with a servo, I can change the var upload and it will change position but every time i try to set up commands thought serial I get nothing, not even printing the serial I just sent.  I'v tried this over and over again in different variations and am starting to get sick of just having my arduino uno just hanging from my wall doing nothing. please help.

 

 

my code:

 

#include <Servo.h>

Servo officeHeat;

int heat40 = 800;

int heat45 = 950;

int heat50 = 1150;

int heat55 = 1325;

int heat60 = 1500;

int heat65 = 1700;

int heat70 = 1825;

int heat75 = 1975;

int heat80 = 2150;

int officeHeatSetTo = heat60;

 

void setup(){

  Serial.begin(9600);

  officeHeat.attach(9);

}

 

 

void loop(){

//check if serial is available

  while(Serial.available() > 0){

    //read serial and set var

    int heatCommand = Serial.read();

    //if the command is A then set heat to 65

    if(heatCommand == 'A'){

      officeHeatSetTo = heat65;

    }

}

  officeHeat.writeMicroseconds(officeHeatSetTo);

//tell computer the current set temp.

  while(true){

    if(officeHeatSetTo == 800){

    Serial.println("40");

  }

    if(officeHeatSetTo == 950){

    Serial.println("45");

  }

    if(officeHeatSetTo == 1150){

    Serial.println("50");

  }

    if(officeHeatSetTo == 1325){

    Serial.println("55");

  }

    if(officeHeatSetTo == 1500){

    Serial.println("60");

  }

      if(officeHeatSetTo == 1700){

    Serial.println("65");

  }

      if(officeHeatSetTo == 1825){

    Serial.println("70");

  }

      if(officeHeatSetTo == 1975){

    Serial.println("75");

  }

      if(officeHeatSetTo == 2150){

    Serial.println("80");

  }

  delay(1000);

}

}

  • Sign in to reply
  • Cancel
  • neilk
    0 neilk over 11 years ago

    Hi Kyle

     

    You are reading a character from the serial input into an integer variable:

     

        //read serial and set var

        int heatCommand = Serial.read();

     

    with the above code, heatCommand will be equal to the Ascii value of 'A' - ie 65 (if you type an 'A', of course).

     

    If you modify your code to use a character variable, then heatCommand will be equal to character 'A', when you type 'A'

     

        //read serial and set var

        char heatCommand = Serial.read();

     

    Good luck

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to neilk

    changed the

        //read serial and set var

        int heatCommand = Serial.read();

    to

        //read serial and set var

        char heatCommand = Serial.read();

    and it still doesn't work

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neilk
    0 neilk over 11 years ago in reply to Former Member

    Can you be more precise and describe exactly what does happen? "It still doesn't work" doesn't tell us very much.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to neilk

    I upload the program to my arduino open serial.  I see the 60 being printed(showing the current set temp.) but, when I send the A nothing changes(servo doesn't move or make a noise of trying and the Serial.println doesnt work)

     

    #include <Servo.h>

    Servo officeHeat;

    int heat40 = 800;

    int heat45 = 950;

    int heat50 = 1150;

    int heat55 = 1325;

    int heat60 = 1500;

    int heat65 = 1700;

    int heat70 = 1825;

    int heat75 = 1975;

    int heat80 = 2150;

    int officeHeatSetTo = heat60;

     

    void setup(){

      Serial.begin(9600);

      officeHeat.attach(9);

    }

     

     

    void loop(){

    //check if serial is available

      while(Serial.available() > 0){

        //read serial and set var

        char heatCommand = Serial.read();

        //if the command is A then set heat to 65

        if(heatCommand == 'A'){

          officeHeatSetTo = heat65;

          Serial.println("Heat set to 65");

        }

    }

      officeHeat.writeMicroseconds(officeHeatSetTo);

    //tell computer the current set temp.

      while(true){

        if(officeHeatSetTo == 800){

        Serial.println("40");

      }

        if(officeHeatSetTo == 950){

        Serial.println("45");

      }

        if(officeHeatSetTo == 1150){

        Serial.println("50");

      }

        if(officeHeatSetTo == 1325){

        Serial.println("55");

      }

        if(officeHeatSetTo == 1500){

        Serial.println("60");

      }

          if(officeHeatSetTo == 1700){

        Serial.println("65");

      }

          if(officeHeatSetTo == 1825){

        Serial.println("70");

      }

          if(officeHeatSetTo == 1975){

        Serial.println("75");

      }

          if(officeHeatSetTo == 2150){

        Serial.println("80");

      }

      delay(1000);

    }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neilk
    0 neilk over 11 years ago in reply to Former Member

    Sorry Kyle - I only looked as far as the first, very obvious error

     

    The structure:

     

      while(true){

        if(officeHeatSetTo == 800){

     

    has the effect of locking the sketch into the while loop, so the serial read is only executed once - the first time through.

    Remove  while(true){     and its corresponding  closing brace - } and the program  flow will be correct.

    It works for me, although I don'y have a servo connected.

     

    Hope this helps.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to neilk

    well I i changed it to while true...I got the "heat set to 65" but, it wasn't printing the the the 60 or 65 like it should be and the servo didn't move.

    #include <Servo.h>

    Servo officeHeat;

    int heat40 = 800;

    int heat45 = 950;

    int heat50 = 1150;

    int heat55 = 1325;

    int heat60 = 1500;

    int heat65 = 1700;

    int heat70 = 1825;

    int heat75 = 1975;

    int heat80 = 2150;

    int officeHeatSetTo = heat60;

     

    void setup(){

      Serial.begin(9600);

      officeHeat.attach(9);

    }

     

     

    void loop(){

    //check if serial is available

      while(true){

        //read serial and set var

        char heatCommand = Serial.read();

        //if the command is A then set heat to 65

        if(heatCommand == 'A'){

          officeHeatSetTo = heat65;

          Serial.println("Heat set to 65");

        }

    }

      officeHeat.writeMicroseconds(officeHeatSetTo);

    //tell computer the current set temp.

      while(true){

        if(officeHeatSetTo == 800){

        Serial.println("40");

      }

        if(officeHeatSetTo == 950){

        Serial.println("45");

      }

        if(officeHeatSetTo == 1150){

        Serial.println("50");

      }

        if(officeHeatSetTo == 1325){

        Serial.println("55");

      }

        if(officeHeatSetTo == 1500){

        Serial.println("60");

      }

          if(officeHeatSetTo == 1700){

        Serial.println("65");

      }

          if(officeHeatSetTo == 1825){

        Serial.println("70");

      }

          if(officeHeatSetTo == 1975){

        Serial.println("75");

      }

          if(officeHeatSetTo == 2150){

        Serial.println("80");

      }

      delay(1000);

    }

    }

     

     

    Then slashed out while true and it went back to Serial.println the for 60 started working again but, now it doesn't give me the "heat set to 60" or move the servo

     

    #include <Servo.h>

    Servo officeHeat;

    int heat40 = 800;

    int heat45 = 950;

    int heat50 = 1150;

    int heat55 = 1325;

    int heat60 = 1500;

    int heat65 = 1700;

    int heat70 = 1825;

    int heat75 = 1975;

    int heat80 = 2150;

    int officeHeatSetTo = heat60;

     

    void setup(){

      Serial.begin(9600);

      officeHeat.attach(9);

    }

     

     

    void loop(){

    //check if serial is available

    //  while(true){

        //read serial and set var

        char heatCommand = Serial.read();

        //if the command is A then set heat to 65

        if(heatCommand == 'A'){

          officeHeatSetTo = heat65;

          Serial.println("Heat set to 65");

    //    }

    }

      officeHeat.writeMicroseconds(officeHeatSetTo);

    //tell computer the current set temp.

      while(true){

        if(officeHeatSetTo == 800){

        Serial.println("40");

      }

        if(officeHeatSetTo == 950){

        Serial.println("45");

      }

        if(officeHeatSetTo == 1150){

        Serial.println("50");

      }

        if(officeHeatSetTo == 1325){

        Serial.println("55");

      }

        if(officeHeatSetTo == 1500){

        Serial.println("60");

      }

          if(officeHeatSetTo == 1700){

        Serial.println("65");

      }

          if(officeHeatSetTo == 1825){

        Serial.println("70");

      }

          if(officeHeatSetTo == 1975){

        Serial.println("75");

      }

          if(officeHeatSetTo == 2150){

        Serial.println("80");

      }

      delay(1000);

    }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    what you need is something like this (Which works on my UNO)

     

    #define ServoEnabled 1 // change to 1 to enable servo output, 0 to disable
    #if ServoEnabled
    #include <Servo.h>
    #endif
    #define heat40  800
    #define heat45  950
    #define heat50  1150
    #define heat55  1325
    #define heat60  1500
    #define heat65  1700
    #define heat70  1825
    #define heat75  1975
    #define heat80  2150
    #define  printDelay 1000 // 1 second between prints
    int HeatSetting = 0;
    int newHeatSetting = 0;
    long printTimer = 0;
    #if ServoEnabled
    Servo officeHeat;
    #endif
    void setup() 
    {
       Serial.begin(9600);
       HeatSetting = heat60;
       newHeatSetting = HeatSetting;
       
    #if ServoEnabled
      officeHeat.attach(9,800,2150);
    #endif
       printTimer = millis() + printDelay;
    }
    void loop() { 
      // Any new commands ?
      check_serial();
      
    #if ServoEnabled
      check_Servo();
    #endif
      // Check if time to print heater setting
      if (millis() > printTimer) 
      {
        Serial.print("Temp Set to ");
        Serial.println(HeatSetting, DEC);
        printTimer = millis()+printDelay;
      }
      // Do other stuff
    }
    void check_serial()
    {
      char a=Serial.read();
      if ((a>='0')&& (a<='Z'))
        {
        switch (a)
          {
            case 'A' :newHeatSetting = heat40; break;
            case 'B' : newHeatSetting = heat45; break;
            case 'C' : newHeatSetting = heat50; break;
            case 'D' : newHeatSetting = heat55; break;
            case 'E' : newHeatSetting = heat60; break;
            case 'F' : newHeatSetting = heat65; break;
            case 'G' : newHeatSetting = heat70; break;
            case 'H' : newHeatSetting = heat75; break;
            case 'I' : newHeatSetting = heat80; break;
            case '0' : Serial.println("0 Received"); break;
            case '1' : Serial.println("1 Received"); break;
            case '2' : Serial.println("2 Received"); break;
            case '3' : Serial.println("3 Received"); break;
            case '4' : Serial.println("4 Received"); break;
            case '5' : Serial.println("5 Received"); break;
            case '6' : Serial.println("6 Received"); break;
            case '7' : Serial.println("7 Received"); break;
            case '8' : Serial.println("8 Received"); break;
            case '=' : Serial.println(HeatSetting, DEC); break;
            default : break;
          }
        }  
    }
    #if ServoEnabled
    void check_Servo()
    {
      if (newHeatSetting != HeatSetting)
      {
        officeHeat.writeMicroseconds(HeatSetting);
        HeatSetting = newHeatSetting;
      }
    }
    #endif

     

    this will only change the servo when the current value is different from the new value, it does not waste time in delays and has a nice structured case for managing commands

     

    regards

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • neilk
    0 neilk over 11 years ago in reply to Former Member

    Kyle

     

    The change you made after my last post wasn't the change I suggested  - you inserted another while(true){ in a different part of the sketch, instead of removing the existing while(true){.

     

    However, now that Peter has written the sketch for you, you need no further help from me.

     

    Good luck

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to neilk

    Ya... I kinda got carried away with it as it was bugging me so much. I think I will turn it into a Blog entry for future reference as there are many items I have covered off in the version i posted like using defines vs INTS etc.

     

    Should be useful as educational material

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    I did notice that the Servo library does odd things at the low end of the range though, asking for lower pulse width actually ended up increasing the pulse width not decreasing it, i even added the initialization to try to mitigate it but it had no effect. I was monitoring with an oscilloscope to observe the output as I did not have an actual servo to hand

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube