Here's my current code that appears to work as I expect and loops round all the patterns.
Just need to work out the most efficient way of showing the light patterns.
Ben
/*
Debounce
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 5; // the number of the pushbutton pin
const int flPin = 0; // Front left strip
const int frPin = 1; // Front right strip
const int rlPin = 2; // Rear left strip
const int rrPin = 3; // Rear right strip
const int tailPin = 4; // Tail strip
const int pattern_num = 3; // Number of light patterns we have defined
// Variables will change:
//int FL = LOW; // the current state of the output pin
//int FR = LOW; // the current state of the output pin
//int RL = LOW; // the current state of the output pin
//int RR = LOW; // the current state of the output pin
//int TAIL = LOW; // the current state of the output pin
int ledState = LOW; //initial state of led's
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int pattern = 0; // tracks which pattern we're on
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(flPin, OUTPUT);
pinMode(frPin, OUTPUT);
pinMode(rlPin, OUTPUT);
pinMode(rrPin, OUTPUT);
pinMode(tailPin, OUTPUT);
// set initial LED state
digitalWrite(flPin, ledState);
digitalWrite(frPin, ledState);
digitalWrite(rlPin, ledState);
digitalWrite(rrPin, ledState);
digitalWrite(tailPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only increment the pattern counter if the new button state is HIGH and the pattern number is less than the number of patterns
if (buttonState == HIGH && pattern < (pattern_num + 1)) {
pattern++;
if (pattern == 4) {
pattern = 0;
}
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
// now run some light shows
switch(pattern){
case 0:
// turn all LED's OFF
digitalWrite(flPin, LOW);
digitalWrite(frPin, LOW);
digitalWrite(rlPin, LOW);
break;
case 1:
digitalWrite(flPin, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(frPin, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(rlPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait half a second and turn LED off
digitalWrite(flPin, LOW);
digitalWrite(frPin, LOW);
digitalWrite(rlPin, LOW);
break;
case 2:
digitalWrite(flPin, HIGH); // turn the LED on by making the voltage HIGH
delay(200);
digitalWrite(flPin, LOW); // turn the LED off by making the voltage LOW
digitalWrite(frPin, HIGH); // turn the LED on by making the voltage HIGH
delay(200);
digitalWrite(frPin, LOW); // turn the LED off by making the voltage LOW
digitalWrite(rlPin, HIGH); // turn the LED on by making the voltage HIGH
delay(200);
digitalWrite(rlPin, LOW);
break;
case 3:
digitalWrite(rlPin, HIGH); // turn the LED on by making the voltage HIGH
delay(200);
digitalWrite(rlPin, LOW); // turn the LED off by making the voltage LOW
digitalWrite(frPin, HIGH); // turn the LED on by making the voltage HIGH
delay(200);
digitalWrite(frPin, LOW); // turn the LED off by making the voltage LOW
digitalWrite(flPin, HIGH); // turn the LED on by making the voltage HIGH
delay(200);
digitalWrite(flPin, LOW);
break;
default:
digitalWrite(flPin, LOW);
digitalWrite(frPin, HIGH);
digitalWrite(rlPin, LOW);
}
}