Program:
#include <IRremote.h> int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results; void setup() { Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver pinMode(2,OUTPUT); // kendali motor 1 pinMode(3,OUTPUT); // motor 1 + pinMode(4,OUTPUT); // motor 1 - pinMode(5,OUTPUT); // kendali motor 2 pinMode(6,OUTPUT); // motor 2 + pinMode(7,OUTPUT); // motor 2 - } void kiri(void){ digitalWrite(2,LOW); digitalWrite(5,LOW); delayMicroseconds(100); digitalWrite(3,HIGH); digitalWrite(4,LOW); delayMicroseconds(100); digitalWrite(6,LOW); digitalWrite(7,HIGH); delayMicroseconds(100); digitalWrite(2,HIGH); digitalWrite(5,HIGH); delayMicroseconds(200); } void kanan(void){ digitalWrite(2,LOW); digitalWrite(5,LOW); delayMicroseconds(100); digitalWrite(3,LOW); digitalWrite(4,HIGH); delayMicroseconds(100); digitalWrite(6,HIGH); digitalWrite(7,LOW); delayMicroseconds(100); digitalWrite(2,HIGH); digitalWrite(5,HIGH); delayMicroseconds(200); } void maju(void){ digitalWrite(2,LOW); digitalWrite(5,LOW); delayMicroseconds(100); digitalWrite(3,LOW); digitalWrite(4,HIGH); delayMicroseconds(100); digitalWrite(6,LOW); digitalWrite(7,HIGH); delayMicroseconds(100); digitalWrite(2,HIGH); digitalWrite(5,HIGH); delayMicroseconds(200); } void mundur(void){ digitalWrite(3,HIGH); digitalWrite(4,LOW); delayMicroseconds(100); digitalWrite(6,HIGH); digitalWrite(7,LOW); delayMicroseconds(100); digitalWrite(2,HIGH); digitalWrite(5,HIGH); delayMicroseconds(200); } void henti(void){ // berhenti digitalWrite(3,LOW); digitalWrite(4,LOW); delayMicroseconds(100); digitalWrite(6,LOW); digitalWrite(7,LOW); delayMicroseconds(100); digitalWrite(2,LOW); digitalWrite(5,LOW); delayMicroseconds(200); } void loop() { int sensor,sensor2; if (irrecv.decode(&results)) { Serial.println(results.value); if((results.value)== 520||(results.value)== 2568){ maju(); delay(10); } if((results.value)== 536||(results.value)== 2584){ kiri(); delay(10); } if((results.value)== 546||(results.value)== 2594){ henti(); delay(10); } if((results.value)== 528||(results.value)== 2576){ kanan(); delay(10); } if((results.value)== 516||(results.value)== 2564){ mundur(); delay(10); } irrecv.resume(); // Receive the next value } /* int sensor,sensor2; baca nilai sensor di PIN A0 (Analog) dan kirim ke serial*/ sensor=analogRead(A0);//kanan sensor2=analogRead(A1); //kiri if(sensor<=900 || sensor2<=900){ if(sensor<sensor2){ kiri();} delay(200); if(sensor2<sensor){ kanan();} delay(200); } }
Check this video
Tidak ada komentar:
Posting Komentar