Berikut ini adalah program robot line follower dengan bahasa C. Enjoy.
PROGRAM LINE FOLLOWER
#include<mega8535.h>
#include<delay.h>
unsigned
char sensor;
void maju ();
void kanan_dikit ();
void kanan_banyak ();
void kiri_dikit ();
void kiri_banyak ();
void kanan_banyaki ();
void kanan_banyak2 ();
void kanan_banting ();
void kiri_banyak2 ();
void kiri_banting ();
void
ada_garis ();
void
cabang ();
void
henti ();
void
main(void)
{
DDRD=DDRB=0xFF;
DDRA=0x00;
//----inisialisasi
pwm----//
TCCR1A=0xa3;
TCCR1B=0x0b;
TCNT1=0x0000;//000016
bit
OCR1A=0x03ff;
OCR1B=0x03FF;
TIFR=0;
//------------------------//
while (1)
{
// PORTA=0xff;//
tambahan, jangan dipakai dlam sebenarnya
sensor=PINA;
sensor&=0b11111111;
switch(sensor)
{
case 0b11100111: maju (); // maju lurus
break;
case 0b11100011: kanan_dikit (); // nyalakan kanan (dikit)
break;
case
0b11110001: kanan_banyak (); // nyalakan
kanan (banyak)
break;
case
0b11100001: kanan_banyaki (); //
nyalakan kanan (banyak)
break;
case
0b11111000: kanan_banyak2 (); //
nyalakan kanan (banyak)
break;
case
0b11110000: kanan_banting (); //
nyalakan kiri (banyak)
break;
case
0b11000111: kiri_dikit (); // nyalakan
kiri (dikit)
break;
case
0b10001111: kiri_banyak (); // nyalakan
kiri (banyak)
break;
case
0b00011111: kiri_banyak2 (); // nyalakan
kiri (banyak)
break;
case
0b00001111: kiri_banting (); // nyalakan
kiri (banyak)
break;
case
0b00000000: ada_garis (); // nyalakan
kiri (banyak)
break;
case 0b11000011: cabang (); // nyalakan kiri (banyak)
break;
case
0b11111111: henti();
break;//
pilih orientasi kanan
}
};
}
//---maju---//
void maju ()
{PORTB=0b00001010;
OCR1A=0x3ff;
//KANAN OCR1A
OCR1B=0x3ff;
}
//---kanan dikit---//
void kanan_dikit ()
{PORTB=0b00001010;
OCR1A=0x200;
OCR1B=0x2ee;
}
//---kanan banyak---//
void kanan_banyak ()
{PORTB=0b00001010;
OCR1A=0x100;
OCR1B=0x3ff;
}
void kanan_banyaki ()
{PORTB=0b00001010;
OCR1A=0x100;
OCR1B=0x3ff;
}
//---kanan banyak2---//
void kanan_banyak2 ()
{PORTB=0b00001001;
OCR1A=0x3ff;
OCR1B=0x3ff;
}
//---kanan banyak2---//
void kanan_banting ()
{PORTB=0b00001001;
OCR1A=0x3ff;
OCR1B=0x3ff;
}
//---kiri dikit---//
void kiri_dikit ()
{PORTB=0b00001010;
OCR1A=0x2ee;
OCR1B=0x200;
}
//---kiri
banyak---//
void
kiri_banyak ()
{PORTB=0b00001010;
OCR1A=0x2ee;
OCR1B=0x100;
}
void
kiri_banyak2 ()
{PORTB=0b00000110;
OCR1A=0x3ff;
OCR1B=0x3ff;
}
//---kanan banyak2---//
void kiri_banting ()
{PORTB=0b00000110;
OCR1A=0x3ff;
OCR1B=0x3ff;
}
//---ada garis---
void ada_garis ()
{PORTB=0b00001010;
OCR1A=0x03ff;
OCR1B=0x03ff;
}
//---cabang---//
void
cabang ()
{PORTB=0b00001010;
OCR1A=0x03ff;
OCR1B=0x0020;
}
//---henti
void
henti ()
{PORTB=0b00000101;
OCR1A=0x03ff;
//KANAN OCR1A
OCR1B=0x03ff;
Sign up here with your email
ConversionConversion EmoticonEmoticon