I'm learning Arduino, and I'm making a code of one traffic light, to control the time of the LEDs using the serial monitor, through a keyword and the time for each LED to stay on, the code for some reason only works when I try GREEN on serial, when I try to put a value on YELLOW or RED, it doesn't work
thanks, i tried to read the serial only once, but when i do that i realized that only the first if of the green works, even though i insert in the serial YELLOW for example
The serial input basics methods that return multiple characters return strings (null terminated character arrays), not Strings (of the String class). It is recommenced to not use the String class with the Arduinos with small amounts of SRAM as memory problems can occur. See the evils of Strings.
That means that you cannot use equality (==) to compare strings and you cannot use + to concatenate strings. There are a set of functions to do those things with strings. See HERE.
In case you may be interested here is a demo of the serial input basics read with end marker and using the strtok function to read and parse your color and time data from serial monitor. The code uses the strcmp function to compare the incoming color.
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
// you should use unsigned long for timing variables
unsigned long led_green_trafficLight;
unsigned long led_yellow_trafficLight;
unsigned long led_red_trafficLight;
void setup()
{
Serial.begin(115200);
Serial.println("<Arduino is ready> Enter a color and a time separated by a space");
Serial.println("Like YELLOW 20000");
Serial.println("make sure newline is enabled in serial monitor line endings");
}
void loop()
{
recvWithEndMarker();
//showNewData();
if (newData)
{
parseData();
Serial.print("Green = ");
Serial.println(led_green_trafficLight);
Serial.print("Yellow = ");
Serial.println(led_yellow_trafficLight);
Serial.print("Red = ");
Serial.println(led_red_trafficLight);
newData = false;
}
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (rc == '\r') // ignore carruage return
{
return;
}
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true)
{
Serial.print("This just in ... ");
Serial.println(receivedChars);
//newData = false;
}
}
void parseData()
{
char *strings[3]; // an array of pointers to the pieces of the above array after strtok()
char *ptr = NULL; byte index = 0;
ptr = strtok(receivedChars, " "); // delimiters, space
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, " ");
}
/*
//Serial.println(index);
// print all the parts
Serial.println("The Pieces separated by strtok()");
for (int n = 0; n < index; n++)
{
Serial.print("piece ");
Serial.print(n);
Serial.print(" = ");
Serial.println(strings[n]);
}
*/
// convert string data to numbers
char* color = strings[0];
unsigned long time = atol(strings[1]);
Serial.print("color = ");
Serial.print(color);
Serial.print(" time = ");
Serial.print(time);
Serial.println(); // blank line
if (strcmp(color, "YELLOW") == 0)
{
led_yellow_trafficLight = time;
}
else
{
led_yellow_trafficLight = 0;
}
if (strcmp(color, "GREEN") == 0)
{
led_green_trafficLight = time;
}
else
{
led_green_trafficLight = 0;
}
if (strcmp(color, "RED") == 0)
{
led_red_trafficLight = time;
}
else
{
led_red_trafficLight = 0;
}
//newData = false;
}
I recommend using the SafeString-libary
It offers (most) of the comfort of Strings (variable-type with a capital "S"
and is even more secure than c_strings (in short string with a lowcase "s".
It’s written using cStrings… so can’t be more secure than cStrings… it’s just that the developer was careful with memory overflow - as you could be without this large library
I recommend understanding cStrings and memory constraints And then pick what suits your case best.
thanks for all the answers and for the library suggestion, i'll analyze which one will be better in my case, anyway it will be useful to learn how to use cstrings