/* this example is from Getting Started With Arduino
modified to my taste.
so far the LEDs are turning on and off with BUTTON7
and fading with two more BUTTONS up or down */
/* What I want to do is:
change colors with the same two BUTTONS up and down, but i don't know how*/
#define REDPIN 3 // LED pins
#define GREENPIN 5
#define BLUEPIN 6
#define BUTTON 2 brightness-- // the input pins where the
#define BUTTON 4 brightness++ // pushbutton are connected
#define BUTTON 7 on/of
int val = 0; // val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off and 1 = LED on
int brightness = 127; // how bright the LED is, stores the brigthness value
unsigned long startTime = 0; // when did we begin pressing?
void setup()
{
pinMode(3, OUTPUT); //tell Arduino LED 3,5,6
pinMode(5, OUTPUT); // are outputs
pinMode(6, OUTPUT);
pinMode(7, INPUT); // and BOTTOMs 7,4,2
pinMode(4, INPUT); // are inputs
pinMode(2, INPUT);
}
void loop()
{
val = digitalRead(7); // read input value and store it // yum, fresh
delay(10); //de-bouncing delay, it works better
//with this than the second de-bouncing delay alone
// check if there was a transition
if ((val == HIGH) && (old_val == LOW))
{
state = 1 - state; // change the stae from off to on or vice-versa
startTime = millis(); // millis() is the Arduino clock
// it returns how many millis have passed
// since the board has been reset.
// remember when the button was last pressed
delay(10); //second de-bouncing delay
}
/* here I want the LEDs change colors using the same
two fading BUTTONS, is that possible?
if( digitalRead(2) == HIGH) // change to red color
{
analogWrite(3, 255); // turns the LED on
analogWrite(5, 0); // turns the LED off
analogWrite(6, 0);
}
if (digitalRead(2) == HIGH) //if pressed again change to a different color
{
analogWrite(3, 0); // turns the LED off
analogWrite(5, 255); // turns the LED on
analogWrite(6, 0);
} */
// This part of the sketch is working fine
//checkwhether the button is being held down
if((digitalRead(4) == HIGH) && (digitalRead(2) == LOW))
{
// if the button is held for more than 500ms.
if (state == 1 && (millis() - startTime) > 500)
brightness++; //increment brightness by 1
delay (0); // delay to avoid brightness going up too fast
// 0 means fast and avobe 0 the LEDs fade slowly
if(brightness > 255) //255 is the max brightness
brightness = 255; // stays at 255
}
//checkwhether the button is being held down
if ((digitalRead(4) == LOW) && (digitalRead(2) == HIGH))
{
// if the button is held for more than 500ms.
if (state == 1 && (millis() - startTime) > 500)
brightness--; // decrement brightness by 1
delay (0); // delay to avoid brightness going down too fast
// 0 means fast and avobe 0 the LEDs fade slowly
if(brightness < 5) // 5 minimun brightness stablish
brightness = 5; // stays at 5
}
old_val = val; // val is now old, let's store it
if (state == 1)
{
analogWrite(3, brightness); // turn LEDs ON at the current brightnes level
analogWrite(5, brightness);
analogWrite(6, brightness);
}
else
{
analogWrite(3,0); //this part of the program makes the LEDs
analogWrite(5,0); //SHUT OFF when pressing the BUTTON 7
analogWrite(6,0);
}
}
/code]