473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Arduino to control LED using serial monitor

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).

4 2049
weaknessforcats
9,208 Expert Mod 8TB
Do you mean a binary 1 or 0?

If so, remember '1' is an ASCII 1 which is 49 decimal.
Feb 2 '18 #2
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
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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

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

Similar topics

1
by: caldera | last post by:
Hi, I want to find the web user control in the datalist. I iterate the datalist using foreach statement in the DataListItem and in thi DataListItem I iterate all Controls objects but I can't find...
6
by: Sakharam Phapale | last post by:
Hi All, How to move the CommandButton control using mouse from one position to other? Is it possible using Drag, Drop events or I have to write my own code for that? I have set the...
1
by: zoneal | last post by:
I am trying to make an ActiveX control using VB.Net, my control has a Microsoft WebBrowser control. When i am trying to build the assembly i am getting an error message "Unable to emit the...
0
by: pandi | last post by:
Hi, I am using a check scanner.It has functions which gives output like image ,checknumber. i am using serial port to get this output in my coding. My problem is i am not able to read the display...
2
by: kplkumar | last post by:
I have created a asp.net date picker control using Javascript. I wanted to do this to avoid doing post back when the calendar window is launched. It works like charm. But the challenge is - the...
0
by: OceanBreeze | last post by:
How can I add a value to the top of the DropDownList control using server side C# code. I have a value e.g. "27122". I need to add it the DropDownList control and display it. Meaning, this new...
1
by: Rushi | last post by:
Hi All, I want to publish one Event from my Header User Control using Master pAge . And want to attach/detach this event from individual pages. Details: I have one User Control Header.ascx....
5
by: rajdevramasamy | last post by:
Hi, In my webpage, i am adding my user control using rendercontrol() using the following code: Dim NumericEditControl As New Control NumericEditControl =...
2
by: crystalelle | last post by:
Hi, I am in desperate of help. I am working with visual basic .net 2005. My interface is made from treeview control and tab control. The big question here is how do I load the tab control...
9
by: WT | last post by:
Hello, I have code created with .net 1.0 and migrated to 3.5. Form 2.0 the XslTransform class is obsolete and the vs2008 compiler generates warnings that these classes are absolete suggesting to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.