I'm in a physics class and would like to go above and beyond, but i need some help getting this code to work. Any suggestions? Code is below all help is appreciated. Basically what i wanted to do is control lights on and off with a pushbutton using my own code, but it is not registering and nothing is lighting up. I also have a potentiometer that will control the brightness of the PWM leds, the pot is working just not the digital pins. There are 5 buttons and 7 LEDs.
Thanks,
Andrew
#define led1 0
#define led2 1
#define led3 2
#define led4 3
#define led5 4
#define led6 5
#define led7 6
#define button1 7
#define button2 8
#define button3 9
#define button4 10
#define button5 11
int state1 = 0; //stores the current state of the led
int state2 = 0;
int state3 = 0;
int state4 = 0;
int state5 = 0;
int pot = 0;
int potval = 0;
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
int temp4 = 0;
int temp5 = 0;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
Serial.begin(9600); //for debugging
}
void loop()
{
pot = analogRead(0); //get reading from pot
potval = map(pot, 0, 1023, 0, 255); //if it appears that the whole scale is not used use a smaller or no resistor
if(button1==1 || button2==1 || button3==1 || button4==1 || button5==1) // if any state change wait 50ms for debounce
{
delay(50); //debounce
temp1 = digitalRead(button1); //then determine which buttons were pressed
temp2 = digitalRead(button2);
temp3 = digitalRead(button3);
temp4 = digitalRead(button4);
temp5 = digitalRead(button5);
if(temp1 == 1 && state1 == 0) //if a button is pressed change the led status, if 0 change to 1, if 1 change to 0.
{
state1 = 1;
}
else
{
state1 = 0;
}
if(temp2 == 1 && state2 == 0)
{
state2 = 1;
}
else
{
state2 = 0;
}
if(temp3 == 1 && state3 == 0)
{
state3 = 1;
}
else
{
state3 = 0;
}
if(temp4 == 1 && state4 == 0)
{
state4 = 1;
}
else
{
state4 = 0;
}
if(temp5 == 1 && state5 == 0)
{
state5 = 1;
}
else
{
state5 = 0;
}
}
if(state3 == 1 ) //change the status of the PWM LEDs
{
analogWrite(led4, potval);
}
else
{
analogWrite(led4, 0); //write the pot value or off
}
if(state5 == 1 )
{
analogWrite(led6, potval);//same as above
}
else
{
analogWrite(led6, 0);
}
digitalWrite(led1, state1); //write status to digital leds
digitalWrite(led2, state2);
digitalWrite(led3, state3);
digitalWrite(led5, state5);
digitalWrite(led7, state5); //control 2 LEDs
Serial.print(state1); //to help with debugging, tell the state
Serial.print(" ");
Serial.print(temp1);
Serial.print(" ");
Serial.println(potval);
delay(20); //slow down the serial conn.
}