-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRGBLED_BeginnerArduino_Full.ino
More file actions
117 lines (95 loc) · 3.34 KB
/
RGBLED_BeginnerArduino_Full.ino
File metadata and controls
117 lines (95 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
////////////////////////////////
// Intro to Arduino Workshop //
// RGBLED Color Editor //
// Engineering Design Center //
// University of Arizona //
// Full Code //
// Rev: 11-15-24 //
////////////////////////////////
// Define LED Pins
#define redLED 5
#define blueLED 6
#define greenLED 7
#define redRGB 9
#define blueRGB 10
#define greenRGB 11
// Define Button Pins
#define buttonRed 2
#define buttonBlue 3
#define buttonGreen 4
//Pot Pin
#define potPin A0
//Variables instantiation
int redBrightness = 0;
int blueBrightness = 0;
int greenBrightness = 0;
int activeLED = 0; // 0 - None, 1 - Red, 2 - Blue, 3 - Green
void setup() {
// put your setup code here, to run once:
// Set the pin mode to input with a pullup resistor to 5V for each button pin
pinMode(buttonRed, INPUT_PULLUP);
pinMode(buttonBlue, INPUT_PULLUP);
pinMode(buttonGreen, INPUT_PULLUP);
// Set the LED pins as outputs
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redRGB, OUTPUT);
pinMode(blueRGB, OUTPUT);
pinMode(greenRGB, OUTPUT);
//Start serial connection
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//Poll the buttons and assign the activeLED
if (digitalRead(buttonRed) == LOW){ //Because of the pullup resistor the button is grounded (LOW) when pressed
activeLED = 1; //Set active LED to Red(1) when button is pressed
}
else if(digitalRead(buttonBlue) == LOW){
activeLED = 2; //Set active LED to Blue(2) when button is pressed
}
else if(digitalRead(buttonGreen) == LOW){
activeLED = 3; //Set active LED to Green(3) when button is pressed
}
/*//Print values to the Serial Monitor for debugging
Serial.print("Active LED: "); Serial.print(activeLED); Serial.print("\n");
Serial.print("Button 1: "); Serial.print(digitalRead(buttonRed)); Serial.print("\n");
Serial.print("Button 2: "); Serial.print(digitalRead(buttonBlue)); Serial.print("\n");
Serial.print("Button 3: "); Serial.print(digitalRead(buttonGreen)); Serial.print("\n");
Serial.println();
delay(500);
//*/
//Turn on the indicator LEDs acordingly
switch(activeLED){
case 1: //Red
digitalWrite(redLED, HIGH);
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, LOW);
redBrightness = analogRead(potPin); //Read value from potentiometer
analogWrite(redRGB, redBrightness / 4); //Set color brightness
break;
case 2: //Blue
digitalWrite(redLED, LOW);
digitalWrite(blueLED, HIGH);
digitalWrite(greenLED, LOW);
blueBrightness = analogRead(potPin); //Read value from potentiometer
analogWrite(blueRGB, blueBrightness / 4); //Set color brightness
break;
case 3: //Red
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, HIGH);
greenBrightness = analogRead(potPin); //Read value from potentiometer
analogWrite(greenRGB, greenBrightness / 4); //Set color brightness
break;
}
//*/ Prints brightness values
Serial.print("Red: ");
Serial.print(redBrightness);
Serial.print(",Blue: ");
Serial.print(blueBrightness);
Serial.print(",Green: ");
Serial.println(greenBrightness);
//*/
}