Devices & Components
RGB Diffused Common Cathode
Resistor 221 ohm
3 position dip switch
Breadboard (generic)
Jumper wires (generic)
Software & Tools
Arduino IDE
Project description
Code
4 prong LED with dip swtich
arduino
1//Whipplashes 2//Code orginally taken from inventr.io and modified to fit the needs of this project 3//4-21-22 4 5int red = 11; //output 6int green = 10; 7int blue = 9; 8 9int Switch1 = 2; //Input 10int Switch2 = 3; 11int Switch3 = 4; 12 13void setup() { 14// Setup pins 15 16 pinMode(red, OUTPUT); 17 pinMode(green, OUTPUT); 18 pinMode(blue, OUTPUT); 19 pinMode(Switch1, INPUT); 20 pinMode(Switch2, INPUT); 21 pinMode(Switch3, INPUT); 22} 23 24void RGB_color(int red_value, int green_value, int blue_value) 25{ 26// RBG function to pass on color values 27 28 analogWrite(red, red_value); 29 analogWrite(green, green_value); 30 analogWrite(blue, blue_value); 31} 32 33 34 35void loop() { //Various if and if else statments designed to trigger once only when the corresponding values on the breadboard are selected. 36 37 if (digitalRead(Switch1) == HIGH && digitalRead(Switch2) != HIGH && digitalRead(Switch3) != HIGH) 38 39 { 40 RGB_color(125, 0, 0); // Red 41 delay(800); 42 RGB_color(0, 125, 0); // Green 43 delay(800); 44 45 } 46 else if (digitalRead(Switch2) == HIGH && digitalRead(Switch1) != HIGH && digitalRead(Switch3) != HIGH) 47 { 48 RGB_color(0, 0, 125); // Blue 49 delay(800); 50 RGB_color(64, 32, 0); // yellow 51 delay(800); 52 } 53 else if (digitalRead(Switch3) == HIGH && digitalRead(Switch2) != HIGH && digitalRead(Switch1) != HIGH) 54 { 55 RGB_color(125, 0, 125); // purple 56 delay(800); 57 RGB_color(125, 125, 125); // white 58 delay(800); 59 } 60 else if (digitalRead(Switch1) == HIGH && digitalRead(Switch2) == HIGH && digitalRead(Switch3) != HIGH) 61 { 62 RGB_color(125, 0, 0); // Red 63 delay(800); 64 RGB_color(0, 125, 0); // Green 65 delay(800); 66 RGB_color(0, 0, 125); // Blue 67 delay(800); 68 RGB_color(64, 32, 0); // yellow 69 delay(800); 70 71 } 72 else if (digitalRead(Switch1) == HIGH && digitalRead(Switch3) == HIGH && digitalRead(Switch2) != HIGH) 73 { 74 RGB_color(125, 0, 0); // Red 75 delay(800); 76 RGB_color(0, 125, 0); // Green 77 delay(800); 78 RGB_color(125, 0, 125); // purple 79 delay(800); 80 RGB_color(125, 125, 125); // white 81 delay(800); 82 } 83 else if (digitalRead(Switch2) == HIGH && digitalRead(Switch3) == HIGH && digitalRead(Switch1) != HIGH) 84 { 85 RGB_color(0, 0, 125); // Blue 86 delay(800); 87 RGB_color(64, 32, 0); // yellow 88 delay(800); 89 RGB_color(125, 0, 125); // purple 90 delay(800); 91 RGB_color(125, 125, 125); // white 92 delay(800); 93 94 } 95 else if (digitalRead(Switch2) == HIGH && digitalRead(Switch3) == HIGH && digitalRead(Switch1) == HIGH) 96 { 97 RGB_color(125, 0, 0); // Red 98 delay(800); 99 RGB_color(0, 125, 0); // Green 100 delay(800); 101 RGB_color(0, 0, 125); // Blue 102 delay(800); 103 RGB_color(64, 32, 0); // yellow 104 delay(800); 105 RGB_color(125, 0, 125); // purple 106 delay(800); 107 RGB_color(125, 125, 125); // white 108 delay(800); 109 } 110 else if (digitalRead(Switch2) != HIGH && digitalRead(Switch3) != HIGH && digitalRead(Switch1) != HIGH) 111 { 112 RGB_color(0,0,0); 113 delay(500); 114 } 115 116}
Downloadable files
screenshot_(193)_4COkQXiRyo.png
screenshot_(193)_4COkQXiRyo.png

screenshot_(193)_4COkQXiRyo.png
screenshot_(193)_4COkQXiRyo.png

Comments
Only logged in users can leave comments