Hi - I got this sketch from a website. It's used for controlling LED's on a Scalextric race track.
The code works fine - if I connect the LED's and run the sketch, the LED's turns on and off as expected.
But there is a lot more data then used in the sketch and I would like to see the data in the Serial Monitor if possible?
Can someone give me a snip of code that shows all data in the Serial Monitor? Every string starts with [ and ends with ] and in between is the data I need
Used Sketch:
//start
// define which pins are to be used for the various light functions
#define GREEN1 12 //edit for your green pin
#define AMBER1 11 //edit for your amber/yellow pin
#define RED1 10 //edit for your 5th red pin
#define RED2 9 //edit for your 4th red pin
#define RED3 8 //edit for your 3rd red pin
#define RED4 7 //edit for your 2nd red pin
#define RED5 6 //edit for your 1st red pin
#define BLUE1 5 //edit for your blue pin
int amberswPin = 4;//edit to be any unused digital pin
int blueswPin = 3;//edit to be any unused digital pin
int ledPin = 13;
unsigned long nextBlink;
char amberswState = LOW;
char blueswState = LOW;
char ledState = LOW;
int CarsInPits = 0;
// setup the pins for outputs, blinking and open the serial port
void setup()
{
pinMode(GREEN1, OUTPUT);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(RED5, OUTPUT);
pinMode(AMBER1, OUTPUT);
pinMode(BLUE1, OUTPUT);
pinMode(ledPin, OUTPUT);
amberswState = LOW;
ledState = LOW;
nextBlink = millis();
Serial.begin(9600);
}
void blink (void)
{ unsigned long now;
now = millis();
if (now >= nextBlink) {
nextBlink = now + 1000; // Next change in one second or edit value 1000 to prefered milliseconds
if (ledState == LOW) {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
}
else {
digitalWrite(ledPin, LOW);
ledState = LOW;
}
}
}
// look for data on the serial port and turn on/off the lights
// data is ascii strings
void loop()
{ blink();
if (ledState == HIGH && amberswState == HIGH) {digitalWrite (AMBER1, HIGH); digitalWrite(GREEN1, LOW);}
else {digitalWrite (AMBER1, LOW);}
if (ledState == LOW && amberswState == HIGH) {digitalWrite (AMBER1, LOW);}
if (ledState == HIGH && blueswState == HIGH) {digitalWrite (BLUE1, HIGH);}
else {digitalWrite (BLUE1, LOW);}
if (ledState == LOW && blueswState == HIGH) {digitalWrite (BLUE1, LOW);}
if ( Serial.available() )
{
String b;
b = Serial.readStringUntil('[');
{ String s;
s = Serial.readStringUntil(']');
if ( s == "SL010" ) { digitalWrite(RED1, LOW); }
if ( s == "SL011" ) { digitalWrite(RED1, HIGH); }
if ( s == "SL020" ) { digitalWrite(RED2, LOW); }
if ( s == "SL021" ) { digitalWrite(RED2, HIGH); }
if ( s == "SL030" ) { digitalWrite(RED3, LOW); }
if ( s == "SL031" ) { digitalWrite(RED3, HIGH); }
if ( s == "SL040" ) { digitalWrite(RED4, LOW); }
if ( s == "SL041" ) { digitalWrite(RED4, HIGH); }
if ( s == "SL050" ) { digitalWrite(RED5, LOW); }
if ( s == "SL051" ) { digitalWrite(RED5, HIGH); digitalWrite(RED1, LOW); digitalWrite(RED2, LOW); digitalWrite(RED3, LOW); digitalWrite(RED4, LOW); digitalWrite(AMBER1, LOW); digitalWrite(GREEN1, LOW);}
if ( s == "SL060" ) { digitalWrite(GREEN1, LOW); }
if ( s == "SL061" ) { digitalWrite(GREEN1, HIGH); }
if ( s == "SL080" ) { digitalWrite(amberswPin, LOW); amberswState = LOW; digitalWrite(GREEN1, HIGH);}
if ( s == "SL081" ) { digitalWrite(amberswPin, HIGH); amberswState = HIGH;}
if ( s == "PT010" ) { CarsInPits-- ;} // A car just left the pits, minus one car from CarsInPits count
if ( s == "PT011" ) { CarsInPits++ ;} // A car just entered the pits, plus one car to CarsInPits count
if ( s == "PT020" ) { CarsInPits-- ;}
if ( s == "PT021" ) { CarsInPits++ ;}
if ( s == "PT030" ) { CarsInPits-- ;}
if ( s == "PT031" ) { CarsInPits++ ;}
if ( s == "PT040" ) { CarsInPits-- ;}
if ( s == "PT041" ) { CarsInPits++ ;}
if ( s == "PT050" ) { CarsInPits-- ;}
if ( s == "PT051" ) { CarsInPits++ ;}
if ( s == "PT060" ) { CarsInPits-- ;}
if ( s == "PT061" ) { CarsInPits++ ;}
if (CarsInPits <= 0 ) { //if there are zero cars in the pit, turn off the blue lights
digitalWrite(blueswPin, LOW); blueswState = LOW;
}
else { // if there is one or more cars in the pit , keep the blue lights going
digitalWrite(blueswPin, HIGH); blueswState = HIGH;
}
if (CarsInPits < 0 || CarsInPits > 6) {CarsInPits = 0; } //if number of cars in pits gets messed up reset to zero [ '||' = 'or' ]
//Serial.println(CarsInPits); //debug
}
}
}
//end