system
October 28, 2014, 4:28am
1
hi every one !
in my program i need to set the delay time through serial command , i have made an example program for this as shown below
int led=4;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
char data = Serial.read();
Serial.println(data);
Serial.println("turn on the accessory");
digitalWrite(led, HIGH);
delay(data);
digitalWrite(led, LOW);
}}
will anybody please tell me what sort of mistake i am making in this
LarryD
October 28, 2014, 4:32am
2
data is chr type, not an integer
You will have to convert it.
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
https://learn.sparkfun.com/tutorials/data-types-in-arduino
http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/
Also, you're only reading a single character from serial, so you'll only be getting the numbers 0 to 9 in data. Is it likely that you'll see your LED flash for somewhere between 1/1000th and less than 1/100th of a second?
Robin2
October 28, 2014, 9:20am
4
Look at these two demos here and here which illustrate how to receive different types of data from the Serial monitor or from a PC program.
The Arduino code in both demos works with the Serial Monitor.
...R