const int buttonpin1=3;
const int buttonpin2=11;
const int ledpin=6;
unsigned int buttonval1;
unsigned int buttonval2;
unsigned int oldbuttonval1 = 1;
unsigned int oldbuttonval2=1;
unsigned int ledstate=0;
unsigned int low=63;
unsigned int med=127;
unsigned int led_percent;
void setup() {
// put your setup code here, to run once:
pinMode(buttonpin1, INPUT_PULLUP);
pinMode(buttonpin2, INPUT_PULLUP);
pinMode(ledpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonval1=digitalRead(buttonpin1);//value of green button
buttonval2=digitalRead(buttonpin2);//value of black button
// execution on the input of green button
// led brightness is increasing with each toogle of green button
if(oldbuttonval1==HIGH && buttonval1==LOW){
delay(200); //debouncing time
switch(ledstate){
case 0:analogWrite(ledpin,low);
Serial.println("the percentage of led brightness: ");
Serial.println("25");
ledstate=1;
break;
case 1:analogWrite(ledpin,med);
Serial.println("the percentage of led brightness: " );
Serial.println("50");
ledstate=2;
break;
case 2:analogWrite(ledpin,HIGH);
Serial.println("the percentage of led brightness:");
Serial.println("100");
ledstate=3;
break;
default:analogWrite(ledpin,HIGH);
Serial.println("the percentage of led brightness:");
Serial.println("100");
ledstate=3;
break;
}
}
// execution on the input of black button
// led brightness is decreasing with each toogle of black button
if(oldbuttonval2==HIGH && buttonval2==LOW){
delay(200);
switch(ledstate){
case 3:analogWrite(ledpin,med);
Serial.println("the percentage of led brightness: ");
Serial.println("50");
ledstate=2;
break;
case 2:analogWrite(ledpin,low);
Serial.println("the percentage of led brightness: " );
Serial.println("25");
ledstate=1;
break;
case 1:analogWrite(ledpin,LOW);
Serial.println("the percentage of led brightness:");
Serial.println("0");
ledstate=0;
break;
default:analogWrite(ledpin,LOW);
Serial.println("the percentage of led brightness:");
Serial.println("0");
ledstate=0;
break;
}
}
oldbuttonval1=buttonval1;
oldbuttonval2=buttonval2;
}