Flashing 2 LEDS independent timing when button is pressed

Yep I'm a noob, sorry.
The project is to display a car racing board game I invented. I have imbedded some LEDs into the board.
I have a Veroboard with 8 momentary-on button switches mounted. 6 of these require no Arduino control.
The other 2 are to have separate duties, which I intend to control with Arduino Nano (ATMEGA328), [I also have available Freetronics "Eleven" R3 - Arduino Uno compatible].

  1. I am attempting to "simulate" a two vehicle collision by illuminating each car with a flashing LED, independently of each other. I have found a sketch that works well by using mills() to control LED1 and LED2. My challenge is to only run the code while the button is being pressed (and perhaps for about 2 seconds after the button is released).
  2. Using the same Nano board, to operate 6 individual LEDs from a separate button switch as a to-and-fro "LED chaser" like KIT in the Knight Rider TV series.

My code is all over the place, as I am trying out different ideas, and attempting to work out what is going on, how to use routines etc, and how to attend to error codes like variable not defined in this section!!!
This is what I have got so far ...

#define LED_1_PIN 12
#define LED_2_PIN 11


//LED 1
unsigned long previousTimeLED1Blink = millis();
unsigned long LED1BlinkDelay = 200;
byte LED1State = LOW;

//LED 2
unsigned long previousTimeLED2Blink = millis();
unsigned long LED2BlinkDelay = 200;
byte LED2State = HIGH;

void setup() {
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  int switchState1;
}

void loop() {
 unsigned long timeNow = millis();
  int switchState1 = digitalRead(2);

if(switchState1 == HIGH){
// High means switch is open hence write to 13 is set low (ie off):
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }

 if(switchState1 == LOW) {

  
  digitalWrite(LED_1_PIN, LED1State);
  digitalWrite(LED_2_PIN, LED2State);
 }

 
}
void toggleLed1() {
  unsigned long timeNow = millis();
if (timeNow - previousTimeLED1Blink > LED1BlinkDelay) {
  previousTimeLED1Blink += LED1BlinkDelay;
  if (LED1State == HIGH) {
    LED1State = LOW;
  }
  else {
    LED1State = HIGH;
  }
}
}

void toggleLed2() {
  unsigned long timeNow;
if (timeNow - previousTimeLED2Blink > LED2BlinkDelay) {
  previousTimeLED2Blink += LED2BlinkDelay;
  if (LED2State == HIGH) {
    LED2State = LOW;
  }
  else {
    LED2State = HIGH;
  }
}
}

type or paste code here

Some suggestions would be welcome

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

the state machine would look something like this

I'd suggest you use a button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2 to handle the button press.

2 Likes

I think you are trying to toggle LEDxState... to do that, you need to call your functions... so the above would look like this...

  if (switchState1 == LOW) {
    toggleLed1(); // <-- call the toggleLed2() function to change the LED1State
    toggleLed2(); // <-- call the toggleLed2() function to change the LED2State
    digitalWrite(LED_1_PIN, LED1State);
    digitalWrite(LED_2_PIN, LED2State);
  }

You still need some work on the button reading (see post #2), and the toggleLedx() functions.

1 Like

Thanks J-M-F Jackson, I'll check it out. Your diagram is helpful.
Also xfpd Seeing your code just makes it so much easier to internalise the syntax as well as the logic.

As a noob I have spent hours and hours already searching google, this forum, project books and our friendly retailer to try and get started.

This is my first sketch and in creating it I have already learnt so much ... but I really appreciate the willingness of this community to help others, and especially to those who have replied to my cry for help from the wilderness of cyberspace.

Other duties demand my attention now (damn!), but I'll be back into getting this runningRight( = 100%) :slight_smile: tonight Melb summer time (GMT +11)

it seems you have already got your flashing LEDs.

Here I have a non blocking KITT like scanner:

http://werner.rothschopf.net/microcontroller/202109_millis_larson_scanner_en.htm

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.