I'm making flashers for my kid's wagon for our walks at dusk/night.
I have a RGB LED strip and a white LED strip. I'd like to be able to change the color of the RGB strip via a button press. Eventually the idea is to make everything a RGB LED so I can make the flashers any color combination. I'm using an Uno to control the LED's and a 12v battery for power.
I can't figure out how to get the button presses to assign a color for the flashes.
Below are the two programs I'm trying to Frankenstein together. Both have been slightly modified by me already.
Is this possible or should I be going a different route?
Flashing
// Timing suquences for the LED's in milliseconds
// First value is on time, second value is off time,
// third value on time and so on (up to 100 values)
// One row for each LED
const int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned int led_timing[][100] = {
{
//white LED
50,50,50,50,50,350, //600 ms
50,50,50,50,50,350, //600 ms
50,150,50,150,50,150,50,150, //80o ms
50,150,50,150,50,150,50,150, //800 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
},
{
//red LED
0,300,50,50,50,50,50,350, //900 ms
50,50,50,50,50,50, //300 ms
0,100,50,150,50,150,50,150,50,150, //900 ms
50,150,50,150,50,150,50,50, //700 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
},
{
//green LED
0,300,50,50,50,50,50,350, //900 ms
50,50,50,50,50,50, //300 ms
0,100,50,150,50,150,50,150,50,150, //900 ms
50,150,50,150,50,150,50,50, //700 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
},
{
//blue LED
0,300,50,50,50,50,50,350, //900 ms
50,50,50,50,50,50, //300 ms
0,100,50,150,50,150,50,150,50,150, //900 ms
50,150,50,150,50,150,50,50, //700 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
50,50,50,200, //350 ms
},
};
// The pins the LED's are connected to
byte led_pins[] = {3,9,10,11}; // pin 3 white
// pin 9 red
// pin 10 green
// pin 11 blue
// Keep track of timing sequence
// Array size taken from led_pins
unsigned long last_change[sizeof(led_pins)/sizeof(led_pins[0])];
byte timing_i[sizeof(led_pins)/sizeof(led_pins[0])];
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
// Initialize LED's as output
for (byte i = 0; i < sizeof(led_pins)/sizeof(led_pins[0]); i++)
{
pinMode(led_pins[i], OUTPUT);
digitalWrite(led_pins[i], HIGH);
}
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// Current timestamp
unsigned long now = millis();
// Keep track of sequence for each LED
for (byte i = 0; i < sizeof(led_pins)/sizeof(led_pins[0]); i++)
{
if (now - last_change[i] >= led_timing[i][timing_i[i]])
{
digitalWrite(led_pins[i], !digitalRead(led_pins[i]));
timing_i[i]++;
// Start over at the end of timing sequence
timing_i[i] %= sizeof(led_timing[i])/sizeof(led_timing[i][0]);
last_change[i] = now;
}
}
}
Button press
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int greenPin = 10; // the pin that the LED is attached to
const int redPin = 9;
const int bluePin = 11;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
/* {255, 127, 0}, // orange
{255, 255, 0}, // yellow
{ 0, 255, 0}, // green
{ 0, 0, 255}, // blue
{127, 0, 255}, // indigo
{255, 0, 255}, // violet
{255, 0, 0} // red
*/
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 8 == 1) {
digitalWrite(redPin, HIGH);
analogWrite(greenPin, 127);
digitalWrite(bluePin, LOW);
}
if (buttonPushCounter % 8 == 2) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite (bluePin, LOW);
}
if (buttonPushCounter % 8 == 3) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
}
if (buttonPushCounter % 8 == 4) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
}
if (buttonPushCounter % 8 == 5) {
analogWrite(redPin, 127);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}
if (buttonPushCounter % 8 == 6) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}
if (buttonPushCounter % 8 == 7) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}