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
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
//============================================================================================
String timeLeds_Serial;
//============================================================================================
int led_green_trafficLight;
int led_yellow_trafficLight;
int led_red_trafficLight;
//============================================================================================
void setup () {
Serial.begin(9600);
delay(1000);
}
//============================================================================================
void funcGREEN() /// GREEN
{
timeLeds_Serial.remove(0, 5);
led_green_trafficLight = timeLeds_Serial.toInt();
}
void funcYELLOW() /// YELLOW
{
timeLeds_Serial.remove(0, 6);
led_yellow_trafficLight = timeLeds_Serial.toInt();
}
void funcRED() /// RED
{
timeLeds_Serial.remove(0, 3);
led_red_trafficLight = timeLeds_Serial.toInt();
}
//============================================================================================
void trafficLight() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
delay(led_green_trafficLight); // GREEN LED
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delay(led_yellow_trafficLight); // YELLOW LED
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(led_red_trafficLight); // RED LED
}
//============================================================================================
void loop () {
trafficLight();
if (Serial.available() > 0)
{
timeLeds_Serial = Serial.readStringUntil('\n');
if (timeLeds_Serial.startsWith("GREEN"))
{
funcGREEN();
}
if (timeLeds_Serial.startsWith("YELLOW"))
{
funcYELLOW();
}
if (timeLeds_Serial.startsWith("RED"))
{
funcRED();
}
}
//============================================================================================
delay(1000);
Serial.println("Green = " + led_green_trafficLight);
delay(1000);
Serial.println("Yellow = " + led_yellow_trafficLight);
delay(1000);
Serial.println("Red = " + led_red_trafficLight);
delay(1000);
}
and sometimes a strange error happens, I'll send a print, in this case I put it in the serial YELLOW20000
