Skip to content

Commit d69710c

Browse files
authored
Update GigaWiFi_RGB_LED_Demo.ino
1 parent f0162fc commit d69710c

File tree

1 file changed

+147
-89
lines changed

1 file changed

+147
-89
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,211 @@
11
/*
22
Arduino Giga WiFi – RGB LED Demonstration
33
4-
This sketch showcases how to control the built-in RGB LED on the Arduino Giga R1 WiFi board.
5-
It demonstrates basic color mixing by setting the Red, Green, and Blue channels independently, allowing you to experiment with various color outputs.
4+
This sketch showcases how to control the built-in RGB LED connected to the digital pins D86, D87 and D88 (Only RGB, not accessible as GPIO) on the Arduino Giga R1 WiFi board.
65
7-
Designed specifically for the Giga R1 WiFi, this example helps new users understand how to interface with onboard components without needing any external wiring.
6+
It demonstrates basic color mixing by setting the Red, Green, and Blue channels independently, allowing you to experiment with various color outputs without needing any external wiring.
7+
8+
Designed specifically for the Giga R1 WiFi, this example helps new users interface with the onboard RGB LED (whose channels are predefined) and understand timing and sequential transitions.
89
910
Board Compatibility:
10-
* Arduino Giga R1 WiFi (only) connected via USB-C.
11+
* Arduino Giga R1 WiFi (only) connected via USB-C.
1112
1213
Features Demonstrated:
13-
* Direct control of RGB LED pins
14-
* Simple timing with delay()
15-
* Sequential color transitions
14+
* Direct control of the built-in RGB LED channels
15+
* Simple timing with delay()
16+
* Sequential color transitions
1617
1718
Available Commands (case-sensitive):
18-
BLED - Switch ON the RED channel of the RGB LED.
19-
GLED - Switch ON the GREEN channel of the RGB LED.
20-
RLED - Switch ON the BLUE channel of the RGB LED.
21-
LEDoff - Switch OFF the LED.
22-
BlinkB - Blink the LED in the Blue color channel.
23-
BlinkG - Blink the LED in the Green color channel.
24-
BlinkR - Blink the LED in the Red color channel.
19+
RLED - Turn on the LED in Red color.
20+
GLED - Turn on the LED in Green color.
21+
BLED - Turn on the LED in Blue color.
22+
LEDoff - Turn off the LED.
23+
BlinkR - Blink the LED in the Red channel (default 5 times).
24+
BlinkG - Blink the LED in the Green channel (default 5 times).
25+
BlinkB - Blink the LED in the Blue channel (default 5 times).
26+
BlinkR(n) - Blink the LED in the Red channel n times.
27+
BlinkG(n) - Blink the LED in the Green channel n times.
28+
BlinkB(n) - Blink the LED in the Blue channel n times.
29+
ToggleRGB - Blink the LED in an RGB sequence.
2530
2631
Usage:
27-
1. Upload the sketch to your Arduino Giga R1 WiFi.
28-
2. Start the serial montior.
29-
3. Interact by typing in any of the previous 7 commands.
32+
1. Upload the sketch to your Arduino Giga R1 WiFi.
33+
2. Open the Serial Monitor.
34+
3. Type one of the above commands to control the LED.
3035
3136
Created: 09 April 2025
3237
Author: Ahmed Sharaf (Tech Forge International LLC)
3338
License: MIT
3439
*/
3540

3641
// Global variables and constants
37-
String inputCommand = ""; // Buffer to store incoming Serial data.
38-
const int commandDelay = 500; // Delay used for LED blinking in milliseconds (change according to your personal pereference).
39-
const int blinkCount = 5; // Number of times to blink the LED (change according to your personal pereference).
42+
String inputCommand = ""; // Buffer to store incoming Serial data.
43+
const int commandDelay = 500; // Delay for LED blinking in milliseconds.
44+
const int defaultBlinkCount = 5; // Default number of blinks if not specified.
4045

4146
// Function declarations
4247
void processCommand(String command);
43-
void TurnOn(int led, String ledName);
44-
void BlinkLED(int led, String ledName);
48+
void TurnOn(int led, String colorName);
49+
void BlinkLED(int led, String colorName, int numBlinks);
4550
void TurnOffLED();
51+
void ToggleRGB();
4652

4753
void setup() {
4854
// Initialize Serial communication at 115200 baud.
4955
Serial.begin(115200);
56+
while (!Serial) { /* Wait for Serial to be ready. */ }
5057

51-
// Wait for the serial port to connect (for native USB boards).
52-
while (!Serial) {
53-
; // Do nothing until Serial is ready.
54-
}
55-
56-
// Configure LED pins as outputs.
57-
pinMode(LEDB, OUTPUT);
58-
pinMode(LEDG, OUTPUT);
59-
pinMode(LEDR, OUTPUT);
58+
// The built-in RGB LED channels are predefined—set them as outputs.
59+
pinMode(LEDR, OUTPUT); //Digital Pin D86 (Only RGB, not accessible as GPIO)
60+
pinMode(LEDG, OUTPUT); //Digital Pin D87 (Only RGB, not accessible as GPIO)
61+
pinMode(LEDB, OUTPUT); //Digital Pin D88 (Only RGB, not accessible as GPIO)
6062

61-
// Turn off all LEDs at startup.
63+
// Ensure the LED is off at startup.
6264
TurnOffLED();
6365

64-
// Display welcome message and available commands.
65-
Serial.println("=========================================================");
66-
Serial.println(" Arduino Giga WiFi | RGB LED Control");
67-
Serial.println("=========================================================");
68-
Serial.println("Enter one of the following 7 commands (case-sensitive):");
69-
Serial.println("---------------------------------------------------------");
70-
Serial.println(" RLED : Switch ON the RED channel of the RGB LED.");
71-
Serial.println(" GLED : Switch ON the GREEN channel of the RGB LED.");
72-
Serial.println(" BLED : Switch ON the BLUE channel of the RGB LED.");
73-
Serial.println(" LEDoff : Switch OFF the RGB LED.");
74-
Serial.println(" BlinkR : Blink the LED in the RED color channel.");
75-
Serial.println(" BlinkG : Blink the LED in the GREEN color channel.");
76-
Serial.println(" BlinkB : Blink the LED in the BLUE color channel.");
77-
Serial.println("---------------------------------------------------------");
66+
// Display the welcome message and available commands.
67+
Serial.println("=========================================================================");
68+
Serial.println(" Arduino Giga WiFi | RGB LED Control");
69+
Serial.println("=========================================================================");
70+
Serial.println("Enter one of the following commands (case-sensitive):");
71+
Serial.println("-------------------------------------------------------------------------");
72+
Serial.println(" RLED : Switch ON the LED in RED color.");
73+
Serial.println(" GLED : Switch ON the LED in GREEN color.");
74+
Serial.println(" BLED : Switch ON the LED in BLUE color.");
75+
Serial.println(" LEDoff : Switch OFF the LED.");
76+
Serial.println(" BlinkR : Blink the LED in the RED channel (default 5 times).");
77+
Serial.println(" BlinkG : Blink the LED in the GREEN channel (default 5 times).");
78+
Serial.println(" BlinkB : Blink the LED in the BLUE channel (default 5 times).");
79+
Serial.println(" BlinkR(n) : Blink the LED in RED n times.");
80+
Serial.println(" BlinkG(n) : Blink the LED in GREEN n times.");
81+
Serial.println(" BlinkB(n) : Blink the LED in BLUE n times.");
82+
Serial.println(" ToggleRGB : Blink the LED in an RGB sequence.");
83+
Serial.println("-------------------------------------------------------------------------");
7884
}
7985

8086
void loop() {
81-
// Read incoming serial data character-by-character.
87+
// Read incoming Serial data character-by-character.
8288
while (Serial.available()) {
8389
char c = Serial.read();
84-
// Append characters to command buffer; ignore newline and comma delimiters.
90+
// Append characters to the command buffer; ignore newline and comma delimiters.
8591
if (c != '\n' && c != ',') {
8692
inputCommand += c;
8793
}
88-
delay(2); // Short delay to ensure complete data reception.
94+
delay(2); // Small delay helps ensure the full command is received.
8995
}
9096

91-
// If a command was received, process it.
97+
// Process the command if any command has been received.
9298
if (inputCommand.length() > 0) {
93-
inputCommand.trim(); // Remove any leading/trailing whitespace.
99+
inputCommand.trim(); // Remove any stray whitespace.
94100
processCommand(inputCommand);
95-
inputCommand = ""; // Clear the command buffer.
96-
Serial.flush(); // Clear any leftover data in the Serial buffer.
101+
inputCommand = ""; // Clear the command buffer.
102+
Serial.flush(); // Clear any leftover Serial data.
97103
}
98104
}
99105

100-
// Processes the received command and triggers the corresponding action.
106+
// Parses and processes incoming commands.
101107
void processCommand(String command) {
102-
// Debug print for testing purposes
103-
Serial.print("Received command: ");
104-
Serial.println(command);
105-
106-
if (command == "BLED") {
107-
TurnOn(LEDB, "Blue");
108-
} else if (command == "GLED") {
109-
TurnOn(LEDG, "Green");
110-
} else if (command == "RLED") {
108+
Serial.println("Received command: " + String(command));
109+
110+
// Parameterized blink commands.
111+
if (command.startsWith("BlinkR(") && command.endsWith(")")) {
112+
int numBlinks = command.substring(command.indexOf('(') + 1, command.indexOf(')')).toInt();
113+
if (numBlinks > 0)
114+
BlinkLED(LEDR, "Red", numBlinks);
115+
else
116+
Serial.println("Invalid number for BlinkR command.");
117+
}
118+
else if (command.startsWith("BlinkG(") && command.endsWith(")")) {
119+
int numBlinks = command.substring(command.indexOf('(') + 1, command.indexOf(')')).toInt();
120+
if (numBlinks > 0)
121+
BlinkLED(LEDG, "Green", numBlinks);
122+
else
123+
Serial.println("Invalid number for BlinkG command.");
124+
}
125+
else if (command.startsWith("BlinkB(") && command.endsWith(")")) {
126+
int numBlinks = command.substring(command.indexOf('(') + 1, command.indexOf(')')).toInt();
127+
if (numBlinks > 0)
128+
BlinkLED(LEDB, "Blue", numBlinks);
129+
else
130+
Serial.println("Invalid number for BlinkB command.");
131+
}
132+
// Default commands.
133+
else if (command == "RLED") {
111134
TurnOn(LEDR, "Red");
112-
} else if (command == "LEDoff") {
135+
}
136+
else if (command == "GLED") {
137+
TurnOn(LEDG, "Green");
138+
}
139+
else if (command == "BLED") {
140+
TurnOn(LEDB, "Blue");
141+
}
142+
else if (command == "LEDoff") {
113143
TurnOffLED();
114-
Serial.println("Turned off all LEDs.");
115-
} else if (command == "BlinkB") {
116-
BlinkLED(LEDB, "Blue");
117-
} else if (command == "BlinkG") {
118-
BlinkLED(LEDG, "Green");
119-
} else if (command == "BlinkR") {
120-
BlinkLED(LEDR, "Red");
121-
} else {
122-
Serial.println("Unrecognized command. Please try again using one of the 7 commands avaialble.");
144+
Serial.println("Turned off the LED.");
145+
}
146+
else if (command == "BlinkR") {
147+
BlinkLED(LEDR, "Red", defaultBlinkCount);
148+
}
149+
else if (command == "BlinkG") {
150+
BlinkLED(LEDG, "Green", defaultBlinkCount);
151+
}
152+
else if (command == "BlinkB") {
153+
BlinkLED(LEDB, "Blue", defaultBlinkCount);
154+
}
155+
else if (command == "ToggleRGB") {
156+
ToggleRGB();
157+
}
158+
else {
159+
Serial.println("Unrecognized command. Please try one of the available commands.");
123160
}
124161
}
125162

126-
// Activates the selected color channel of the RGB LED; assumes active LOW.
127-
void TurnOn(int led, String ledName) {
128-
// Ensure the LED is off before switching to the selected color.
129-
TurnOffLED();
130-
digitalWrite(led, LOW); // Activate the specified color channel.
131-
Serial.println("Turned on the " + ledName + " LED.");
163+
// Turns on a specified LED color channel (active LOW logic).
164+
void TurnOn(int led, String colorName) {
165+
TurnOffLED(); // Ensure only one channel is active.
166+
digitalWrite(led, LOW); // Activate the channel.
167+
Serial.println("Turned on the LED in " + colorName + " color.");
132168
}
133169

134-
// Blinks the specified color channel of the RGB LED a given number of times.
135-
void BlinkLED(int led, String colorName) {
136-
// Ensure all LEDs are off before blinking.
137-
TurnOffLED();
138-
Serial.println("Blinking the LED in " + colorName + " " + String(blinkCount) + " times.");
139-
for (int i = 0; i < blinkCount; i++) {
140-
digitalWrite(led, LOW); // Turn LED ON with the specified color.
170+
// Blinks the given LED channel a specified number of times.
171+
void BlinkLED(int led, String colorName, int numBlinks) {
172+
TurnOffLED(); // Ensure all channels are off before blinking.
173+
Serial.println("Blinking the LED in " + colorName + " " + String(numBlinks) + " times.");
174+
for (int i = 0; i < numBlinks; i++) {
175+
digitalWrite(led, LOW); // Turn channel on.
141176
delay(commandDelay);
142-
digitalWrite(led, HIGH); // Turn LED OFF.
177+
digitalWrite(led, HIGH); // Turn channel off.
143178
delay(commandDelay);
144179
}
145180
}
146181

147-
// Turns off all color channels of the integrated RGB LED; assumes active LOW.
182+
// Turns off all channels (assumes active LOW logic).
148183
void TurnOffLED() {
149184
digitalWrite(LEDB, HIGH);
150185
digitalWrite(LEDG, HIGH);
151186
digitalWrite(LEDR, HIGH);
152-
delay(2); // Short delay to ensure state change is registered.
187+
delay(2); // Brief delay to ensure state change.
188+
}
189+
190+
// Sequentially blinks the RGB LED in a set sequence.
191+
void ToggleRGB() {
192+
Serial.println("Toggling through RGB sequence.");
193+
194+
// Blink Red
195+
digitalWrite(LEDR, LOW);
196+
delay(commandDelay);
197+
TurnOffLED();
198+
delay(commandDelay);
199+
200+
// Blink Green
201+
digitalWrite(LEDG, LOW);
202+
delay(commandDelay);
203+
TurnOffLED();
204+
delay(commandDelay);
205+
206+
// Blink Blue
207+
digitalWrite(LEDB, LOW);
208+
delay(commandDelay);
209+
TurnOffLED();
210+
delay(commandDelay);
153211
}

0 commit comments

Comments
 (0)