I am relativity new to the world of digital electronics. I am trying to learn as fast as possible because I am about to be teaching it to High School Kids in a little more than a month. I am working on a project to use a push button to switch between multiple RGB LED Strip programs. I have pulled the majority of the code from different places as you will see in the notes. I had the Edge detection working great with a simple LED hooked up to pin 13, I was able to get it to flash at different rates, stay on, and then turn off. I can also get the RGB codes to work on their own. When I inserted the RGB code into the Edge Detection program it gets stuck in the first program. The button detection program does not recognize the push button after the first press. I have been messing with the program for hours now but I can not figure out what I am doing wrong. Please help me determine the problem.
Thanks,
Brian
/*
Using a push button to switch between 3 differnt RGB Codes and turning the system off.
This code is compiled from two sources. First the edge detection program was written by Tom Igoe and can be found at http://arduino.cc/en/Tutorial/ButtonStateChange
Secondly, the RGB color code is found at http://www.ladyada.net/products/rgbledstrip/. The Schemetic can be found there as well.
The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)
* 3 STP16NF06 MOSFETs are used.
* MOSFET 1(Green) G-Digital Pin6 D-Green Input on RGB Strip S-Ground
* MOSFET 2(Red) G-Digital Pin5 D-Red Input on RGB Strip S-Ground
* MOSFET 3(Blue) G-Digital Pin3 D-Blue Input on RGB Strip S-Ground
*LED Strip 12v source is attached to the 9v Arduino Supply
*/
// 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
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
int r, g, b;
#define FADESPEED 5 // make this higher to slow down
// 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(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}
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
// 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");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
//off
if (buttonPushCounter == 0) {
digitalWrite(ledPin, LOW);
}
//color fade
if (buttonPushCounter==1){
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
// fade from yellow to green
for (r = 255; r > 0; r--) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from teal to blue
for (g = 255; g > 0; g--) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
//Pink to Orange Strobe
if (buttonPushCounter==2){
{
//pink
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 80);
analogWrite(GREENPIN,15);
delay(250);
//orange
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN,35);
delay(250);
}
}
}
if (buttonPushCounter==3){
//white-on
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPIN,255);
delay(100);
//off
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN,0);
delay(100);
}
if (buttonPushCounter==4){
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN,0);;
buttonPushCounter = 0;
}
}