|
| 1 | +// C++ code |
| 2 | +// |
| 3 | +void setup() { |
| 4 | + pinMode(9, OUTPUT); |
| 5 | + pinMode(10, OUTPUT); |
| 6 | + pinMode(11, OUTPUT); |
| 7 | + |
| 8 | + Serial.begin(9600); |
| 9 | +} |
| 10 | + |
| 11 | +void loop() { |
| 12 | + int mode = 6; // Change this variable to select the mode (1 to ...) |
| 13 | + |
| 14 | + switch (mode) { |
| 15 | + case 1: |
| 16 | + singleColorBlink(); |
| 17 | + break; |
| 18 | + case 2: |
| 19 | + randomizeColors(); |
| 20 | + break; |
| 21 | + case 3: |
| 22 | + generateSecondaryColors(); |
| 23 | + break; |
| 24 | + case 4: |
| 25 | + random16MillionColors(); |
| 26 | + break; |
| 27 | + case 5: |
| 28 | + increaseBrightness(); |
| 29 | + break; |
| 30 | + case 6: |
| 31 | + increaseBrightness2(); |
| 32 | + break; |
| 33 | + default: |
| 34 | + Serial.println("Invalid mode selected."); |
| 35 | + break; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void singleColorBlink() { |
| 40 | + Serial.println("Mode 1: Single Color Blink"); |
| 41 | + |
| 42 | + digitalWrite(9, HIGH); |
| 43 | + delay(1000); // Wait for 1000 millisecond(s) |
| 44 | + digitalWrite(9, LOW); |
| 45 | + delay(1000); // Wait for 1000 millisecond(s) |
| 46 | + |
| 47 | + digitalWrite(10, HIGH); |
| 48 | + delay(1000); // Wait for 1000 millisecond(s) |
| 49 | + digitalWrite(10, LOW); |
| 50 | + delay(1000); // Wait for 1000 millisecond(s) |
| 51 | + |
| 52 | + digitalWrite(11, HIGH); |
| 53 | + delay(1000); // Wait for 1000 millisecond(s) |
| 54 | + digitalWrite(11, LOW); |
| 55 | + delay(1000); // Wait for 1000 millisecond(s) |
| 56 | +} |
| 57 | + |
| 58 | +void randomizeColors() { |
| 59 | + Serial.println("Mode 2: Randomize Colors"); |
| 60 | + |
| 61 | + int randno = random(9, 12); |
| 62 | + Serial.print("Random number = "); |
| 63 | + Serial.println(randno); |
| 64 | + digitalWrite(randno, HIGH); |
| 65 | + delay(100); |
| 66 | + digitalWrite(randno, LOW); |
| 67 | + delay(100); |
| 68 | +} |
| 69 | + |
| 70 | +void generateSecondaryColors() { |
| 71 | + Serial.println("Mode 3: Generate Secondary Colors"); |
| 72 | + |
| 73 | + digitalWrite(9, HIGH); |
| 74 | + digitalWrite(10, HIGH); |
| 75 | + delay(1000); // Wait for 1000 millisecond(s) |
| 76 | + digitalWrite(9, LOW); |
| 77 | + digitalWrite(10, LOW); |
| 78 | + delay(1000); // Wait for 1000 millisecond(s) |
| 79 | + |
| 80 | + digitalWrite(10, HIGH); |
| 81 | + digitalWrite(11, HIGH); |
| 82 | + delay(1000); // Wait for 1000 millisecond(s) |
| 83 | + digitalWrite(10, LOW); |
| 84 | + digitalWrite(11, LOW); |
| 85 | + delay(1000); // Wait for 1000 millisecond(s) |
| 86 | + |
| 87 | + digitalWrite(11, HIGH); |
| 88 | + digitalWrite(9, HIGH); |
| 89 | + delay(1000); // Wait for 1000 millisecond(s) |
| 90 | + digitalWrite(11, LOW); |
| 91 | + digitalWrite(9, LOW); |
| 92 | + delay(1000); // Wait for 1000 millisecond(s) |
| 93 | +} |
| 94 | + |
| 95 | +void random16MillionColors() { |
| 96 | + Serial.println("Mode 4: Random 16 Million Colors"); |
| 97 | + |
| 98 | + int R = random(0, 256); |
| 99 | + Serial.print("RED = "); |
| 100 | + Serial.println(R); |
| 101 | + |
| 102 | + int B = random(0, 256); |
| 103 | + Serial.print("BLUE = "); |
| 104 | + Serial.println(B); |
| 105 | + |
| 106 | + int G = random(0, 256); |
| 107 | + Serial.print("GREEN = "); |
| 108 | + Serial.println(G); |
| 109 | + |
| 110 | + analogWrite(9, G); |
| 111 | + analogWrite(10, B); |
| 112 | + analogWrite(11, R); |
| 113 | + |
| 114 | + delay(1000); // Add a delay to make the color visible for a while |
| 115 | +} |
| 116 | + |
| 117 | +void increaseBrightness() { |
| 118 | + Serial.println("Mode 5: Increase Brightness"); |
| 119 | + |
| 120 | + // Full brightness (100%) |
| 121 | + colorRGB(255, 0, 0, 100); |
| 122 | + delay(1000); |
| 123 | + |
| 124 | + // Half brightness (50%) |
| 125 | + colorRGB(255, 0, 0, 50); |
| 126 | + delay(1000); |
| 127 | + |
| 128 | + // Quarter brightness (25%) |
| 129 | + colorRGB(255, 0, 0, 25); |
| 130 | + delay(1000); |
| 131 | +} |
| 132 | + |
| 133 | +void colorRGB(int red, int green, int blue, int brightness_percent) { |
| 134 | + const int redPin = 9; |
| 135 | + const int greenPin = 10; |
| 136 | + const int bluePin = 11; |
| 137 | + |
| 138 | + analogWrite(redPin, red * brightness_percent / 100); |
| 139 | + analogWrite(greenPin, green * brightness_percent / 100); |
| 140 | + analogWrite(bluePin, blue * brightness_percent / 100); |
| 141 | +} |
| 142 | + |
| 143 | +void increaseBrightness2(){ |
| 144 | + //Increading Brightness for each colour |
| 145 | + Serial.println("Mode 6: Increase Brightness Implementation 2"); |
| 146 | + |
| 147 | + for (int R=0; R<=255; R++){ |
| 148 | + Serial.print("R = "); |
| 149 | + Serial.println(R); |
| 150 | + analogWrite(11,R); |
| 151 | + } |
| 152 | + analogWrite(11,0); |
| 153 | + |
| 154 | + for (int B=0; B<=255; B++){ |
| 155 | + Serial.print("B = "); |
| 156 | + Serial.println(B); |
| 157 | + analogWrite(10,B); |
| 158 | + } |
| 159 | + analogWrite(10,0); |
| 160 | + |
| 161 | + for (int G=0; G<=255; G++){ |
| 162 | + Serial.print("G = "); |
| 163 | + Serial.println(G); |
| 164 | + analogWrite(9,G); |
| 165 | + } |
| 166 | + analogWrite(9,0); |
| 167 | +} |
| 168 | + |
0 commit comments