1st "own" Project, Please check my code :) and help me out. [SOLVED]

Hi
This is my first ever "Own" creation project (Given up on spirit level calibration)

I am wanting to make a test box to check that trailer lights are working correctly. We have 2 different ways the plugs are wired.
I want to be able to Plug in the trailer plug, select whether it is Old or New style wiring, and then run a sequence. (Eg. If button old is pressed, then turn on the side lights, wait to get to the back of the trailer, then put on the brake light... Flash the left hand indicator and so on).
I have written my Sketch and there is already a fault, something to do with the brackets at the end?

Please can you have a look, see if it can be improved, and Also see if it will actually flash the indicators for 5 seconds.

//Define the trailer socket pins to the Arduino pins
int trlpin_A = 13; //old is nothing // New is Side lights
int trlpin_B = 12; //old is LHS Brake Light // New is LH Indicate
int trlpin_C = 11; //old is Convoy // New is Convoy
int trlpin_E = 10; //old is Side Lights // New is Side lights
int trlpin_F = 9;  //old is Fog Light // New is Brake Light (Blackout)??
int trlpin_H = 8;  //old is nothing // New is Fog Light
int trlpin_J = 7;  //old is RHS Brake light // New is RH Indicator
int trlpin_M = 6;  //old is LH Indicator // New is Brake Lights
int trlpin_N = 5;  //old is RH Indicator // New is Reverse Light
//Define the two pushbuttons to the Arduino
int switchPin_old = 4; //select device is plugged into Old style trailer plug
int switchPin_new = 3; //select device is plugged into New style trailer plug
int val1;
int val2;
int indFlashTime = 250; //Indicator flashing time on and off delay
int walkToBackDelay = 10000;
int delayForNextLight = 5000;

void setup() {
  pinMode(trlpin_A, OUTPUT);
  pinMode(trlpin_B, OUTPUT);
  pinMode(trlpin_C, OUTPUT);
  pinMode(trlpin_E, OUTPUT);
  pinMode(trlpin_F, OUTPUT);
  pinMode(trlpin_H, OUTPUT);
  pinMode(trlpin_J, OUTPUT);
  pinMode(trlpin_M, OUTPUT);
  pinMode(trlpin_N, OUTPUT);
  pinMode(switchPin_old, INPUT);
  pinMode(switchPin_new, INPUT);
}


void loop() {
  val1 = digitalRead(switchPin_old);
  val2 = digitalRead(switchPin_new);
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //run the sequence of testing lights IF the "Old" push button is pressed///
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (val1 == HIGH) {                 //IF the old style switch is pressed
    digitalWrite(trlpin_E, HIGH);     //Turn on the trailer side lights
    delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
    digitalWrite(trlpin_B, HIGH);     //Turn on the Left brake light
    digitalWrite(trlpin_J, HIGH);     //Turn on the Right brake light (Hopefully at the same time as left)
    delay(delayForNextLight);         //Delay for next light test (5 seconds)
    digitalWrite(trlpin_B, LOW);      //Turn off the Left brake light
    digitalWrite(trlpin_J, LOW);      //Turn off the Right brake light (Hopefully at the same time as left)
    //Hopefully the side lights are still on, but the brake lights have BOTH come on for 5 seconds and gone off again
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Next I want to test the indicators LHS first with falshing on and off for under 1 second for about 5 seconds (not sure how to change The 5 seconds)
    for (int x = 0; x < 10; x++) {
      digitalWrite(trlpin_M, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
      delay(250);
      digitalWrite(trlpin_M, LOW);
      delay(250);
      delay(delayForNextLight);

      for (int x = 0; x < 10; x++) {
        digitalWrite(trlpin_N, HIGH);   //Turn on the RH Indicator and flash (Hopefully!)
        delay(250);
        digitalWrite(trlpin_N, LOW);
        delay(250);
      }
      //Next still Keeping the side lights on, I want to test the Fog light
      digitalWrite(trlpin_F, HIGH);     //Turn on the rear Fog Light
      delay(delayForNextLight);         //Delay
      digitalWrite(trlpin_F, LOW);      //Turn off the rear Fog Light
      delay(delayForNextLight);         //Delay
      digitalWrite(trlpin_E, LOW);      //Turn off the side lights
      delay(delayForNextLight);
      digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Hopefully the side lights are now Off)
      delay(delayForNextLight);
      digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      //run the sequence of testing lights IF the "NEW" push button is pressed///
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      if (val2 == HIGH) {                 //IF the NEW style switch is pressed
        digitalWrite(trlpin_A, HIGH);//Turn on the trailer side lights
        digitalWrite(trlpin_E, HIGH);
        delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
        digitalWrite(trlpin_F, HIGH);     //Turn on the brake light (This could be Brake light Blackout???)
        digitalWrite(trlpin_M, HIGH);     //Turn on the brake lights (Hopefully at the same time as left)
        delay(delayForNextLight);         //Delay for next light test (5 seconds)
        digitalWrite(trlpin_F, LOW);      //Turn off the Left brake light
        digitalWrite(trlpin_M, LOW);      //Turn off the Right brake light (Hopefully at the same time as left)
        //Hopefully the side lights are still on, but the brake lights have BOTH come on for 5 seconds and gone off again
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Next I want to test the indicators LHS first with falshing on and off for under 1 second for about 5 seconds (not sure how to change The 5 seconds)
        for (int x = 0; x < 10; x++) {
          digitalWrite(trlpin_B, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
          delay(250);
          digitalWrite(trlpin_B, LOW);
          delay(250);
          delay(delayForNextLight);

          for (int x = 0; x < 10; x++) {
            digitalWrite(trlpin_J, HIGH);   //Turn on the RH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_J, LOW);
            delay(250);
          }
          //Next still Keeping the side lights on, I want to test the Fog light
          digitalWrite(trlpin_H, HIGH);     //Turn on the rear Fog Light
          delay(delayForNextLight);         //Delay
          digitalWrite(trlpin_H, LOW);      //Turn off the rear Fog Light
          delay(delayForNextLight);         //Delay
          digitalWrite(trlpin_A, LOW);      //Turn off the side lights
          digitalWrite(trlpin_E, LOW);
          digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Hopefully the side lights are now Off)
          delay(delayForNextLight);
          digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
        }
}

Here is my error code

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Nano, ATmega328"

nato_trl_light_test_v1.ino: In function 'void loop()':
nato_trl_light_test_v1:114: error: expected '}' at end of input
nato_trl_light_test_v1:114: error: expected '}' at end of input
nato_trl_light_test_v1:114: error: expected '}' at end of input
expected '}' at end of input

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

You were missing closing curly brackets at the end of 3 for loops. I think I put them in the right places (} // missing curly). I slightly reformatted your code. I like to have all the curly brackets on their own line. Missing mates are easier to see.

//Define the trailer socket pins to the Arduino pins
int trlpin_A = 13; //old is nothing // New is Side lights
int trlpin_B = 12; //old is LHS Brake Light // New is LH Indicate
int trlpin_C = 11; //old is Convoy // New is Convoy
int trlpin_E = 10; //old is Side Lights // New is Side lights
int trlpin_F = 9;  //old is Fog Light // New is Brake Light (Blackout)??
int trlpin_H = 8;  //old is nothing // New is Fog Light
int trlpin_J = 7;  //old is RHS Brake light // New is RH Indicator
int trlpin_M = 6;  //old is LH Indicator // New is Brake Lights
int trlpin_N = 5;  //old is RH Indicator // New is Reverse Light
//Define the two pushbuttons to the Arduino
int switchPin_old = 4; //select device is plugged into Old style trailer plug
int switchPin_new = 3; //select device is plugged into New style trailer plug
int val1;
int val2;
int indFlashTime = 250; //Indicator flashing time on and off delay
int walkToBackDelay = 10000;
int delayForNextLight = 5000;

void setup()
{
    pinMode(trlpin_A, OUTPUT);
    pinMode(trlpin_B, OUTPUT);
    pinMode(trlpin_C, OUTPUT);
    pinMode(trlpin_E, OUTPUT);
    pinMode(trlpin_F, OUTPUT);
    pinMode(trlpin_H, OUTPUT);
    pinMode(trlpin_J, OUTPUT);
    pinMode(trlpin_M, OUTPUT);
    pinMode(trlpin_N, OUTPUT);
    pinMode(switchPin_old, INPUT);
    pinMode(switchPin_new, INPUT);
}


void loop()
{
    val1 = digitalRead(switchPin_old);
    val2 = digitalRead(switchPin_new);
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //run the sequence of testing lights IF the "Old" push button is pressed///
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (val1 == HIGH)
    {                 //IF the old style switch is pressed
        digitalWrite(trlpin_E, HIGH);     //Turn on the trailer side lights
        delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
        digitalWrite(trlpin_B, HIGH);     //Turn on the Left brake light
        digitalWrite(trlpin_J, HIGH);     //Turn on the Right brake light (Hopefully at the same time as left)
        delay(delayForNextLight);         //Delay for next light test (5 seconds)
        digitalWrite(trlpin_B, LOW);      //Turn off the Left brake light
        digitalWrite(trlpin_J, LOW);      //Turn off the Right brake light (Hopefully at the same time as left)
        
        //Hopefully the side lights are still on, but the brake lights have BOTH come on for 5 seconds and gone off again
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Next I want to test the indicators LHS first with falshing on and off for under 1 second for about 5 seconds (not sure how to change The 5 seconds)
        for (int x = 0; x < 10; x++)
        {
            digitalWrite(trlpin_M, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_M, LOW);
            delay(250);
            delay(delayForNextLight);
        }  //  missing curly
        
        for (int x = 0; x < 10; x++)
        {
            digitalWrite(trlpin_N, HIGH);   //Turn on the RH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_N, LOW);
            delay(250);
        }
        //Next still Keeping the side lights on, I want to test the Fog light
        digitalWrite(trlpin_F, HIGH);     //Turn on the rear Fog Light
        delay(delayForNextLight);         //Delay
        digitalWrite(trlpin_F, LOW);      //Turn off the rear Fog Light
        delay(delayForNextLight);         //Delay
        digitalWrite(trlpin_E, LOW);      //Turn off the side lights
        delay(delayForNextLight);
        digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Hopefully the side lights are now Off)
        delay(delayForNextLight);
        digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
        
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //run the sequence of testing lights IF the "NEW" push button is pressed///
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (val2 == HIGH)
        {                 //IF the NEW style switch is pressed
            digitalWrite(trlpin_A, HIGH);//Turn on the trailer side lights
            digitalWrite(trlpin_E, HIGH);
            delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
            digitalWrite(trlpin_F, HIGH);     //Turn on the brake light (This could be Brake light Blackout???)
            digitalWrite(trlpin_M, HIGH);     //Turn on the brake lights (Hopefully at the same time as left)
            delay(delayForNextLight);         //Delay for next light test (5 seconds)
            digitalWrite(trlpin_F, LOW);      //Turn off the Left brake light
            digitalWrite(trlpin_M, LOW);      //Turn off the Right brake light (Hopefully at the same time as left)            
        }  //  missing curly
        
        //Hopefully the side lights are still on, but the brake lights have BOTH come on for 5 seconds and gone off again
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Next I want to test the indicators LHS first with falshing on and off for under 1 second for about 5 seconds (not sure how to change The 5 seconds)
        for (int x = 0; x < 10; x++)
        {
            digitalWrite(trlpin_B, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_B, LOW);
            delay(250);
            delay(delayForNextLight);
        }  //  missing curly
        for (int x = 0; x < 10; x++)
        {
            digitalWrite(trlpin_J, HIGH);   //Turn on the RH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_J, LOW);
            delay(250);
        }
        //Next still Keeping the side lights on, I want to test the Fog light
        digitalWrite(trlpin_H, HIGH);     //Turn on the rear Fog Light
        delay(delayForNextLight);         //Delay
        digitalWrite(trlpin_H, LOW);      //Turn off the rear Fog Light
        delay(delayForNextLight);         //Delay
        digitalWrite(trlpin_A, LOW);      //Turn off the side lights
        digitalWrite(trlpin_E, LOW);
        digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Hopefully the side lights are now Off)
        delay(delayForNextLight);
        digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
    }
}

groundfungus:
You were missing closing curly brackets at the end of 3 for loops. I think I put them in the right places (} // missing curly). I slightly reformatted your code. I like to have all the curly brackets on their own line. Missing mates are easier to see.

//Define the trailer socket pins to the Arduino pins

int trlpin_A = 13; //old is nothing // New is Side lights
int trlpin_B = 12; //old is LHS Brake Light // New is LH Indicate
int trlpin_C = 11; //old is Convoy // New is Convoy
int trlpin_E = 10; //old is Side Lights // New is Side lights
int trlpin_F = 9;  //old is Fog Light // New is Brake Light (Blackout)??
int trlpin_H = 8;  //old is nothing // New is Fog Light
int trlpin_J = 7;  //old is RHS Brake light // New is RH Indicator
int trlpin_M = 6;  //old is LH Indicator // New is Brake Lights
int trlpin_N = 5;  //old is RH Indicator // New is Reverse Light
//Define the two pushbuttons to the Arduino
int switchPin_old = 4; //select device is plugged into Old style trailer plug
int switchPin_new = 3; //select device is plugged into New style trailer plug
int val1;
int val2;
int indFlashTime = 250; //Indicator flashing time on and off delay
int walkToBackDelay = 10000;
int delayForNextLight = 5000;

void setup()
{
   pinMode(trlpin_A, OUTPUT);
   pinMode(trlpin_B, OUTPUT);
   pinMode(trlpin_C, OUTPUT);
   pinMode(trlpin_E, OUTPUT);
   pinMode(trlpin_F, OUTPUT);
   pinMode(trlpin_H, OUTPUT);
   pinMode(trlpin_J, OUTPUT);
   pinMode(trlpin_M, OUTPUT);
   pinMode(trlpin_N, OUTPUT);
   pinMode(switchPin_old, INPUT);
   pinMode(switchPin_new, INPUT);
}

void loop()
{
   val1 = digitalRead(switchPin_old);
   val2 = digitalRead(switchPin_new);
   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   //run the sequence of testing lights IF the "Old" push button is pressed///
   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   if (val1 == HIGH)
   {                 //IF the old style switch is pressed
       digitalWrite(trlpin_E, HIGH);     //Turn on the trailer side lights
       delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
       digitalWrite(trlpin_B, HIGH);     //Turn on the Left brake light
       digitalWrite(trlpin_J, HIGH);     //Turn on the Right brake light (Hopefully at the same time as left)
       delay(delayForNextLight);         //Delay for next light test (5 seconds)
       digitalWrite(trlpin_B, LOW);      //Turn off the Left brake light
       digitalWrite(trlpin_J, LOW);      //Turn off the Right brake light (Hopefully at the same time as left)
       
       //Hopefully the side lights are still on, but the brake lights have BOTH come on for 5 seconds and gone off again
       //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       //Next I want to test the indicators LHS first with falshing on and off for under 1 second for about 5 seconds (not sure how to change The 5 seconds)
       for (int x = 0; x < 10; x++)
       {
           digitalWrite(trlpin_M, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
           delay(250);
           digitalWrite(trlpin_M, LOW);
           delay(250);
           delay(delayForNextLight);
       }  //  missing curly
       
       for (int x = 0; x < 10; x++)
       {
           digitalWrite(trlpin_N, HIGH);   //Turn on the RH Indicator and flash (Hopefully!)
           delay(250);
           digitalWrite(trlpin_N, LOW);
           delay(250);
       }
       //Next still Keeping the side lights on, I want to test the Fog light
       digitalWrite(trlpin_F, HIGH);     //Turn on the rear Fog Light
       delay(delayForNextLight);         //Delay
       digitalWrite(trlpin_F, LOW);      //Turn off the rear Fog Light
       delay(delayForNextLight);         //Delay
       digitalWrite(trlpin_E, LOW);      //Turn off the side lights
       delay(delayForNextLight);
       digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Hopefully the side lights are now Off)
       delay(delayForNextLight);
       digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
       
       ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       //run the sequence of testing lights IF the "NEW" push button is pressed///
       ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       if (val2 == HIGH)
       {                 //IF the NEW style switch is pressed
           digitalWrite(trlpin_A, HIGH);//Turn on the trailer side lights
           digitalWrite(trlpin_E, HIGH);
           delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
           digitalWrite(trlpin_F, HIGH);     //Turn on the brake light (This could be Brake light Blackout???)
           digitalWrite(trlpin_M, HIGH);     //Turn on the brake lights (Hopefully at the same time as left)
           delay(delayForNextLight);         //Delay for next light test (5 seconds)
           digitalWrite(trlpin_F, LOW);      //Turn off the Left brake light
           digitalWrite(trlpin_M, LOW);      //Turn off the Right brake light (Hopefully at the same time as left)            
       }  //  missing curly
       
       //Hopefully the side lights are still on, but the brake lights have BOTH come on for 5 seconds and gone off again
       //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       //Next I want to test the indicators LHS first with falshing on and off for under 1 second for about 5 seconds (not sure how to change The 5 seconds)
       for (int x = 0; x < 10; x++)
       {
           digitalWrite(trlpin_B, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
           delay(250);
           digitalWrite(trlpin_B, LOW);
           delay(250);
           delay(delayForNextLight);
       }  //  missing curly
       for (int x = 0; x < 10; x++)
       {
           digitalWrite(trlpin_J, HIGH);   //Turn on the RH Indicator and flash (Hopefully!)
           delay(250);
           digitalWrite(trlpin_J, LOW);
           delay(250);
       }
       //Next still Keeping the side lights on, I want to test the Fog light
       digitalWrite(trlpin_H, HIGH);     //Turn on the rear Fog Light
       delay(delayForNextLight);         //Delay
       digitalWrite(trlpin_H, LOW);      //Turn off the rear Fog Light
       delay(delayForNextLight);         //Delay
       digitalWrite(trlpin_A, LOW);      //Turn off the side lights
       digitalWrite(trlpin_E, LOW);
       digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Hopefully the side lights are now Off)
       delay(delayForNextLight);
       digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
   }
}

Great Job groundfungus, much appreciated. :slight_smile:

I got the "Flash part" from a traffic light example, I want to make the indicators flash for about 5 seconds. As I copied directly, will this work, or do I need to modify it? I'm not familiar with the for statement :confused:

 for (int x = 0; x < 10; x++)
        {
            digitalWrite(trlpin_M, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_M, LOW);
            delay(250);
            delay(delayForNextLight);
        }

As it is the delay(delayForNextLight) will rum every time through the for loop (10 times). Probably that line should be outside of the loop (my bad) so that once the loop finishes, the delay will happen only once between the for loops. Check the other for loops to see if that line needs to be moved. Blink shoud work, hook up some LEDs and test.

for (int x = 0; x < 10; x++)
        {
            digitalWrite(trlpin_M, HIGH);   //Turn on the LH Indicator and flash (Hopefully!)
            delay(250);
            digitalWrite(trlpin_M, LOW);
            delay(250);
        }
            delay(delayForNextLight);

Thanks groundfungus. I have found a explication to the for function, so I'm going to study it now. I got requested to sort dinner out!

Thanks again

You are welcome. If you have more questions, we are here all week.