/*****************************************************************************************
   PC Lap Counter Messages
 *****************************************************************************************/
#define RED1_ON  "SL011"
#define RED1_OFF "SL010"
#define RED2_ON  "SL021"
#define RED2_OFF "SL020"
#define RED3_ON  "SL031"
#define RED3_OFF "SL030"
#define RED4_ON  "SL041"
#define RED4_OFF "SL040"
#define RED5_ON  "SL051"
#define RED5_OFF "SL050"

#define GO_ON       "SL061"
#define GO_OFF      "SL060"
#define STOP_ON     "SL071"
#define STOP_OFF    "SL070"
#define CAUTION_ON  "SL081"
#define CAUTION_OFF "SL080"

#define PWR_ON    "PW001"
#define PWR_OFF   "PW000"
#define PWR_1_ON  "PW011"
#define PWR_1_OFF "PW010"
#define PWR_2_ON  "PW021"
#define PWR_2_OFF "PW020"
#define PIT_1_IN  "PT011"
#define PIT_1_OUT  "PT010"
#define PIT_2_IN  "PT021"
#define PIT_2_OUT  "PT020"


//
// DEFINIZIONE PIN USATI PER PULSANTI
//
#define Start_Race 46
#define Pause_Race 41
#define ReStart_Race 49
#define End_Race 2
#define Power_on 52
#define Yellow_Flag 51
#define Stop_Go_Line_1 53
#define Stop_Go_Line_2 50
int stato_pulsante;
int stato_pulsante_PWR = LOW;

//
// DEFINIZIONE PIN USATI PER LUCI LED
//
#define GREEN1 8
#define AMBER 11
#define RED1 7
#define RED2 6
#define RED3 5
#define RED4 4
#define RED5 3
#define led_1 22
#define led_2 24
#define lane_1_go 39
#define lane_1_stop 31
#define lane_1_pit 30
#define lane_2_go 23
#define lane_2_stop 25
#define lane_2_pit 26
#define led_power_on 37
#define led_power_off 32
//
// DEFINIZIONE PIN USATI PER RELE'
//
#define Relay_1 10
#define Relay_2 9
#define STARTER 48
//
//
void setup() {
//  
  Serial.begin(9600);
//
// DEFINIZIONE TIPOLOGIA PIN INPUT / OUTPUT
//
pinMode(Start_Race, INPUT_PULLUP);
pinMode(Pause_Race, INPUT_PULLUP);
pinMode(ReStart_Race, INPUT_PULLUP);
pinMode(End_Race, INPUT_PULLUP);
pinMode(Power_on, INPUT_PULLUP);
pinMode(Yellow_Flag, INPUT_PULLUP);
pinMode(Stop_Go_Line_1, INPUT_PULLUP);
pinMode(Stop_Go_Line_2, INPUT_PULLUP);
pinMode(GREEN1, OUTPUT);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(RED5, OUTPUT);
pinMode(AMBER, OUTPUT);
pinMode (Relay_1,OUTPUT);
pinMode (Relay_2,OUTPUT);
pinMode (STARTER,OUTPUT);
pinMode (led_1,OUTPUT);
pinMode (led_2,OUTPUT);
pinMode (lane_1_go,OUTPUT);
pinMode (lane_1_stop,OUTPUT);
pinMode (lane_1_pit,OUTPUT);
pinMode (lane_2_go,OUTPUT);
pinMode (lane_2_stop,OUTPUT);
pinMode (lane_2_pit,OUTPUT);
pinMode (led_power_on,OUTPUT);
pinMode (led_power_off,OUTPUT);

//
// DEFINIZIONE STATO PIN AVVIO
//
digitalWrite(Start_Race,HIGH);
digitalWrite(Pause_Race,HIGH);
digitalWrite(ReStart_Race,HIGH);
digitalWrite(End_Race,HIGH);
digitalWrite(Power_on,HIGH);
digitalWrite(Yellow_Flag,HIGH);
digitalWrite(Stop_Go_Line_1,HIGH);
digitalWrite(Stop_Go_Line_2,HIGH);
digitalWrite(STARTER,LOW);
digitalWrite(led_1,LOW);
digitalWrite(led_2,LOW);
digitalWrite(RED1,HIGH);
digitalWrite(RED2,HIGH);
digitalWrite(RED3,HIGH);
digitalWrite(RED4,HIGH);
digitalWrite(RED5,HIGH);
digitalWrite(Relay_1,LOW);
digitalWrite(Relay_2,LOW);
digitalWrite(lane_1_go,LOW);
digitalWrite(lane_1_stop,HIGH);
digitalWrite(lane_1_pit,LOW);
digitalWrite(lane_2_go,LOW);
digitalWrite(lane_2_stop,HIGH);
digitalWrite(lane_2_pit,LOW);
digitalWrite(led_power_off,HIGH);
digitalWrite(led_power_on,LOW);
stato_pulsante = 0;
}
//
void loop() {
  // put your main code here, to run repeatedly:
  
// PRESSIONE PULSANTE START 
if (digitalRead(Start_Race)==LOW){
                                  stato_pulsante =  1;
                                  //stato_pulsante_PWR = HIGH;
                                  digitalWrite(STARTER, HIGH);
                                  digitalWrite(Relay_1, HIGH);
                                  digitalWrite(Relay_2, HIGH);
                                  Serial.println("[BT01]");
                                  delay(600);
                                  }
                                 
// PRESSIONE PULSANTE PAUSE                                   
if (digitalRead(Pause_Race)==LOW){
                                  stato_pulsante =  0;
                                  //stato_pulsante_PWR = LOW;
                                  Serial.println("[BT03]");
                                  delay(1000);
                                  digitalWrite(RED5,HIGH);
                                  digitalWrite(RED4,HIGH);
                                  digitalWrite(RED3,HIGH);
                                  digitalWrite(RED2,HIGH);
                                  digitalWrite(RED1,HIGH);
                                  digitalWrite(STARTER, LOW);
                                  digitalWrite(Relay_1, LOW);
                                  digitalWrite(Relay_2, LOW);
                                                                    }
// PRESSIONE PULSANTE RESTART
if (digitalRead(ReStart_Race)==LOW){
                                    stato_pulsante =  0;
                                    //stato_pulsante_PWR = HIGH;
                                    Serial.println("[BT02]");
                                    delay(600);
                                    stato_pulsante =  0;
                                    }
// PRESSIONE PULSANTE END
if (digitalRead(End_Race)==LOW){
                                stato_pulsante =  0;
                                //stato_pulsante_PWR = LOW;
                                Serial.println("[BT07]");
                                delay(1000);
                                digitalWrite(RED5,HIGH);
                                digitalWrite(RED4,HIGH);
                                digitalWrite(RED3,HIGH);
                                digitalWrite(RED2,HIGH);
                                digitalWrite(RED1,HIGH);
                                digitalWrite(GREEN1, LOW);
                                digitalWrite(STARTER, LOW);
                                digitalWrite(Relay_1, LOW);
                                digitalWrite(Relay_2, LOW);
                                }
// PRESSIONE PULSANTE POWER 
if (digitalRead(Power_on)==LOW){

                                  stato_pulsante_PWR = ! stato_pulsante_PWR;
                                  Serial.println("[BT08]");
                                  delay(600);
                                
                                    if (stato_pulsante_PWR == HIGH) {
                                      digitalWrite(Relay_1, HIGH);
                                      digitalWrite(Relay_2, HIGH);
                                      digitalWrite(GREEN1, HIGH);
                                      digitalWrite(lane_1_go,HIGH);
                                      digitalWrite(lane_1_stop,LOW);
                                      digitalWrite(lane_2_go,HIGH);
                                      digitalWrite(lane_2_stop,LOW);
                                      digitalWrite(led_power_off,LOW);
                                      digitalWrite(led_power_on,HIGH);
                                      digitalWrite(led_1, HIGH);
                                      digitalWrite(led_2, HIGH);
                                      digitalWrite(RED1,LOW);
                                      digitalWrite(RED2,LOW);
                                      digitalWrite(RED3,LOW);
                                      digitalWrite(RED4,LOW);
                                      digitalWrite(RED5,LOW);

                                } else {
                                      digitalWrite(Relay_1, LOW);
                                      digitalWrite(Relay_2, LOW);
                                      digitalWrite(GREEN1, LOW);
                                      digitalWrite(lane_1_go,LOW);
                                      digitalWrite(lane_1_stop,HIGH);
                                      digitalWrite(lane_2_go,LOW);
                                      digitalWrite(lane_2_stop,HIGH);
                                      digitalWrite(led_power_off,HIGH);
                                      digitalWrite(led_power_on,LOW);
                                      digitalWrite(led_1, LOW);
                                      digitalWrite(led_2, LOW);
                                      digitalWrite(RED1,HIGH);
                                      digitalWrite(RED2,HIGH);
                                      digitalWrite(RED3,HIGH);
                                      digitalWrite(RED4,HIGH);
                                      digitalWrite(RED5,HIGH);
                                    }
                                }
// PRESSIONE PULSANTE YELLOW FLAG                                
if (digitalRead(Yellow_Flag)==LOW){
                                   //stato_pulsante =  0;
                                   digitalWrite(AMBER, HIGH);
                                   //digitalWrite(GREEN1, LOW);
                                   //digitalWrite(Relay_1, LOW);
                                   // digitalWrite(Relay_2, LOW);
                                   Serial.println("[BT09]");
                                   delay(600);
                                   }
// PRESSIONE PULSANTI STOP & GO LANE 1 E 2                                   
if (digitalRead(Stop_Go_Line_1)==LOW){
                                      Serial.println("[SG01]");
                                      delay(1000);
                                      }
if (digitalRead(Stop_Go_Line_2)==LOW){
                                      Serial.println("[SG02]");
                                      delay(1000);
                                      }
// condizioni lettura stato seriale
//
if ( Serial.available() )
//
{ String b;
b = Serial.readStringUntil('[');
  { String s;
 
  s = Serial.readStringUntil(']');
  
  if ( s == "RC1E00:00:01" ) {
                              //digitalWrite(STARTER, HIGH);
                              //delay (100);
                              digitalWrite(STARTER, LOW);
                              //stato_pulsante_PWR = LOW;
                              }
  if ( s == "SL011" ) { digitalWrite(RED5, LOW); }
  if ( s == "SL021" ) { digitalWrite(RED4, LOW); }
  if ( s == "SL031" ) { digitalWrite(RED3, LOW); }
  if ( s == "SL041" ) { digitalWrite(RED2, LOW); }
  if ( s == "SL051" ) { digitalWrite(RED1, LOW); }
  if ( s == "SL060" ) { digitalWrite(GREEN1, LOW); }
  if ( s == "SL061" ) { digitalWrite(GREEN1, HIGH);}
  if ( s == "SL080" ) { digitalWrite(AMBER, LOW);digitalWrite(GREEN1, HIGH); }
  if ( s == "SL081" ) { 
                        digitalWrite(AMBER, HIGH);
                        digitalWrite(GREEN1, LOW);
                        digitalWrite(Relay_1, LOW);
                        digitalWrite(Relay_2, LOW);
                      }
  if ( s == "PW000" ) {
                        digitalWrite(GREEN1, LOW);
                        digitalWrite(Relay_1, LOW);
                        digitalWrite(Relay_2, LOW);
                        digitalWrite(led_1, LOW);
                        digitalWrite(led_2, LOW);
                        digitalWrite(lane_1_go,LOW);
                        digitalWrite(lane_1_stop,HIGH);
                        digitalWrite(lane_2_go,LOW);
                        digitalWrite(lane_2_stop,HIGH);
                        digitalWrite(led_power_off,HIGH);
                        digitalWrite(led_power_on,LOW);
                      }

  if ( s == "PW001" ) {
                        digitalWrite(GREEN1, HIGH);
                        digitalWrite(Relay_1, HIGH);
                        digitalWrite(Relay_2, HIGH);
                        digitalWrite(led_1, HIGH);
                        digitalWrite(led_2, HIGH);
                        digitalWrite(lane_1_go,HIGH);
                        digitalWrite(lane_1_stop,LOW);
                        digitalWrite(lane_2_go,HIGH);
                        digitalWrite(lane_2_stop,LOW);
                        digitalWrite(led_power_off,LOW);
                        digitalWrite(led_power_on,HIGH);
                       }
  if (( s == "PW010") && (stato_pulsante =  0)) 
                      { digitalWrite(Relay_1, LOW);
                        digitalWrite(led_1, LOW);
                        digitalWrite(lane_1_go,LOW);
                        digitalWrite(lane_1_stop,HIGH);
                      }
  if ( s == "PW010" ) 
                      { digitalWrite(Relay_1, LOW);
                        digitalWrite(led_1, LOW);
                        digitalWrite(lane_1_go,LOW);
                        digitalWrite(lane_1_stop,HIGH);
                      }
  if ( s == "PW011" ) 
                      { digitalWrite(Relay_1, HIGH);
                        digitalWrite(led_1, HIGH);
                        digitalWrite(lane_1_go,HIGH);
                        digitalWrite(lane_1_stop,LOW);
                      }
  if ( s == "FL010000" )
                       { digitalWrite(Relay_1, LOW);
                         digitalWrite(led_1,LOW);
                         digitalWrite(lane_1_go,LOW);
                         digitalWrite(lane_1_stop,HIGH);
                       }
  if (( s == "PW020") && (stato_pulsante =  0))
                       { digitalWrite(Relay_2, LOW);
                         digitalWrite(led_2, LOW);
                         digitalWrite(lane_2_go,LOW);
                         digitalWrite(lane_2_stop,HIGH);
                       }
  if ( s == "PW020" ) 
                      { digitalWrite(Relay_2, LOW);
                        digitalWrite(led_2, LOW);
                        digitalWrite(lane_2_go,LOW);
                        digitalWrite(lane_2_stop,HIGH);

                      }
  if ( s == "PW021" ) 
                       { digitalWrite(Relay_2, HIGH);
                         digitalWrite(led_2, HIGH);
                         digitalWrite(lane_2_go,HIGH);
                         digitalWrite(lane_2_stop,LOW);
                       }
  if ( s == "FL020000" ) 
                       { digitalWrite(Relay_2, LOW);
                         digitalWrite(led_2,LOW); 
                         digitalWrite(lane_2_go,LOW);
                         digitalWrite(lane_2_stop,HIGH);
                       }


 if ( s == "PT010" ) 
                       {                       
                         digitalWrite(lane_1_pit,LOW);
                       }

if ( s == "PT011" ) 
                       {                       
                         digitalWrite(lane_1_pit,HIGH);
                       }
if ( s == "PT020" ) 
                       {                       
                         digitalWrite(lane_2_pit,LOW);
                       }

if ( s == "PT021" ) 
                       {                       
                         digitalWrite(lane_2_pit,HIGH);
                       }
    }
  }
}
$abcdeabcde151015202530fghijfghij
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module