Starting lights for slot cars

Hi all. I am pretty new to Arduino. I have designed a control panel for my slot car set. One component is the F1 style starting lights. I have 5 red lights running vertically and a green led separate to these. The lights are working ok but I want to be able to start them with a button and only run once. Also would like to add a basic buzzer sound as each light goes out. Please help.
Thank you.

`1  // variables
int RED1= 2;
int RED2 = 3;
int RED3 = 4;
int RED4 = 5;
int RED5 = 6;
int GREEN = 7;


int DELAY_RED1 = 1000;
int DELAY_RED2 = 1000;
int DELAY_RED3 = 1000;
int DELAY_RED4 = 1000;
int DELAY_RED5 = 1000;
int DELAY_GREEN = 1000;

// basic functions
void setup()
{
  pinMode(RED1, OUTPUT);
  pinMode(RED2, OUTPUT);
  pinMode(RED3, OUTPUT);
  pinMode(RED4, OUTPUT);
  pinMode(RED5, OUTPUT);
  pinMode(GREEN, OUTPUT);
  
 

}
void loop()
{
  red1_light();
  delay(DELAY_RED1);
  red2_light();
  delay(DELAY_RED2);
  red3_light();
  delay(DELAY_RED3);
  red4_light();
  delay(DELAY_RED4);
  red5_light();
  delay(DELAY_RED5);
  green_light();
  delay(DELAY_GREEN);


}
void red1_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);
  
}

void red2_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);
}

void red3_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);
}

void red4_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);

}

void red5_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);

  }

void green_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, HIGH);
 
 
 
 

}




That's simple enough. What have you tried so far?

Any code in setup() only runs once. Move the code you have in loop() into setup().

1 Like

Connect a push button between say pin 8 and GND.
Add:

int startButton = 8;

Set the 'startButton' pin to be an input with pullup resistor by adding the following to setup().

pinMode(startButton, INPUT_PULLUP);

and then add

 while (digitalRead(startButton)) {
    // wait until startButton is pressed
  }

at the beginning of loop().

That line just carries out what is inside the curly brackets (i.e. nothing) until the 'startButton' pin goes low (button pressed).

Then if you then add something at the end of loop() to turn the green LED off after a suitable delay, everything will be ready to press the startButton again for the next race.

1 Like

Hi John. Thanks for you help. All up and running with a buzzer as well. The code is probably too complex but i got it to work. Only thing I would like is to turn off the green light after 2 seconds. Tried a few things but no luck. Cheers.

int startButton = 8;

const int buzzer = 9; //buzzer to arduino pin 9


// variables
int RED1= 2;
int RED2 = 3;
int RED3 = 4;
int RED4 = 5;
int RED5 = 6;
int GREEN = 7;


int DELAY_RED1 = 100;
int DELAY_RED2 = 100;
int DELAY_RED3 = 100;
int DELAY_RED4 = 100;
int DELAY_RED5 = 100;
int DELAY_GREEN = 100;

// basic functions
void setup()
{

  pinMode(startButton, INPUT_PULLUP);

  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output

   
  


  pinMode(RED1, OUTPUT);
  pinMode(RED2, OUTPUT);
  pinMode(RED3, OUTPUT);
  pinMode(RED4, OUTPUT);
  pinMode(RED5, OUTPUT);
  pinMode(GREEN, OUTPUT);
  
 



  while (digitalRead(startButton)) {
    // wait until startButton is pressed
  }

  red1_light();
  delay(DELAY_RED1);

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec


  red2_light();
  delay(DELAY_RED2);

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec


  red3_light();
  delay(DELAY_RED3);

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec


  red4_light();
  delay(DELAY_RED4);
  
  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec
  
  
  red5_light();
  delay(DELAY_RED5);


  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);        // ...for 1sec

    green_light();
  delay(DELAY_GREEN);

 

  
  

}
void red1_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);
  
}

void red2_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);
}

void red3_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);
}

void red4_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);

}

void red5_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);

  }

void green_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, HIGH);


 
 }
void loop()

{

}



int startButton = 8;

const int buzzer = 9; //buzzer to arduino pin 9


// variables
int RED1= 2;
int RED2 = 3;
int RED3 = 4;
int RED4 = 5;
int RED5 = 6;
int GREEN = 7;


int DELAY_RED1 = 100;
int DELAY_RED2 = 100;
int DELAY_RED3 = 100;
int DELAY_RED4 = 100;
int DELAY_RED5 = 100;
int DELAY_GREEN = 100;

// basic functions
void setup()
{

  pinMode(startButton, INPUT_PULLUP);

  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output

   
  


  pinMode(RED1, OUTPUT);
  pinMode(RED2, OUTPUT);
  pinMode(RED3, OUTPUT);
  pinMode(RED4, OUTPUT);
  pinMode(RED5, OUTPUT);
  pinMode(GREEN, OUTPUT);
  
 



  while (digitalRead(startButton)) {
    // wait until startButton is pressed
  }

  red1_light();
  delay(DELAY_RED1);

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec


  red2_light();
  delay(DELAY_RED2);

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec


  red3_light();
  delay(DELAY_RED3);

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec


  red4_light();
  delay(DELAY_RED4);
  
  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec
  
  
  red5_light();
  delay(DELAY_RED5);


  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);        // ...for 1sec

    green_light();
  delay(DELAY_GREEN);

 

  
  

}
void red1_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);
  
}

void red2_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);
}

void red3_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);
}

void red4_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, LOW);

}

void red5_light()
{
  digitalWrite(RED1, HIGH);
  digitalWrite(RED2, HIGH);
  digitalWrite(RED3, HIGH);
  digitalWrite(RED4, HIGH);
  digitalWrite(RED5, HIGH);
  digitalWrite(GREEN, LOW);

  }

void green_light()
{
  digitalWrite(RED1, LOW);
  digitalWrite(RED2, LOW);
  digitalWrite(RED3, LOW);
  digitalWrite(RED4, LOW);
  digitalWrite(RED5, LOW);
  digitalWrite(GREEN, HIGH);


 
 }
void loop()

{

}




Hello,

Yes your code is too much complicated for the 'simple' task. I'll help you minimize it if you want to.

Just saying, don't know if you watch a lot F1 but for the start, there is no green light. The lights just go ON once at a time, and when they all turn off it means go, no green light

Also, there is certain level of randomness on the delay.

Warning: You pasted twice the code in your precedent post. That is confusing xD

Here you set the light on and then wait for the delay to end. But you're doing nothing after, whereas you want the green LED to turn off. You just have to add

digitalWrite(GREEN, LOW);

And change DELAY_GREEN to the good value

Not sure about it. I mean, there is a random delay for all the LEDs to turn off, but I think all the LEDs are turning ON at a known rate no?

Hi Anthony. Yes I would love it if you could recommend some simpler code. The green light is mainly for my grandkids. The slot car track isn’t f1 more of a mountain rally track. I watch FI so I know about the random timing they use. For my layout it’s not necessary. Just need now to turn off the green after 2 seconds. I’ll try what you recommended. Sorry about the double up, I didn’t notice it.
Cheers.

Yes, I think so. Delay is random for turn off.

Just asking,

Are you familiar with array? And array of more than 1 dimension?

I know of them but as I’m quite new to coding I’d need some help to code it. I’m just happy that at least it works as I’d hoped. Thanks.

Blockquote

Yes, at least 5x too complex! There is much repetition.

It has a lot of commands to turn LEDs on that are already on, or off that are already off.

int RED1= 2;
int RED2 = 3;
int RED3 = 4;
int RED4 = 5;
int RED5 = 6;

Since your LEDs are on adjacent pins, you could use for-loops to avoid repeating code, for example:

  for (byte pin = RED1; pin <= RED5; pin++) {
    pinMode(pin, OUTPUT);
  }

You also missed a trick with the tone() function.

tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(250);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);

could be

  tone(buzzer, 2000, 250); // Send 2KHz sound signal for 1/4 sec...
  delay(750);

Here you go:

const int startButton = 8;
const int buzzer      = 9;  // Buzzer pin 9

// LED + Delay in an array [LED, delai]
const int ledSequence[][2] = {
  { 2, 750 },  // RED1
  { 3, 750 },  // RED2
  { 4, 750 },  // RED3
  { 5, 750 },  // RED4
  { 6, 750 },  // RED5
  { 7, 2000 }  // GREEN
};

const int numLEDs    = sizeof(ledSequence) / sizeof(ledSequence[0]);
const int DELAY_BUZZ = 250;

void setup()
{
  pinMode(startButton, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);

  for (int i = 0; i < numLEDs; i++)
  {
    pinMode(ledSequence[i][0], OUTPUT);
    digitalWrite(ledSequence[i][0], LOW);
  }
}

void loop()
{
  static bool Start_Done = false;

  if (!digitalRead(startButton) && !Start_Done)
  {
    Initiate_Start_Procedure();
    Start_Done = true;
  }
}

void Initiate_Start_Procedure(void)
{
  for (int i = 0; i < numLEDs; i++)
  {
    int pin       = ledSequence[i][0];
    int delayTime = ledSequence[i][1];

    digitalWrite(pin, HIGH);

    if (i == numLEDs - 1)
    {
      // Last LED (GREEN), turn off the red ones
      for (int j = 0; j < numLEDs - 1; j++)
      {
        digitalWrite(ledSequence[j][0], LOW);
      }
      tone(buzzer, 2000, 2 * DELAY_BUZZ); // Buzzer longer on the last one
    }
    else
    {
      tone(buzzer, 2000, DELAY_BUZZ);
    }

    delay(delayTime);
  }

  // Turn off everything at the end (basically turns off only the green one but just in case)
  for (int i = 0; i < numLEDs; i++)
  {
    digitalWrite(ledSequence[i][0], LOW);
  }
}

We could do a bit more simple using structures but there is no need here. I didn't test the code, just simulated it in my head. In case anything isn't going well don't hesistate. If you have question please feel free to ask.

Here is a link to the project.

The LEDs work as it should, or at least as I understood it should

I cannot test the buzzer because the sound of my computer doesn't work

Hi Anthony (legend). Your new code works perfectly. One last thing. I would like to be able to press the button to start the sequence again with having to disconnect the power every time.
Cheers.

That's pretty easy. I explain you how it works and let you try to do the necessary to do what you want. This way you'll learn

How did I make it so it works only once:

At the begining, the procedure hasn't been done so I set Start_Done to false.

A static variable is a variable that keep its state from a call to another. It's also a variable that gets the init value only once. So here, Start_Done is set to false only the first time we enter in the loop() function. Then it keeps its value.

Then we check if we click on the button with !digitalRead(startButton). If we click on this one, we also check if the procedure hasn't already been done with: && !Start_Done

If this is the case (button pressed and Start_Done = false) then we initiate the procedure and after this set Start_Done = true;

Next time we'll click on the button, we won't enter in the if since Start_Done is true, so !Start_Done is false, therefore if (!digitalRead(startButton) && !Start_Done) is false.

So what you have to do is find a way to do so each time you press the button, the IF statement is true.
Don't overthink, it's really easy

Feel free to ask for more help

Now I'm lost. Managed to take the buzzer off the green light but can't think through this part of the code to have button start the sequence.

This needs to become this:

  if (!digitalRead(startButton))
  {
    Initiate_Start_Procedure();
  }

Thanks Anthony. I appreciate you helping me to work out the solution. I'll need to study up a bit more. Everything works perfectly. Next project is to set up lap timers with infra red sensors and 7 segment cathodes. I've done some tutorials so stand by. Haha. Cheers.