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].
- 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).
- 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
