Hi All,
First post so please be gentle!
I have an Arduino Nano that I'm trying to code like a traffic advisor light bar (basically, a light bar with multiple flash modes)
I know the Arduino won't be able to drive the light bar directly, the outputs will be used to drive transistors in the finished item (for personal use, won't be sold) but I'm using some 5mm green LED's just to see the pin state.
I would like it to change patterns using a mode button and to remember the state it was last in when powered on again.
I may have cut out too much of the code below, but it's currently 230 lines long!
If you think I've cut out too much, let me know and I'll put up the full, unedited code.
Note: I've tried both EEPROM.write & EEPROM.set, both have the same results.
#include <EEPROM.h>
const int LED1 = 9;
const int LED2 = 10;
const int LED3 = 11;
const int LED4 = 12;
const int Wait = 250;
const int buttonPin = 6;
int ButtonPushCount = EEPROM.read(1); // I will be using address 1 of the EEPROM for mode memory & selection.
int ButtonState = 0; // Button State
int LastButtonState = 0; // Last Button State
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
void loop() {
ButtonState = digitalRead(buttonPin);
if (ButtonState != LastButtonState) {
if (ButtonState == HIGH) {
ButtonPushCount++;
}
delay(50);
LastButtonState = ButtonState;
if (ButtonPushCount >= 4) {
ButtonPushCount = 0;
}
}
if (EEPROM.read(1) == 0) { // sample flash pattern below. the 2 consecutive delays are because other patterns need half the delay and I didn't see the point in declaring 2 constants.
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
delay(Wait);
delay(Wait);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
delay(Wait);
delay(Wait);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
ButtonState = digitalRead(buttonPin);
if (ButtonState != LastButtonState) {
if (ButtonState == HIGH) {
ButtonPushCount++;
EEPROM.put(1, ButtonPushCount);
}
delay(50);
LastButtonState = ButtonState;
if (ButtonPushCount >= 4) {
ButtonPushCount = 0;
}
}
if (EEPROM.read(1) == 1) {
// same code as above, different flash pattern
}
if (EEPROM.read(1) == 2) {
// same code as above, different flash pattern
}
if (EEPROM.read(1) == 3) {
// same code as above, different flash pattern
}
}
}
As I'm trying to troubleshoot the code myself at the moment, there is a bunch of serial.println that I have removed from this code, but what it has told me is that it is writing to, and reading from the EEPROM fine, but at any time that the number stored in the EEPROM isn't a 0, it has no code to run, so it just keeps reading the EEPROM and waiting for a button press to change the EEPROM value back to 0, where it will then run the first flash pattern again.
buttonPin is pulled to ground via a 220ohm resistor (the only value I have to hand) and I'm using the built in 3.3v output as the + to pull the pin high when pressed via a momentary switch.