First time trying code dose any of this look right?

I was tasked by a friend to make a lighting system for atv drag races.

i have eventually decided that i needed an arduino to be able to do what im looking for and have never done any coding at all.

i have watched a few basic toutorials to get some bacics and believe thisd may get me somewhere.

i do not yet have an arduino to try the code out on and figured i could throw it on here and get some sudgestions.

function--

i want there to be one operator with one on off switch.

when the switch in the off position all lights will be off except the yellow staging light.

once the switch is turned on the yellow goes out and the green start light is on

if the left or right lane triggers a photoelectric switch at the start line before the light is green it lights a corresponding red light for the lane.

the first lane to the finish lights a corresponding orange light to show who crossed the finish line first using a photoelectric sensor giving voltage as they pass it

once the race is over the switch is turned off and all lights are turned off except the yellow staging light.

any and all input is appriciated. like i said i might have watches 2 hours of youtube videos so this may be pretty bad. just had a feeling i may be close.

thanks

You can run simulations on Tinkercad or Wokwi.

It's usual to post code as the actual text, so readers can load it into the IDE to work on, not as a pic.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

int trigger = 1;
int yellow = 2;
int green = 3;
int redL = 4;
int redR = 5;              //  pin numbers are random at this point
int orangeL = 6;
int orangeR = 7;
int SL1 = 8;               // all outputs will be connected to relays to control lights
int SL2 = 9;
int SR1 = 10;
int SR2 = 11;



void setup() {
  pinMode(trigger, INPUT);   //  on/off switch
  pinMode(SL1, INPUT);       //  first photoelectric sensir left lane
  pinMode(SL2, INPUT);       //  second photoelectric sensir left lane
  pinMode(SR1, INPUT);       //  first photoelectric sensir right lane
  pinMode(SR2, INPUT);       //  second photoelectric sensir right lane

  pinMode(yellow, OUTPUT);   //  yellow staging light
  pinMode(green, OUTPUT);    //  green start light
  pinMode(redL, OUTPUT);     // left lane advanced start
  pinMode(redR, OUTPUT);     //  right lane advanced start
  pinMode(orangeL, OUTPUT);  //  left lane finished first
  pinMode(orangeR, OUTPUT);  //  right lane finished first



}

void loop()

{
  if (digitalRead(trigger) == LOW) {
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(redL, LOW);                        // switch off - all lights except staging light off
    digitalWrite(redR, LOW);
    digitalWrite(orangeL, LOW);
    digitalWrite(orangeR, LOW);
  }

  {
    if (digitalRead(trigger) == HIGH) {
      digitalWrite(yellow, LOW);                      //  switch on - staging light off and green start light on
      digitalWrite(green, HIGH);
    }
  }


  {
    if (digitalRead(SL1) == HIGH && yellow == HIGH) {
      digitalWrite(redL, HIGH);                               //  activation for left lane early start
      delay(10000);
    }
    else {
      digitalWrite(redL, LOW);
    }

    {
      if (digitalRead(SR1) == HIGH && yellow == HIGH) {
        digitalWrite(redR, HIGH);                              //  activation for left lane early start
        delay(10000);
      }
      else {
        digitalWrite(redR, LOW);
      }

      {
        if (digitalRead(SL2) == HIGH && (green == HIGH) && (orangeR == LOW) {
        digitalWrite(orangeL, HIGH);                                           //  win light for left lane
          delay(8000);
        }
        else {
          digitalWrite(orangeL, LOW);
        }

        {
          if (digitalRead(SR2) == HIGH && (green == HIGH) && (orangeL == LOW) {
          digitalWrite(orangeR, HIGH);                                            //  win light for right lane
            delay(8000);
          }
          else {
            digitalWrite(orangeR, LOW);
          }

        }

If this is for full sized man toys, you are going to have to do some due diligence in the hardware area.

Suggest you write up a story board of some sort so as to get every possibility written down and accounted for.

Suggest you start with a preliminary schematic of all the elements needed.

  • A laser beam light breaking would be a first recommendation.

  • Switches that are normally closed would be much less susceptible to electrical noise spikes than normally open switches.

  • You need to decide how Lights are to be powered.

  • Cabling needs to compensate for electrical noise causing problems.

As for your software, do not use delay( ) doing so locks up all your code for that delay period of time.

Do not use Input #1 as it is used for communications.

Study what State Machine techniques are all about, write your sketch as a State machine.

Switches should be monitored for a change in state, not for a switch level.

i always have to test the code to make sure it works properly. i have a harder time with visual inspection of the logic

consider

#undef MyHW
#ifdef MyHW
const byte PinYel  = 13;
const byte PinGrn  = 12;
const byte PinRedL = 11;
const byte PinRedR =  9;
const byte PinOraL = 10;
const byte PinOraR =  8;

const byte PinSeq  = A1;
const byte PinSL1  = A2;
const byte PinSL2  = A2;
const byte PinSR1  = A3;
const byte PinSR2  = A3;
#else
const byte PinYel  =  2;
const byte PinGrn  =  3;
const byte PinRedL =  4;
const byte PinRedR =  5;
const byte PinOraL =  6;
const byte PinOraR =  7;

const byte PinSeq  =  1;
const byte PinSL1  =  8;
const byte PinSL2  =  9;
const byte PinSR1  = 10;
const byte PinSR2  = 11;
#endif


const byte PinLeds [] = { PinYel, PinGrn, PinRedL, PinRedR, PinOraL, PinOraR };
const byte PinSens [] = { PinSeq, PinSL1, PinSL2, PinSR1, PinSR2 };

enum { Off = HIGH, On = LOW };

byte butState;

const char *stStr [] = { "None", "Stage", "Go", "Finish" };
enum { None, Stage, Go, Finish };
int state;

// -----------------------------------------------------------------------------
void
advState (void)
{
    if (Finish < ++state)
        state = None;
    Serial.println (stStr [state]);
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    switch (state)  {
    case None:
        for (unsigned n = 0; n < sizeof(PinLeds); n++)
            digitalWrite (PinLeds [n], Off);
        break;

    case Stage:
        digitalWrite (PinYel, On);

        if (LOW == digitalRead (PinSL1)) {
            digitalWrite (PinRedL, On);
            Serial.println (" penalty lane left");
        }
        if (LOW == digitalRead (PinSR1))  {
            digitalWrite (PinRedR, On);
            Serial.println (" penalty lane right");
        }
        break;

    case Go:
        digitalWrite (PinYel, Off);
        digitalWrite (PinGrn, On);

        if (LOW == digitalRead (PinSL2) && LOW != digitalRead (PinRedL))  {
            digitalWrite (PinOraL, On);
            advState ();
            Serial.println (" winner lane left");
        }
        if (LOW == digitalRead (PinSR2) && LOW != digitalRead (PinRedR))  {
            digitalWrite (PinOraR, On);
            advState ();
            Serial.println (" winner lane right");
        }
        break;

    case Finish:
        digitalWrite (PinGrn, Off);
        break;
    }

    byte but = digitalRead (PinSeq);
    if (butState != but)  {
        butState = but;
        delay (10);         // debounce

        if (LOW == but)
            advState ();
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(PinSens); n++)
        pinMode (PinSens [n], INPUT_PULLUP);

    butState = digitalRead (PinSeq);

    for (unsigned n = 0; n < sizeof(PinLeds); n++)  {
        digitalWrite (PinLeds [n], Off);
        pinMode      (PinLeds [n], OUTPUT);
    }
}

These 'if' statements won't work because you are comparing constants (2, 3, 6, and 7) to the constant 'HIGH" (1) and none of them will ever match. You can, in most cases, read the state of an output pin with digitalRead(pin) so you can change yellow == HIGH to digitalRead(yellow) == HIGH. Another solution is to keep a variable with the last state you wrote:

   digitalWrite(yellow, HIGH);
   yellowState = HIGH;
.
.
.
  if (yellowState == HIGH)

With not much more to learn you can test your code and circuitry here, as mentioned by @dead_parrot :


 //  pin numbers are random at this point

You shoukd leave pins 0 and 1 unused so the serial monitor can be employed to assist in confirming the flow of you program and finding errors.

Serial.read() functions can be handy as well.

a7

Thank you for the info. I made a few quick references as to what I'm trying to achieve.



(upload://xOeiGYJrTsy9xkO7ebo73pv3qnb.jpeg)
512Cgl9DMLL._AC_SY300_SX300_QL70_ML2
61v9-L6carL.AC_SY450

the wiring diagram that i drew up was done before i realized that you could buy specific relay modules and i have one ordered.

also i am trying to decide what style of beam sensor would work best.
reflector style means all wiring will be able to be placed between racing lanes but may be less accurate and harder to align (this is intended to be a mobile set up)
however if i need to use the transmitter and receiver style i will need to power the two transmitters on the outside of the left lane and right lane independently of the main system.