Advertisement
microrobotics

1x5 Matrix Array 5Key Red Green Yellow

Apr 26th, 2023 (edited)
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. 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.
  5.  
  6. 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.
  7. */
  8.  
  9. // Include the Arduino library
  10. #include <Arduino.h>
  11.  
  12. // Define LED pins
  13. const int ledPins[] = {2, 3, 4, 5, 6};
  14. const int numLeds = 5;
  15.  
  16. // Define LED colors
  17. enum Color {RED, GREEN, YELLOW};
  18.  
  19. // Define the 1x5 LED matrix
  20. Color matrix[] = {GREEN, YELLOW, YELLOW, YELLOW, RED};
  21.  
  22. void setup() {
  23.   // Set LED pins as outputs
  24.   for (int i = 0; i < numLeds; i++) {
  25.     pinMode(ledPins[i], OUTPUT);
  26.   }
  27. }
  28.  
  29. void loop() {
  30.   // Iterate through the matrix and turn on the corresponding LEDs
  31.   for (int i = 0; i < numLeds; i++) {
  32.     setColor(ledPins[i], matrix[i]);
  33.     delay(1000); // Wait for 1 second (1000 milliseconds)
  34.     digitalWrite(ledPins[i], LOW); // Turn off the LED
  35.   }
  36. }
  37.  
  38. void setColor(int pin, Color color) {
  39.   switch (color) {
  40.     case RED:
  41.       analogWrite(pin, 255); // Full intensity for Red
  42.       break;
  43.     case GREEN:
  44.       analogWrite(pin, 127); // Half intensity for Green
  45.       break;
  46.     case YELLOW:
  47.       digitalWrite(pin, HIGH); // Turn on the LED with Yellow color
  48.       break;
  49.   }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement