|
| 1 | +/* |
| 2 | + Arduino Giga WiFi – RGB LED Demonstration |
| 3 | +
|
| 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. |
| 6 | +
|
| 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. |
| 8 | +
|
| 9 | + Board Compatibility: |
| 10 | + * Arduino Giga R1 WiFi (only) connected via USB-C. |
| 11 | +
|
| 12 | + Features Demonstrated: |
| 13 | + * Direct control of RGB LED pins |
| 14 | + * Simple timing with delay() |
| 15 | + * Sequential color transitions |
| 16 | +
|
| 17 | + 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. |
| 25 | +
|
| 26 | + 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. |
| 30 | +
|
| 31 | + Created: 09 April 2025 |
| 32 | + Author: Ahmed Sharaf (Tech Forge International LLC) |
| 33 | + License: MIT |
| 34 | +*/ |
| 35 | + |
| 36 | +// 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). |
| 40 | + |
| 41 | +// Function declarations |
| 42 | +void processCommand(String command); |
| 43 | +void TurnOn(int led, String ledName); |
| 44 | +void BlinkLED(int led, String ledName); |
| 45 | +void TurnOffLED(); |
| 46 | + |
| 47 | +void setup() { |
| 48 | + // Initialize Serial communication at 115200 baud. |
| 49 | + Serial.begin(115200); |
| 50 | + |
| 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); |
| 60 | + |
| 61 | + // Turn off all LEDs at startup. |
| 62 | + TurnOffLED(); |
| 63 | + |
| 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("---------------------------------------------------------"); |
| 78 | +} |
| 79 | + |
| 80 | +void loop() { |
| 81 | + // Read incoming serial data character-by-character. |
| 82 | + while (Serial.available()) { |
| 83 | + char c = Serial.read(); |
| 84 | + // Append characters to command buffer; ignore newline and comma delimiters. |
| 85 | + if (c != '\n' && c != ',') { |
| 86 | + inputCommand += c; |
| 87 | + } |
| 88 | + delay(2); // Short delay to ensure complete data reception. |
| 89 | + } |
| 90 | + |
| 91 | + // If a command was received, process it. |
| 92 | + if (inputCommand.length() > 0) { |
| 93 | + inputCommand.trim(); // Remove any leading/trailing whitespace. |
| 94 | + processCommand(inputCommand); |
| 95 | + inputCommand = ""; // Clear the command buffer. |
| 96 | + Serial.flush(); // Clear any leftover data in the Serial buffer. |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Processes the received command and triggers the corresponding action. |
| 101 | +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") { |
| 111 | + TurnOn(LEDR, "Red"); |
| 112 | + } else if (command == "LEDoff") { |
| 113 | + 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."); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 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."); |
| 132 | +} |
| 133 | + |
| 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. |
| 141 | + delay(commandDelay); |
| 142 | + digitalWrite(led, HIGH); // Turn LED OFF. |
| 143 | + delay(commandDelay); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// Turns off all color channels of the integrated RGB LED; assumes active LOW. |
| 148 | +void TurnOffLED() { |
| 149 | + digitalWrite(LEDB, HIGH); |
| 150 | + digitalWrite(LEDG, HIGH); |
| 151 | + digitalWrite(LEDR, HIGH); |
| 152 | + delay(2); // Short delay to ensure state change is registered. |
| 153 | +} |
0 commit comments