Hello!
Its morgofryxx here and well, guys i hate to admit it, but Im stuck. In fact I have been stuck on this segment of code for almost 3 weeks now with no major breakthrough. I am trying to create a program that will enable a multi click function for a button turning on an RGB LED (which I got from adafruit) and then after a delay of a few seconds will turn it off. Then after the RGB LED is turned off, a delay of a few more seconds follows after which a standard led is turned on for a bit and then off. As soon as I can receive some guidance here the better. This is really important since it is for school and I have a deadline... Please respond ASAP!!
I used the tutorial on statechange detection from the arduino website and followed a guide on rgb leds that I found on a different website... Anyway...
The code that I have been using for the program is as follows (without the 2 lines of ////// obviously, they are just there to separate the description from code lol) :
///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
const int buttonPin = A1; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// the pin that the LED is attached to
// Variables will change:
int PushCounter = 0;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int redPin = 6;
int greenPin = 5;
int bluePin = 7;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(motorPin, 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 (buttonState == HIGH) {
PushCounter++;
Serial.println("on");
Serial.print("number of pushes: ");
Serial.println(PushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
if (PushCounter == 1) {
setColor(255,0,0);
delay(2000);
setColor(0,0,0);
delay(2000);
digitalWrite(motorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
}
else if (PushCounter == 2) {
setColor(0,255,0);
delay(2000);
setColor(0,0,0);
delay(2000);
digitalWrite(motorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
}
else if (PushCounter == 3) {
setColor(0,80,8);
delay(2000);
setColor(0,0,0);
delay(2000);
digitalWrite(motorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
}
else {
setColor(0,0,0);
}
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}