LED Light Bar


Breadboard



Shematic




Code


  1. void setup() {
  2. for (int i = 2; i < 9; i++) { // Choose pins 2-8
  3. pinMode(i, OUTPUT); // Set the pins as outputs
  4. }
  5. }
  6. // Define function to turn off all LEDs at the same time
  7. void allLEDsOff(void) {
  8. for (int i = 2; i < 9; i++) {
  9. digitalWrite(i, LOW);
  10. }
  11. }
  12. // Switch on LEDs in sequence from left to right
  13. void loop() {
  14. for (int i = 2; i < 8; i++) { // Run loop once for each LED
  15. allLEDsOff(); // Turn off all LEDs
  16. digitalWrite(i, HIGH); // Turn on current LED
  17. delay(200); // Delay of 200 ms,
  18. // then repeat loop to move on to next LED
  19. }
  20. for (int i = 8; i > 2; i--) { // Light LEDs from right to left
  21. allLEDsOff();
  22. digitalWrite(i, HIGH);
  23. delay(200);
  24. }
  25. }