Devices & Components
Arduino Uno Rev3
RGB Diffused Common Cathode
Tactile Switch, Top Actuated
Breadboard (generic)
Resistor 220 ohm
Rotary potentiometer (generic)
Jumper wires (generic)
Project description
Code
The code
arduino
1int rPin = 6; 2int gPin = 5; 3int bPin = 3; 4int buttonPin = 13; 5int potPin = A0; 6 7//an array holds the different modes 8String modes[3] = {"blue" , "red" , "green"}; 9String currentMode; 10 11int PWM; 12int potVal; 13 14int greenVal; 15int blueVal; 16int redVal; 17 18bool greenState; 19bool blueState; 20bool redState; 21 22int currentButtonState; 23int oldButtonState; 24int i = 0 ; 25 26 27void setup() { 28 29pinMode(rPin,OUTPUT); 30pinMode(bPin,OUTPUT); 31pinMode(gPin,OUTPUT); 32pinMode(buttonPin,INPUT_PULLUP); 33pinMode(potPin,INPUT); 34} 35 36void loop() { 37 38 39//controls the current colour mode 40currentButtonState = digitalRead(buttonPin); 41if (currentButtonState == 1 && oldButtonState==0){ 42 i = i+1; 43 delay(50); 44 45//ensure the array loops around 46 if(i == 3){ 47 i=0; 48 } 49} 50oldButtonState = currentButtonState; 51 52 53currentMode = modes[i]; 54 55 56potVal = analogRead(potPin); 57PWM = (potVal * 255./1023.); 58 59//potentiometer sens adjustment 60if (PWM <= 20){ 61 PWM = 0; 62} 63 64 65 66if (currentMode == "red"){ 67 greenState=false; 68 69 if(redState == false && PWM == redVal){ 70 redState = true; 71 } 72 73 if(redState==true){ 74 redVal = PWM; 75 analogWrite(rPin, redVal); 76 } 77 78} 79 80 81if (currentMode == "green"){ 82 blueState = false; 83 84 if(greenState == false && PWM == greenVal){ 85 greenState = true; 86 } 87 88 if(greenState==true){ 89 greenVal = PWM; 90 analogWrite(gPin, greenVal); 91 } 92} 93 94 95if (currentMode == "blue"){ 96 redState = false; 97 98 if(blueState == false && PWM == blueVal){ 99 blueState = true; 100 } 101 102 if(blueState==true){ 103 blueVal = PWM; 104 analogWrite(bPin, blueVal); 105 } 106} 107 108 109}
Downloadable files
Schematic
Schematic

Comments
Only logged in users can leave comments