I have two separate sketches one is to turn on an off the led's and the other one is to change between cases, both are working but i'm having problems to put them together.
The only thing that the program is doing is turning on the LED's I think i'm having problems whit the curly brackets.
#define REDPIN 3 // Red LED connected to pin # 3
#define GREENPIN 5 //Green LED connected to pin # 5
#define BLUEPIN 6 // Blue LED connected to pin # 6
// Pushbuttons must be momentary tactile pushbuttons.
// 5 volt from the Arduino goes connected to one side of the momentary switch
// and the other side goes connected to the numbers from the Arduino and
//to ground also with a 10k pullup resistor.
#define BUTTON 4 brightness++ and state up // This is going to do two funtions, but I'm worried
// changing state for now
#define BUTTON 2 brightness-- and state down // This also is going to do two functions
#define BUTTON 7 on/of // this momentary pusbutton is going to turn the LEDs on and off
// and the Arduino is going to have a separate kill switch.
// this integers are for the on/off sketch
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
//this integers are for the switching up and down cases
int btnPressCount = 0;
int btnStateUp = LOW;
int lastBtnStateUp = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;
void setup(){
pinMode(REDPIN, OUTPUT); //tell Arduino LED
pinMode(GREENPIN, OUTPUT); // are outputs
pinMode(BLUEPIN, OUTPUT);
pinMode(7, INPUT); // and BOTTOMs 7,4,2
pinMode(4, INPUT); // are inputs
pinMode(2, INPUT);
}
void loop(){ // this part of the sketch turns the LEDs on
val = digitalRead(7); // read input value and store it // yum, fresh
delay(10); //de-bouncing delay, (my idea), it works better
//with this than the second de-bouncing delay alone
if ((val == HIGH) && (old_val == LOW)) // check if there was a transition
{
state = 1 - state; // change the stae from off to on or vice-versa
delay(10); //second de-bouncing delay
old_val = val; // val is now old, let's store it
if (state == 1)
//here starts the changing cases sketch
{
btnStateUp = digitalRead(4);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
btnPressCount++; // Count this press
lastBtnStateUp = btnStateUp; // Use btnPressCount
btnStateDn = digitalRead(2);
if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
btnPressCount--; // Count this press
lastBtnStateDn = btnStateDn; // Use btnPressCount
//this part mantains the counts equal to 0
// so it doesn't make the btnPressCount unsigned
if(btnPressCount < 0) // <-- Add me
btnPressCount = 0; // <-- And me
btnPressCount = btnPressCount % 7; // Keep count in the range 0 to 6
switch(btnPressCount)
{
case 0:
analogWrite(REDPIN, 255); // turns the LED on
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 255);
break;
case 1:
analogWrite(REDPIN, 255); // on
analogWrite(GREENPIN, 0); // off
analogWrite(BLUEPIN, 0);
break;
case 2:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0 );
break;
case 3:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 255);
break;
case 4:
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
break;
case 5:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 255);
break;
case 6:
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 255);
break;
}
}
else
{
analogWrite(REDPIN, 0); //this part of the program makes the LEDs
analogWrite(GREENPIN, 0); //SHUT OFF when pressing the BUTTON 7
analogWrite(BLUEPIN ,0);
}
}
}
/code]