Advertisement
microrobotics

8-segment Bar Graph LED

Apr 17th, 2023
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This code sets the LED pins as outputs using the pinMode() function in the setup() function. In the loop() function, each LED segment is turned on in sequence using the digitalWrite() function with a delay between each segment.
  3.  
  4. After all segments are turned on, they are turned off in sequence with the same delay using digitalWrite(). This sequence repeats indefinitely.
  5.  
  6. Note that the specific pinout and segment mapping may vary depending on the manufacturer of your Bar Graph LED. You should refer to the datasheet or product documentation for the correct pinout and segment mapping for your device.
  7. */
  8.  
  9. // define the LED pins
  10. const int LED_A = 2;
  11. const int LED_B = 3;
  12. const int LED_C = 4;
  13. const int LED_D = 5;
  14. const int LED_E = 6;
  15. const int LED_F = 7;
  16. const int LED_G = 8;
  17. const int LED_DP = 9;
  18.  
  19. // define the delay time
  20. const int DELAY_TIME = 100;
  21.  
  22. void setup() {
  23.   // set the LED pins as outputs
  24.   pinMode(LED_A, OUTPUT);
  25.   pinMode(LED_B, OUTPUT);
  26.   pinMode(LED_C, OUTPUT);
  27.   pinMode(LED_D, OUTPUT);
  28.   pinMode(LED_E, OUTPUT);
  29.   pinMode(LED_F, OUTPUT);
  30.   pinMode(LED_G, OUTPUT);
  31.   pinMode(LED_DP, OUTPUT);
  32.  
  33.   // initialize the serial port for debugging
  34.   Serial.begin(9600);
  35.  
  36.   // print a message to the serial port
  37.   Serial.println("Bar Graph LED ready!");
  38. }
  39.  
  40. void loop() {
  41.   // turn on segment A
  42.   digitalWrite(LED_A, HIGH);
  43.   delay(DELAY_TIME);
  44.  
  45.   // turn on segment B
  46.   digitalWrite(LED_B, HIGH);
  47.   delay(DELAY_TIME);
  48.  
  49.   // turn on segment C
  50.   digitalWrite(LED_C, HIGH);
  51.   delay(DELAY_TIME);
  52.  
  53.   // turn on segment D
  54.   digitalWrite(LED_D, HIGH);
  55.   delay(DELAY_TIME);
  56.  
  57.   // turn on segment E
  58.   digitalWrite(LED_E, HIGH);
  59.   delay(DELAY_TIME);
  60.  
  61.   // turn on segment F
  62.   digitalWrite(LED_F, HIGH);
  63.   delay(DELAY_TIME);
  64.  
  65.   // turn on segment G
  66.   digitalWrite(LED_G, HIGH);
  67.   delay(DELAY_TIME);
  68.  
  69.   // turn on the decimal point
  70.   digitalWrite(LED_DP, HIGH);
  71.   delay(DELAY_TIME);
  72.  
  73.   // turn off all segments
  74.   digitalWrite(LED_A, LOW);
  75.   digitalWrite(LED_B, LOW);
  76.   digitalWrite(LED_C, LOW);
  77.   digitalWrite(LED_D, LOW);
  78.   digitalWrite(LED_E, LOW);
  79.   digitalWrite(LED_F, LOW);
  80.   digitalWrite(LED_G, LOW);
  81.   digitalWrite(LED_DP, LOW);
  82.   delay(DELAY_TIME);
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement