Arduino - FCB task

 



Here's an explanation of how I solved the problem of creating a Knight Rider-style LED pattern using an Arduino:

Define Variables and Pins: First, I defined the necessary variables and pin numbers. I used an array ledPins to store the pin numbers to which the LEDs are connected. In this case, I assumed 6 LEDs arranged in a line, and I assigned pins 2 to 7 for them. I also set a delayTime variable to control the speed of the LED animation.

Setup Function: In the setup() function, I used a for loop to set all the pins in the ledPins array as OUTPUT mode. This step is important to ensure that the Arduino knows the pins will be used to send signals to the LEDs.

Loop Function: The main part of the code is in the loop() function. This is where the Knight Rider LED animation is achieved.

Left to Right: In the first for loop, I iterated through each LED from left to right. For each LED, I turned it on by setting the corresponding pin to HIGH, waited for a short duration (delayTime), and then turned it off by setting the pin to LOW. This created the effect of the LEDs lighting up in sequence from left to right.

Right to Left: In the second for loop, I reversed the order of iteration and repeated the same process, but this time the LEDs lit up in reverse order from right to left.

Repeating Pattern: The loop() function continues to run indefinitely, which means the LED pattern will repeat continuously. The sequence of LEDs lighting up from left to right and then from right to left creates the classic Knight Rider-style animation.

Uploading and Running: After writing the code, you can upload it to your Arduino board using the Arduino IDE. Once uploaded, the code will run on the Arduino, and you'll see the LEDs blink in the desired Knight Rider pattern.

Post a Comment

Previous Post Next Post