// variable definition const int LED = 9; const int BUTTON = 7; int butt,butt_old,state,light; // Arduino init void setup() { pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); state=0; } // Arduino loop void loop() { butt=digitalRead(BUTTON); if ((butt-butt_old)==1) state=state+1; // state changes during the rising edge of "butt" if (state==3) state=0; // state cycle among 0, 1 or 2 if (state==0) light=0; // on state 0 light is OFF if (state==1) light=32; // on state 1 light is dim if (state==2) light=255; // on state 2 light is ON analogWrite(LED,light); butt_old=butt; delay(50); // to avoid button rebounds }