Help with my Code Please...

Hi
This is my first "Own Written" project. I am learning more and more day by day.
The sketch all works fine, but I'm not convinced I have written it as best as I could. The project is to test Trailer lights, that are wired up in two different ways. So you push one button for the old style, and another for the new style of wiring.
I have learnt that I can create a Void such as in my sketch void Tft_Side () That way I can use it for both switch functions.
But I have all my code written twice for the two Buttons due to the fact the Pins change. Is there a way of creating "Voids" for the different tests, and then change the pins somehow or have I managed the best I can?

The code will follow, as I can't fit it in the 9000 Char limit!

Cheers

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"    

#define _rst 1      ///48
#define _dc 2       ///49
#define _miso 50    ///Not used
#define _mosi 51    ///Nano 11
#define _sclk 52    ///Nano 13
#define _cs 53      ///Nano 10

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);

//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;//Not Used anymore
//int val2;
int indFlashTime = 350; //Indicator flashing time on and off delay
int brakeFlashTime = 800; //Time for brake light flashes
int walkToBackDelay = 10000; //Time given to walk around the trailer
int delayForNextLight = 4000;
int tftDelay = 100;
int buttonStateOldSw;// For De-Bouncing the swithes
int buttonStateNewSw;
int oldSwVal1;
int oldSwVal2;
int newSwVal1;
int newSwVal2;
int lightModeOld = 0;
int lightModeNew = 0;

void setup()
{
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(3);
  testTriangles();
  testRoundRects();
  testFilledRoundRects();
  tft.fillScreen(ILI9340_BLACK);
  tft.setTextColor(ILI9340_RED);
  tft.setTextSize(3);
  tft.setCursor(40, 10);
  tft.println("Please Choose");
  tft.setTextSize(2);
  tft.setCursor(20, 80);
  tft.setTextColor(ILI9340_GREEN);
  tft.println("Button 1 for OLD style");
  tft.setCursor(20, 120);
  tft.println("Button 2 for NEW style");
  tft.setTextColor(ILI9340_MAGENTA);
  tft.setCursor(20, 190);
  tft.println("NATO Trailer Light Tester");
  tft.setCursor(75, 220);
  tft.println("By Charles Brennan");
  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);
  buttonStateOldSw = digitalRead(switchPin_old);  //button debounce
  buttonStateNewSw = digitalRead(switchPin_new);  //button debounce
}


void loop()
{
  oldSwVal1 = digitalRead(switchPin_old);
  delay(10);
  oldSwVal2 = digitalRead(switchPin_old);
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //run the sequence of testing lights IF the "Old" push button is pressed///
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (oldSwVal1 == oldSwVal2) {                 // make sure we got 2 consistant readings!
    if (oldSwVal1 != buttonStateOldSw) {          // the button state has changed!
      if (oldSwVal1 == HIGH) {                // check if the button is pressed
        if (lightModeOld == 0) {          // is the light off?
          lightModeOld = 1;               // turn light on!
          Tft_Old_Test ();
          digitalWrite(trlpin_E, HIGH);     //Turn on the trailer side lights
          Tft_Side ();
          delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
          
          for (int x = 0; x < 5; x++)//Turn on the brake and flash 5 Times
          {
            Tft_Brake ();
            digitalWrite(trlpin_B, HIGH);   
            digitalWrite(trlpin_J, HIGH);
            delay(brakeFlashTime);
            digitalWrite(trlpin_B, LOW);
            digitalWrite(trlpin_J, LOW);
            delay(brakeFlashTime);
          }
        }
                          
          
          Tft_LH_Ind ();
          for (int x = 0; x < 10; x++)
          {
            digitalWrite(trlpin_M, HIGH);   //Turn on the LH Indicator and flash 
            delay(indFlashTime);
            digitalWrite(trlpin_M, LOW);
            delay(indFlashTime);
          }//  missing curly
          delay(delayForNextLight);

    
          Tft_RH_Ind ();  
          for (int x = 0; x < 10; x++)
          {
            
            digitalWrite(trlpin_N, HIGH);   //Turn on the RH Indicator and flash 
            delay(indFlashTime);
            digitalWrite(trlpin_N, LOW);
            delay(indFlashTime);
          }
          delay(delayForNextLight);

        
          Tft_Fog ();  
          digitalWrite(trlpin_F, HIGH);     //Turn on the rear Fog Light
          delay(delayForNextLight);         //Delay
          digitalWrite(trlpin_F, LOW);      //Turn off the rear Fog Light
          tft.fillScreen(ILI9340_BLACK);
          digitalWrite(trlpin_E, LOW);      //Turn off the side lights
          delay(delayForNextLight);
          digitalWrite(trlpin_C, HIGH);     //Turn On convoy light (Side lights are now Off)
          Tft_Convoy ();  
          delay(delayForNextLight);
          digitalWrite(trlpin_C, LOW);      //Turn Off the Convoy Light
          Tft_End ();  
            delay(5000);
            software_Reset();
        } else {
          lightModeOld = 0;
                }
      }
    }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //run the sequence of testing lights IF the "NEW" push button is pressed///
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        {
          newSwVal1 = digitalRead(switchPin_new);
          delay(10);
          newSwVal2 = digitalRead(switchPin_new);
          if (newSwVal1 == newSwVal2) {                 // make sure we got 2 consistant readings!
            if (newSwVal1 != buttonStateNewSw) {          // the button state has changed!
              if (newSwVal1 == HIGH) {                // check if the button is pressed
                if (lightModeNew == 0) {          // is the light off?
                  lightModeNew = 1;               // turn light on!
                  Tft_New_Test ();
                  //IF the NEW style switch is pressed
                  Tft_Side ();
                  digitalWrite(trlpin_A, HIGH);
                  digitalWrite(trlpin_E, HIGH);
                  Serial.println("Turn On Side Lights 2 LED");  //Turn on the trailer side lights
                  delay(walkToBackDelay);                       //Wait 10 seconds to get to walk to back of trailer
                  for (int x = 0; x < 5; x++)
          {
            Tft_Brake ();
            digitalWrite(trlpin_F, HIGH);   //Turn on the brake and flash 
            digitalWrite(trlpin_M, HIGH);
            delay(brakeFlashTime);
            digitalWrite(trlpin_F, LOW);
            digitalWrite(trlpin_M, LOW);
            delay(brakeFlashTime);
          }


                 
                }  

                
                Tft_LH_Ind ();                    //Turn on the LH Indicator and flash 
                for (int x = 0; x < 10; x++)
                {
                  digitalWrite(trlpin_B, HIGH);   
                  delay(indFlashTime);
                  digitalWrite(trlpin_B, LOW);
                  delay(indFlashTime);
                }
                  delay(delayForNextLight);
                  
                Tft_RH_Ind ();                    //Turn on the RH Indicator and flash  
                for (int x = 0; x < 10; x++)
                {
                  digitalWrite(trlpin_J, HIGH);   
                  delay(indFlashTime);
                  digitalWrite(trlpin_J, LOW);
                  delay(indFlashTime);
                }
                  delay(delayForNextLight);
                
                  digitalWrite(trlpin_H, HIGH);     //Turn on the rear Fog Light
                  Tft_Fog ();
                  delay(delayForNextLight);         //Delay
                  digitalWrite(trlpin_H, LOW);      //Turn off the rear Fog Light
                  tft.fillScreen(ILI9340_BLACK);
                  delay(delayForNextLight);         //Delay
                  digitalWrite(trlpin_A, LOW);      //Turn off the side lights
                  digitalWrite(trlpin_E, LOW);
                  Tft_Convoy ();
                  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
                  tft.fillScreen(ILI9340_BLACK);
                  Tft_End ();
                  delay(5000);
                  software_Reset();
              } else {
                lightModeNew = 0;
                }
              }
              }
            }
        }
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0");  
}  
void Tft_Side ()
{
          tft.fillScreen(ILI9340_RED);
          tft.setCursor(50, 90);
          tft.setTextColor(ILI9340_WHITE);
          tft.setTextSize(10);
          tft.println("SIDE");
}
void Tft_Brake ()
{
            tft.fillScreen(ILI9340_BLACK);
            delay(tftDelay);
            tft.fillScreen(ILI9340_RED);
            tft.setCursor(50, 100);
            tft.setTextColor(ILI9340_WHITE);
            tft.setTextSize(8);
            tft.println("BRAKE");
}
void Tft_LH_Ind ()
{
          tft.fillScreen(ILI9340_BLUE);
          delay(tftDelay);
          tft.setCursor(50, 165);
          tft.setTextColor(ILI9340_YELLOW);
          tft.fillCircle(60, 60, 60, ILI9340_YELLOW);
          tft.setTextSize(10);
          tft.println("LEFT");
}
void Tft_RH_Ind ()
{
          tft.fillScreen(ILI9340_BLUE);
          delay(tftDelay);
          tft.setCursor(40, 165);
          tft.setTextColor(ILI9340_YELLOW);
          tft.fillCircle(255, 60, 60, ILI9340_YELLOW);
          tft.setTextSize(9);
          tft.println("RIGHT");
}
void Tft_Fog ()
{
          tft.fillScreen(ILI9340_BLACK);
          delay(tftDelay);
          tft.setCursor(50, 20);
          tft.setTextColor(ILI9340_RED);
          tft.fillCircle(150, 170, 55, ILI9340_RED);
          tft.setTextSize(11);
          tft.println("FOG");
}
void Tft_Convoy ()
{
          tft.fillScreen(ILI9340_BLACK);
          delay(tftDelay);
          tft.setCursor(40, 40);
          tft.setTextColor(ILI9340_RED);
          tft.fillCircle(150, 170, 55, ILI9340_WHITE);
          tft.setTextSize(7);
          tft.println("CONVOY");
}
void Tft_End ()
{
          tft.fillScreen(ILI9340_BLACK);
          delay(tftDelay);
          tft.setCursor(40, 40);
          tft.setTextColor(ILI9340_RED);
          tft.setTextSize(8);
          tft.println("END");
          tft.setCursor(40, 150);
          tft.println("TEST");
}
void Tft_Old_Test ()
{
          testFillScreen();
          tft.fillScreen(ILI9340_BLACK);
          tft.setCursor(30, 90);
          tft.setTextColor(ILI9340_GREEN);  
          tft.setTextSize(5);
          tft.println("Old Style");
          tft.setCursor(30, 150);
          tft.println("Selected");
          delay(4000);
}
void Tft_New_Test ()
{
          testFillScreen();
          tft.fillScreen(ILI9340_BLACK);
          tft.setCursor(30, 90);
          tft.setTextColor(ILI9340_GREEN);  
          tft.setTextSize(5);
          tft.println("New Style");
          tft.setCursor(30, 150);
          tft.println("Selected");
          delay(4000);
}

unsigned long testFillScreen() {
 unsigned long start = micros();
 tft.fillScreen(ILI9340_BLACK);
 tft.fillScreen(ILI9340_RED);
 tft.fillScreen(ILI9340_GREEN);
 tft.fillScreen(ILI9340_BLUE);
 tft.fillScreen(ILI9340_BLACK);
 return micros() - start;
}

unsigned long testTriangles() {
 unsigned long start;
 int           n, i, cx = tft.width()  / 2 - 1,
 cy = tft.height() / 2 - 1;

 tft.fillScreen(ILI9340_BLACK);
 n     = min(cx, cy);
 start = micros();
 for(i=0; i<n; i+=5) {
   tft.drawTriangle(
   cx    , cy - i, // peak
   cx - i, cy + i, // bottom left
   cx + i, cy + i, // bottom right
   tft.Color565(0, 0, i));
  }
  return micros() - start;
}

unsigned long testRoundRects() {
 unsigned long start;
 int           w, i, i2,
 cx = tft.width()  / 2 - 1,
 cy = tft.height() / 2 - 1;

 tft.fillScreen(ILI9340_BLACK);
 w     = min(tft.width(), tft.height());
 start = micros();
 for(i=0; i<w; i+=6) {
   i2 = i / 2;
   tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.Color565(i, 0, 0));
 }

 return micros() - start;
}

unsigned long testFilledRoundRects() {
 unsigned long start;
 int           i, i2,
 cx = tft.width()  / 2 - 1,
 cy = tft.height() / 2 - 1;

 tft.fillScreen(ILI9340_BLACK);
 start = micros();
 for(i=min(tft.width(), tft.height()); i>20; i-=6) {
   i2 = i / 2;
   tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.Color565(0, i, 0));
 }

 return micros() - start;
}

You can pass arguments to functions:

enum sides {leftSide, rightSide};

void Tft_Side (enum sides side) {
  if (side == leftSide) {
    // Do left side stuff
  } else if (side == rightSide) {
    // Do right side stuff
  }
}

"enum" is just a way to associates a list of different names with different constants. In this case "leftSide" is 0 and "rightSide" is 1.

They are called "functions", not "voids"

When you see void myFunc() { it means that the function does not return a result.

The Thread planning and implementing a program uses functions and may be worth looking at.

...R

Thanks guys. I'm doing some more research and getting more ideas.
Thanks again.