By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Arduino to control LED using serial monitor

P: 7
My project is to control the LED by send '1' or '0' via serial monitor.
My task for this project is when '1' is send via serial monitor, the Led ON PIN 3 need to blink until '0' is send via serial monitor, the LED will be turn off. Then, when next '1' is send , the Led ON PIN 3 will blink again. But it doesn't work for my code. This is my first time to use arduino, can anyone tell me what is wrong in my code and help me to do some correction . Below is my code:

Expand|Select|Wrap|Line Numbers
  1. char data = 0;            //Variable for storing received data
  2. void setup()
  3. {
  4.     Serial.begin(115200);   //Sets the baud for serial data transmission                               
  5.     pinMode(3, OUTPUT);  //Sets digital pin 13 as output pin
  6. }
  7. void loop()
  8. {
  9.    if(Serial.available()>0 )      // Send data only when you receive data:
  10.    {
  11.       data = Serial.read();        //Read the incoming data & store into data
  12.     Serial.print(data);          //Print Value inside data in Serial monitor
  13.      Serial.print("\n");        
  14.  
  15.  
  16.             while(data == '1')
  17.             {
  18.                 digitalWrite(3, HIGH);   //If value is 1 then LED turns ON
  19.                 delay(2000);
  20.                digitalWrite(3, LOW);
  21.                delay(2000);
  22.  
  23.  
  24.  
  25.             }    
  26.  
  27.       while(data == '0')         //  Checks whether value of data is equal to 0
  28.       {
  29.          digitalWrite(3, LOW);    //If value is 0 then LED turns OFF
  30.  
  31.       }
  32.  
  33.  
  34. }
  35. }
  36.  
Feb 2 '18 #1

✓ answered by donbock

You want to check for new serial inputs while blinking the led in order to respond quickly to a 0 input. I suggest one state variable to identify the logical state of the led (blinking or off) and another state variable to control the physical state of the led (on or off).

Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Do you mean a binary 1 or 0?

If so, remember '1' is an ASCII 1 which is 49 decimal.
Feb 2 '18 #2

P: 7
No, the '1' or '0' is just simple char that send via serial monitor. The char can be anything set by the programmer .

If '1' is send, the LED need to blink until '0' is send, the LED will stop to blink.

But my code doesn't work, can u help me to point out my mistakes or do corrections on my code?
Feb 2 '18 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
There are no character variables in C or C++. The char data type is an integer large enough to hold one character of the implementation dataset. In the case of ASCII (our case) the char is an 8-bit integer which is large enough to hold any ASCII character.

The '1' is an ASCII 1 which is 00110001 in binary. A 1 is 00000001.

I assume you have this correctly set up between you and your device.

The other thing is you have two loops where I think you need only one:

Expand|Select|Wrap|Line Numbers
  1. if(Serial.available()>0 )
  2. {
  3.    data = Serial.read(); 
  4.    if ( data == '1')
  5.    {
  6.  
  7.    }
  8.    else
  9.    {
  10.  
  11.    }
  12.  
  13. }
  14.  
Lastly, never, ever, use global variables. Read this:
https://bytes.com/topic/c/insights/7...obal-variables

Does any of this help?
Feb 3 '18 #4

Expert 100+
P: 2,396
You want to check for new serial inputs while blinking the led in order to respond quickly to a 0 input. I suggest one state variable to identify the logical state of the led (blinking or off) and another state variable to control the physical state of the led (on or off).
Feb 4 '18 #5

Post your reply

Sign in to post your reply or Sign up for a free account.