Skip to content

Instantly share code, notes, and snippets.

@cowdinosaur
Created April 12, 2019 07:18
Show Gist options
  • Save cowdinosaur/a12e3ecedb0164392ee0ec3a85b8c787 to your computer and use it in GitHub Desktop.
Save cowdinosaur/a12e3ecedb0164392ee0ec3a85b8c787 to your computer and use it in GitHub Desktop.
#include<IRremote.h> //including infrared remote header file
#define button_2 34935 //code for button 1
#define button_4 10455 //code for button 2
#define button_5 43095 //code for button 3
#define button_6 26775 //code for button 4
#define button_8 39015 //code for button 4
int RECV_PIN = 9; //IR receiver pin
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned int value;
int speedPin_M1 = 5; //M1 Speed Control
int speedPin_M2 = 6; //M2 Speed Control
int directionPin_M1 = 4; //M1 Direction Control
int directionPin_M2 = 7; //M1 Direction Control
void setup() {
Serial.begin(9600);
pinMode(speedPin_M1, OUTPUT);
pinMode(speedPin_M2, OUTPUT);
pinMode(directionPin_M1, OUTPUT);
pinMode(directionPin_M2, OUTPUT);
irrecv.enableIRIn(); //
}
void loop() {
if (irrecv.decode(&results)) {
value = results.value;
if (value == button_2) {
carAdvance(255, 255);
} else if (value == button_4) {
carTurnLeft(255, 255);
} else if (value == button_5) {
carStop();
} else if (value == button_6) {
carTurnRight(255, 255);
} else if (value == button_8) {
carBack(255, 255);
}
irrecv.resume();
}
}
void carStop() { // Motor Stop
digitalWrite(speedPin_M2, 0);
digitalWrite(directionPin_M1, LOW);
digitalWrite(speedPin_M1, 0);
digitalWrite(directionPin_M2, LOW);
}
void carTurnLeft(int leftSpeed, int rightSpeed) { //Turn Left
analogWrite (speedPin_M2, leftSpeed); //PWM Speed Control
digitalWrite(directionPin_M1, LOW);
analogWrite (speedPin_M1, rightSpeed);
digitalWrite(directionPin_M2, HIGH);
}
void carTurnRight(int leftSpeed, int rightSpeed) { //Turn Right
analogWrite (speedPin_M2, leftSpeed);
digitalWrite(directionPin_M1, HIGH);
analogWrite (speedPin_M1, rightSpeed);
digitalWrite(directionPin_M2, LOW);
}
void carBack(int leftSpeed, int rightSpeed) { //Move backward
analogWrite (speedPin_M2, leftSpeed);
digitalWrite(directionPin_M1, LOW);
analogWrite (speedPin_M1, rightSpeed);
digitalWrite(directionPin_M2, LOW);
}
void carAdvance(int leftSpeed, int rightSpeed) { //Move forward
analogWrite (speedPin_M2, leftSpeed);
digitalWrite(directionPin_M1, HIGH);
analogWrite (speedPin_M1, rightSpeed);
digitalWrite(directionPin_M2, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment