byte ledRed =9;
byte ledGrn = 10;
byte ledBlu = 11;
byte ledReady = 13;
byte button[] = {2, 3, 4};
int buttonstate[]= {0,0,0};
int buttonlevel[]= {0,0,0};
int ledval[] = {0,50,100,150,200,255};
unsigned long lastpressed[] = {0,0,0};
void setup(){
pinMode (ledReady,OUTPUT); // I use this to notify me when reset completed
pinMode (ledRed,OUTPUT);
pinMode (ledGrn, OUTPUT);
pinMode (ledBlu, OUTPUT);
for (int x =0;x<=3; x++)
{
pinMode (button[x], INPUT); // configure buttons
}
digitalWrite (ledRed, LOW);
digitalWrite (ledGrn, LOW);
digitalWrite (ledBlu, LOW);
digitalWrite (ledReady,HIGH); // I use this to notify me when reset completed
/* Serial.begin(9600); // for debugging only */
}
void loop(){
for (int x = 0;x <=2; x++){
buttonstate[x] = digitalRead (button[x]); // check if 1 of 3 buttons pressed
if (buttonstate[x] == HIGH && (millis()- lastpressed[x]) >500) {
buttonlevel[x] +=1;
if (buttonlevel[x] > 5) {buttonlevel[x] = 0;}
/* Serial.println ( buttonlevel[x]); // for debug */
lastpressed[x] = millis(); // get time of when button pressed used for debounce
}
}
analogWrite (ledRed, ledval[buttonlevel[0]]); // set level of Red LED
analogWrite (ledGrn, ledval[buttonlevel[1]]); // set level of Green LED
analogWrite (ledBlu, ledval[buttonlevel[2]]); // set level of Blue LED */
delay (250);
}
No comments:
Post a Comment