// // Pin assignment: ( The RGB Led Pins need to be PWM pins; // the potentiometer pin should be an analog input ) // // Pin 3 : RGB LED Red // Pin 6 : RGB LED Blue // Pin 5 : RGB LED Blue // Pin 9 : RGB LED Green // Pin 2 : red LED // Pin 4 : blue LED // Pin 7 : blue LED // Pin 8 : green LED // Pin 4 : potentiometer // Pin 10 : left switch // Pin 11 : right switch int rgbPins[] = { 3, 6, 5, 9 }; int ledPins[] = { 2, 4, 7, 8 }; int knobPin = 4; int switch1Pin = 10; int switch2Pin = 11; void setup() { for( int i = 0; i < 4; i++ ){ pinMode( rgbPins[i], OUTPUT ); pinMode( ledPins[i], OUTPUT ); } } /* readButton: Returns the current state of a digital input. Only returns true if the input is constant high for at least 10 milliseconds to debounce the input. */ boolean readButton( int pin ) { int msecs = 10; while( digitalRead( pin ) && msecs > 0 ){ delay( 1 ); msecs--; } return( msecs == 0 ); } /* lightLED: Turns on given LED, turning off all others You can pass -1 to turn off all LEDs */ void lightLED( int led ) { for( int i = 0; i < 4; i++ ) digitalWrite( ledPins[i], ( i == led ) ? HIGH : LOW ); } int currentLED = 0; boolean blinking = 0; boolean blinkState = HIGH; unsigned long lastMillis = 0; void loop() { // the left button activates the next LED and resets the // blinking state if( readButton( switch1Pin ) ){ // wait until the button is depressed while( readButton( switch1Pin ) ) ; currentLED = ( currentLED + 1 ) % 4; lightLED( currentLED ); blinking = 0; blinkState = HIGH; } // the right button activates the blinking state. while // the LED is blinking, you can fade the corresponding RGB colour // with the potentiometer if( readButton( switch2Pin ) ){ while( readButton( switch2Pin ) ) ; blinking = 1; lastMillis = millis(); } if( blinking ){ // blink the LED every 250 ms if( millis() > ( lastMillis + 250 ) ){ lastMillis = millis(); blinkState = !blinkState; } int val; val = analogRead( knobPin ); val = map( val, 0, 1023, 0, 255 ); // analogWrite will set the PWM output on a pin analogWrite( rgbPins[currentLED], val ); } if( blinkState ) lightLED( currentLED ); else lightLED( -1 ); }