12 Mayıs 2015 Salı

To adjust brightness with leds and buttons

Brightness can be adjusted by buttons. One button increases the brightness, other decreases.





Code is in the below :

const int ledPin = 11;
const int button1= 2;
const int button2 = 3;
byte brightness[] = {0, 30, 100,180,255}; //these are brightness levels, they can be changed
byte state=0;
void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
}
void loop() {
 
  if(!digitalRead(button1)){//button pressed
    if(state<4){
     state++;
     analogWrite(ledPin, brightness[state]);
     Serial.println("High brightness");
     delay(250);
    }
  }
  else if(!digitalRead(button2)){//button pressed
    if(state>0){
      state--;
      analogWrite(ledPin, brightness[state]);
      Serial.println("Low brightness");
      delay(250);
    }
  }
  Serial.println(state);
  Serial.println(brightness[state]);
 
   
 
}



Hiç yorum yok:

Yorum Gönder