Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code defines a 1x5 LED matrix with 5 LEDs connected to pins 2, 3, 4, 5, and 6 on the Arduino board. The LED colors are defined using an enumeration, and the matrix array stores the color for each LED. The setColor function sets the color of the LED based on the value provided.
- Please note that this example assumes you are using single-color LEDs with different resistors to represent Red, Green, and Yellow. If you are using RGB LEDs or a different setup, you would need to modify the code accordingly.
- Also, make sure to connect the LEDs to the appropriate pins on the Arduino board and use appropriate resistors to limit the current flowing through them.
- */
- // Include the Arduino library
- #include <Arduino.h>
- // Define LED pins
- const int ledPins[] = {2, 3, 4, 5, 6};
- const int numLeds = 5;
- // Define LED colors
- enum Color {RED, GREEN, YELLOW};
- // Define the 1x5 LED matrix
- Color matrix[] = {GREEN, YELLOW, YELLOW, YELLOW, RED};
- void setup() {
- // Set LED pins as outputs
- for (int i = 0; i < numLeds; i++) {
- pinMode(ledPins[i], OUTPUT);
- }
- }
- void loop() {
- // Iterate through the matrix and turn on the corresponding LEDs
- for (int i = 0; i < numLeds; i++) {
- setColor(ledPins[i], matrix[i]);
- delay(1000); // Wait for 1 second (1000 milliseconds)
- digitalWrite(ledPins[i], LOW); // Turn off the LED
- }
- }
- void setColor(int pin, Color color) {
- switch (color) {
- case RED:
- analogWrite(pin, 255); // Full intensity for Red
- break;
- case GREEN:
- analogWrite(pin, 127); // Half intensity for Green
- break;
- case YELLOW:
- digitalWrite(pin, HIGH); // Turn on the LED with Yellow color
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement