Skip to content

Instantly share code, notes, and snippets.

@tj-devel709
Last active March 8, 2020 21:47
Show Gist options
  • Save tj-devel709/b1da1d1893cc895cce71a86a3f39805a to your computer and use it in GitHub Desktop.
Save tj-devel709/b1da1d1893cc895cce71a86a3f39805a to your computer and use it in GitHub Desktop.
Start of Remote
#include <Servo.h> //servo library
Servo myservo;
#include <IRremote.h>// create servo object to control servo
int Echo = A4;
int Trig = A5;
#define FORWARD 16736925
//#define JUNK1 4294967295
//#define JUNK2 3343195340
#define ONE 16738455
#define BACK 16754775
#define LEFT 16720605
#define RIGHT 16761405
#define STOP 16712445
#define RECV_PIN 12
#define LED 13 //define LED pin
#define L 16738455
#define UNKNOWN_L 1386468383
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define carSpeed 250
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long val;
unsigned long preMillis;
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
bool isAutonomous = false;
int lastpressed = -1;
String str;
void forward(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Forward");
}
void back() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("Back");
}
void left() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Left");
}
void right() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("Right");
}
void pause() {
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
// Serial.println("Pause!");
}
//Ultrasonic distance measurement Sub function
int Distance_test() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance / 58;
return (int)Fdistance;
}
void setup() {
myservo.attach(3,700,2400); // attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pause();
irrecv.enableIRIn();
}
//void infrared(){
// if (irrecv.decode(&results)){
// preMillis = millis();
// val = results.value;
// Serial.println(val);
// irrecv.resume();
// switch(val){
// case FORWARD: forward(); break;
// case BACK: back(); break;
// case LEFT: left(); break;
// case RIGHT: right(); break;
// case STOP: isAutonomous = true; break;
// default: break;
// }
// }
// else{
// if(millis() - preMillis > 500){
// pause();
// preMillis = millis();
// }
// }
//}
void loop() {
if (irrecv.decode(&results)){
preMillis = millis();
val = results.value;
//str = "Result: " + val;
// Serial.print("Result : ");
Serial.println(val);
irrecv.resume();
switch(val){
case FORWARD:isAutonomous = false; lastpressed = 0; forward(); break;
case BACK: isAutonomous = false; lastpressed = 1; back(); break;
case LEFT: isAutonomous = false; lastpressed = 2; left(); break;
case RIGHT: isAutonomous = false; lastpressed = 3; right(); break;
case ONE: isAutonomous = true; lastpressed = 4; ObstacleAvoidance(); break;
case STOP: lastpressed = 5; pause(); break;
// default:
// Serial.println("In Default");
// if (lastpressed == 0 || lastpressed == 1 || lastpressed == 2 || lastpressed == 3)
// {
// pause();
// Serial.println("Pausing in Default");
// };
// break;
// }
}
}
else{
if(millis() - preMillis > 500){
// pause();
preMillis = millis();
}
}
switch(lastpressed){
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: ObstacleAvoidance(); break;
case -1: break;
}
// if (isAutonomous){
// ObstacleAvoidance();
// }
// else {
// infrared();
// }
}
void ObstacleAvoidance () {
// Serial.println("Entering obstacleAvoidance");
myservo.write(90); //setservo position according to scaled value
delay(500);
middleDistance = Distance_test();
if(middleDistance <= 40) {
pause();
delay(500);
myservo.write(10);
delay(1000);
rightDistance = Distance_test();
delay(500);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
leftDistance = Distance_test();
delay(500);
myservo.write(90);
delay(1000);
if(rightDistance > leftDistance) {
right();
delay(400);
pause();
}
else if(rightDistance < leftDistance) {
left();
delay(400);
pause();
}
else if((rightDistance <= 40) || (leftDistance <= 40)) {
back();
delay(180);
}
else {
forward();
}
}
else {
forward();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment