Hey all,
I have some code that runs a LED strip. My problem is one of the "effects" I have is that it turns on different LED's (Making a rainbow)(red on, blue on, red off, green on, etc...). However, the thing I don't like is that it is not being registered that a button is being pushed to switch modes until it reaches the end of the effect....
Is there a piece of code (or a command) that will read in a button push in the middle of an if statement? I know that it's in the middle of an if while in a loop, so it may not be possible and mode switch may only be able to happen at the end of the command. I don't know if there is an on button interrupt?
Mode 8 in the code is the one I'm looking at...mode 7 does the same as well.
Thanks in advance for help!
/*
Not all code is mine!!
Some code by Joshua David --- TechHelpBlog.com
Some code by Arduino.CC
Some code by Adafruit for SetColor;
Some code found for public domain use (author not in comments)
all compliled by Lance == cub526 and free to use!!
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// 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
//variables for our pins
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
//Set Initial Values
int RedVal = 0;
int GreenVal = 0;
int BlueVal = 0;
int fade = 15; // Delay Time
//two colors below are my school colors
//midnight blue
int RedVal1 = 25;
int GreenVal1 = 25;
int BlueVal1 = 112;
//gold
// Colour Value 2
int RedVal2 = 255;
int GreenVal2 = 215;
int BlueVal2 = 0;
int mode = 1; //used for mode slection in certain if's
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonModeChange();
if (buttonPushCounter == 10) { //we've hit the end of the loop, go back to one!
buttonPushCounter = 1;
Serial.println("Change");
}
if (buttonPushCounter == 1) {//red
setColor(255, 0, 0);
Serial.println("1");
}
if (buttonPushCounter == 2) {
setColor(0, 255, 0); // green
Serial.println("2");
}
if (buttonPushCounter == 3) {
setColor(0, 0, 255); // blue
Serial.println("3");
}
if (buttonPushCounter == 4) {
setColor(255, 255, 0); // YELLOW
Serial.println("4");
}
if (buttonPushCounter == 5) { //fade between two colors
if(mode == 1){
if(RedVal < RedVal1){ // If RedVal is less than desired RedVal1
RedVal ++; // increment RedVal
}
else if(RedVal > RedVal1){ // If RedVal is more than desired RedVal1
RedVal --; // decrement RedVal
}
else if(RedVal == RedVal1){ // If RedVal is equal to desired RedVal1
//Do Nothing
}
// Repeated as above for BlueVal
if(BlueVal < BlueVal1){
BlueVal ++;
}
else if(BlueVal > BlueVal1){
BlueVal --;
}
else if(BlueVal == BlueVal1){
//Do Nothing
}
// Repeated as above for GreenVal
if(GreenVal < GreenVal1){
GreenVal ++;
}
else if (GreenVal > GreenVal1){
GreenVal --;
}
else if (GreenVal == GreenVal1){
// Do Nothing
}
// Now we have a new set of values, we write them to the PWM Pins.
analogWrite(redPin, RedVal);
analogWrite(greenPin, GreenVal);
analogWrite(bluePin, BlueVal);
delay(fade); // Implement a bit of delay to slow the change of colour down a bit
if(RedVal == RedVal1 && GreenVal == GreenVal1 && BlueVal == BlueVal1){ // If RedVal & GreenVal & BlueVal are all at the desired values
delay(1500); // Delay to hold this colour for a little while
mode = 2; // Change the mode to the next colour. Exiting this while loop and into the next one
}
}
if (mode == 2){
if(RedVal < RedVal2){ // If RedVal is less than desired RedVal1
RedVal ++; // increment RedVal
}
else if(RedVal > RedVal2){ // If RedVal is more than desired RedVal1
RedVal --; // decrement RedVal
}
else if(RedVal == RedVal2){ // If RedVal is equal to desired RedVal1
//Do Nothing
}
// Repeated as above for BlueVal
if(BlueVal < BlueVal2){
BlueVal ++;
}
else if(BlueVal > BlueVal2){
BlueVal --;
}
else if(BlueVal == BlueVal2){
//Do Nothing
}
// Repeated as above for GreenVal
if(GreenVal < GreenVal2){
GreenVal ++;
}
else if (GreenVal > GreenVal2){
GreenVal --;
}
else if (GreenVal == GreenVal2){
// Do Nothing
}
// Now we have a new set of values, we write them to the PWM Pins.
analogWrite(redPin, RedVal);
analogWrite(greenPin, GreenVal);
analogWrite(bluePin, BlueVal);
delay(fade); // Implement a bit of delay to slow the change of colour down a bit
if(RedVal == RedVal2 && GreenVal == GreenVal2 && BlueVal == BlueVal2){ // If RedVal & GreenVal & BlueVal are all at the desired values
delay(1500); // Delay to hold this colour for a little while
mode = 1; // Change the mode to the next colour. Exiting this while loop and into the next one
}
}
}
if (buttonPushCounter == 6){ // all white
setColor(255,255,255);
Serial.println("6");
mode = 1; //for the previous effect, need to change this back to 1 for future use (we don't know what it was before)
}
if (buttonPushCounter == 7) //fourth of july Effect
{
if (mode == 1){
setColor(255, 0, 0); // red
delay(250);
mode = 2;
}
if (mode ==2)
{
setColor(255, 255, 255); // white
delay(250);
mode = 3;
}
if (mode ==3)
{
setColor(0, 0, 255); // blue
delay(250);
mode = 1;
}
}
if (buttonPushCounter == 8)
{
int r, g, b;
// fade from blue to violet
for (r = 0; r < 255; r++) {
analogWrite(redPin, r);
delay(fade);
}
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(bluePin, b);
delay(fade);
}
// fade from red to yellow
for (g = 0; g < 255; g++) {
analogWrite(greenPin, g);
delay(fade);
}
// fade from yellow to green
for (r = 255; r > 0; r--) {
analogWrite(redPin, r);
delay(fade);
}
// fade from green to teal
for (b = 0; b < 255; b++) {
analogWrite(bluePin, b);
delay(fade);
}
// fade from teal to blue
for (g = 255; g > 0; g--) {
analogWrite(greenPin, g);
delay(fade+3);
}
}
if (buttonPushCounter == 9)
{
setColor(0,0,0); //all off
Serial.println("ALL OFF: 8");
mode =1; //all done for loop, mode = 1 again just in case;
}
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void buttonModeChange ()
{
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
// wend 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
// wend from on to off:
Serial.println("off");
}
}
lastButtonState = buttonState;
}