Sunday, October 1, 2017

Day 2 - Lab: Knight Rider LEDs

OVERVIEW:
Today, we set up our Arduino by downloading the necessary software to our computers. We also wrote our first computer program in the Arduino software. Our task was to make an LED blink on and off. For homework, we have to figure out how to wire multiple LEDs to our breadboard and make them interact with the Arduino. More specifically, we have to make our LEDs blink similar to the way the KITT car from Knight Rider blinks. Here is a preview of the materials we used:

Note: Those are 1k ohm resistors


IN CLASS LAB:
The lab we did in class was relatively simple. All we had to do was wire a single LED to our breadboard and make it blink using the Arduino. This is the sample code we analyzed and compiled for the in-class lab:

void setup() { 
pinMode(10,OUTPUT); //Initialize Pin 

void loop() { 
digitalWrite(10,HIGH); //Set the LED On 
delay(1000); //Wait for 1000 ms 
digitalWrite(10,LOW); //Set the LED Off 
delay(1000); //Wait for 1 second 

LED wired to Arduino

LAB HOMEWORK:
In class, we learned how to wire one LED to a breadboard. Our task for homework was to wire several LEDs to a breadboard and see if we could light them up like the KITT car. A quick Google search revealed that the way the LEDs blinked and moved on the show was much more complicated than I had imagined. At first, I just tried getting the LEDs to move back and forth in a straight line.

Here is the corresponding LED circuit in action with the code uploaded to the Arduino:

This worked out alright, but it wasn't exactly what I was hoping for. With this code, the LEDs only turned on and off sequentially. However, in the TV show, the car had several lights on at a single time with varying brightness around the "center point". In other words, there were always other LEDs leading and following the brightest one in the sequence. With this in mind, I began to write a modified version of the aforementioned code that would incorporate these "trailing" LEDs.




I liked this way much better, so I decided to keep it for the next class. In hindsight, I think I could have made my code much neater and less repetitive by making my LED pin number a variable and using it as a counter in a few for() statements. For example, the Blinking_LED_2 code from above could have been rewritten as the following:

int LEDpin = 10;
int LEDonTime = 200;
int LEDoffTime = 200;

void setup() {
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}

void loop() {
for (LEDpin=10; LEDpin>=5; k--) {
digitalWrite(LEDpin, HIGH);
delay(LEDonTime);
}

for (LEDpin=10; LEDpin>=7; k--) {
digitalWrite(LEDpin, LOW);
delay(LEDoffTime);
}

for (LEDpin=7; LEDpin<=10; k++) {
digitalWrite(LEDpin, HIGH);
delay(LEDonTime);
}

for (LEDpin=5; LEDpin<=8; k++) {
digitalWrite(LEDpin, LOW);
delay(LEDoffTime);
}
}

I was curious to see if this code would actually work, so I decided to upload it to the Arduino.


Here is a video clip of the modified code in action. The results are very similar to the original.

As you can see, the results of this modified code are almost exactly the same as its previous iteration. Therefore, my modified code is definitely more efficient than the old one.







No comments:

Post a Comment