Knight Rider dengan Arduino

Arduino sudah merebak sebagai salah satu microprosesor untuk elektronika praktis. Berbeda dengan Raspberry yang lebih kompleks. Arduino cukup menjadi dasar untuk pembelajaran elektronika praktis. Sudah cukup lama meninggalkan kegiatan ini setelah lulus kuliah.

Setelah membeli modul Arduino Uno (kw), percobaan pertama pun dimulai. Cara termudah melakukan pengetesan adalah dengan menggunakan LED. Dengan melihat rujukan contoh program untuk LED dan sedikit modifikasi maka diperoleh lampu Knight Rider. Lampu mobil yang pernah jadi salah satu serial favorit di jaman baheula. Dengan menggunakan 7 input dari keluaran Arduino yang difungsikan sebagai output digital diperoleh lampu LED berjalan.

Pemrogramannya pun cukup mudah. Belum menggunakan iterasi untuk pengulangan programnya. Cukup menuliskan per line urutan jalannya LED. Untuk memudahkan pemula mendayagunakan Arduino miliknya.

Berikut adalah contoh program yang ditulis untuk LED Knight Rider.

//Example 01 : Blinking LED

const int LED0 = 13; // LED connected to digital pin 13
const int LED1 = 12; // LED connected to digital pin 12
const int LED2 = 11; // LED connected to digital pin 11
const int LED3 = 10; // LED connected to digital pin 10
const int LED4 = 9; // LED connected to digital pin 9
const int LED5 = 8; // LED connected to digital pin 8
const int LED6 = 7; // LED connected to digital pin 7
int state = 0;

void setup() {
pinMode(LED0, OUTPUT); // sets the digital pin as output
pinMode(LED1, OUTPUT); // sets the digital pin as output
pinMode(LED2, OUTPUT); // sets the digital pin as output
pinMode(LED3, OUTPUT); // sets the digital pin as output
pinMode(LED4, OUTPUT); // sets the digital pin as output
pinMode(LED5, OUTPUT); // sets the digital pin as output
pinMode(LED6, OUTPUT); // sets the digital pin as output
// put your setup code here, to run once:

}

void loop()
{
if(state==0) {
digitalWrite(LED0, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED0,LOW); // turns the LED off
state = state + 1;
}

if(state==1) {
digitalWrite(LED1, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED1,LOW); // turns the LED off
state = state + 1;
}

if(state==2) {
digitalWrite(LED2, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED2,LOW); // turns the LED off
state = state + 1;
}

if(state==3) {
digitalWrite(LED3, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED3,LOW); // turns the LED off
state = state + 1;
}

if(state==4) {
digitalWrite(LED4, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED4,LOW); // turns the LED off
state = state + 1;
}

if(state==5) {
digitalWrite(LED5, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED5,LOW); // turns the LED off
state = state + 1;
}

if(state==6) {
digitalWrite(LED6, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED6,LOW); // turns the LED off
state = state – 1;
}

if(state==5) {
digitalWrite(LED5, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED5,LOW); // turns the LED off
state = state – 1;
}

if(state==4) {
digitalWrite(LED4, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED4,LOW); // turns the LED off
state = state – 1;
}

if(state==3) {
digitalWrite(LED3, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED3,LOW); // turns the LED off
state = state – 1;
}

if(state==2) {
digitalWrite(LED2, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED2,LOW); // turns the LED off
state = state – 1;
}

if(state==1) {
digitalWrite(LED1, HIGH); //turns the LED on
delay(150); //waits for a 150 msecond
digitalWrite(LED1,LOW); // turns the LED off
state = state – 1;
}

}

Material yang digunakan pun cukup sederhana :

  • LED 3 mm warna (biru, kuning)
  • kabel jumper
  • Arduino Uno
  • Downloader

Jangan lupa menghubungkan kaki anoda (positif) LED pada masing-masing pin digital Arduino.

Selamat mencoba…

FYI, untuk versi loop-nya saya cek di example ternyata sudah ada… Padahal baru mau ubah untuk versi loop. Ihik..

Please check seperti yang di bawah ini. Sudah saya ubah sesuai 7 LED yang digunakan.

/*
Arrays

Demonstrates the use of an array to hold pin numbers
in order to iterate over the pins in a sequence.
Lights multiple LEDs in sequence, then in reverse.

Unlike the For Loop tutorial, where the pins have to be
contiguous, here the pins can be in any random order.

The circuit:
* LEDs from pins 2 through 7 to ground

created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Array
*/

int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
13, 12, 11, 10, 9, 8, 7
}; // an array of pin numbers to which LEDs are attached
int pinCount = 7; // the number of pins (i.e. the length of the array)

void setup() {
// the array elements are numbered from 0 to (pinCount – 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);

}

// loop from the highest pin to the lowest:
for (int thisPin = pinCount – 1; thisPin >= 0; thisPin–) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s