Created
October 3, 2016 20:56
-
-
Save romank0/782d58dd07c60885f34e56e46fa3e031 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Timer.h> | |
#include <IRremote.h> | |
#include <Servo.h> | |
#define irPin 5 | |
#define motorASpeedPin 10 | |
#define motorAIn1Pin 9 | |
#define motorAIn2Pin 8 | |
#define servoPin 6 | |
#define echoPin 11 | |
#define trigPin 12 | |
#define buzzerPin 13 | |
#define FORWARD HIGH | |
#define BACKWARD LOW | |
IRrecv irrecv(irPin); | |
decode_results results; | |
Timer t; | |
#define maximumRange 200 | |
#define minimumRange 2 | |
#define stopRange 25 | |
long duration, distance; | |
int motorACurrentDir = 0; | |
int motorACurrentSpeed = 0; | |
Servo steerServo; | |
#define LOG(x) Serial.print(x) | |
#define LOGLN(x) Serial.println(x) | |
#define LOGVAL(message, val) LOG(message); LOG(val); | |
#define LOGVALLN(message, val) LOG(message); LOGLN(val); | |
enum command { | |
MOVE_FORWARD, | |
MOVE_BACKWARD, | |
STRAIGHT, | |
LEFT, | |
RIGHT, | |
STOP, | |
FULL_SPEED, | |
ACCELERATE, | |
DECELERATE | |
}; | |
// buttons | |
#define up_button 0xFF629D | |
#define down_button 0xFFA857 | |
#define green_button 0xFF22DD | |
#define blue_button 0xFFC23D | |
#define three_button 0xFFB04F | |
#define one_button 0xFF6897 | |
#define two_button 0xFF9867 | |
#define star_button 0xFF42BD | |
#define ok_button 0xFF02FD | |
#define left_button 0xFF22DD | |
#define right_button 0xFFC23D | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(motorASpeedPin, OUTPUT); | |
pinMode(motorAIn1Pin, OUTPUT); | |
pinMode(motorAIn2Pin, OUTPUT); | |
irrecv.enableIRIn(); // Start the receiver | |
steerServo.attach(servoPin); | |
steer(90); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
t.every(1000, check_obstacle); | |
} | |
void set_speed(int speed) { | |
if (speed < 0) { | |
speed = 0; | |
} | |
if (speed > 255) { | |
speed = 255; | |
} | |
LOGVALLN("set_speed=", speed); | |
motorACurrentSpeed = speed; | |
analogWrite(motorASpeedPin, speed); | |
} | |
void rotate(int dir, int speed) { | |
LOGVAL("rotate ", dir); | |
LOGVALLN(", ", speed); | |
if (motorACurrentDir != dir) { | |
stop(); | |
} | |
digitalWrite(motorAIn2Pin, 1-dir); | |
digitalWrite(motorAIn1Pin, dir); | |
motorACurrentDir = dir; | |
set_speed(speed); | |
} | |
void stop() { | |
digitalWrite(motorAIn1Pin, LOW); | |
digitalWrite(motorAIn2Pin, LOW); | |
set_speed(0); | |
delay(400); | |
} | |
void steer(int angle) { | |
int cur_speed = motorACurrentSpeed; | |
stop(); | |
steerServo.write(angle); | |
rotate(motorACurrentDir, cur_speed); | |
} | |
command get_command(int button) { | |
LOG("button="); | |
Serial.println(results.value, HEX); | |
switch(button) { | |
case one_button: | |
return DECELERATE; | |
case two_button: | |
return ACCELERATE; | |
case three_button: | |
return FULL_SPEED; | |
case up_button: | |
return MOVE_FORWARD; | |
case down_button: | |
return MOVE_BACKWARD; | |
case star_button: | |
return STOP; | |
case ok_button: | |
return STRAIGHT; | |
case right_button: | |
return RIGHT; | |
case left_button: | |
return LEFT; | |
} | |
} | |
void on_command(command cmd) { | |
switch(cmd) { | |
case DECELERATE: | |
set_speed(motorACurrentSpeed - 20); | |
break; | |
case ACCELERATE: | |
set_speed(motorACurrentSpeed + 20); | |
break; | |
case FULL_SPEED: | |
set_speed(255); | |
break; | |
case MOVE_FORWARD: | |
rotate(FORWARD, 255); | |
break; | |
case MOVE_BACKWARD: | |
rotate(BACKWARD, 255); | |
break; | |
case STOP: | |
stop(); | |
break; | |
case STRAIGHT: | |
steer(90); | |
break; | |
case RIGHT: | |
steer(150); | |
break; | |
case LEFT: | |
steer(30); | |
break; | |
} | |
} | |
int moment_dist() { | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
//Calculate the distance (in cm) based on the speed of sound. | |
distance = duration/58.2; | |
if (distance >= maximumRange || distance <= minimumRange){ | |
distance = 1000; | |
} | |
return distance; | |
} | |
int dist_from_us() { | |
int d1 = moment_dist(); | |
int d2 = moment_dist(); | |
int d3 = moment_dist(); | |
if (d1 >= d2) { | |
distance = d2 >= d3 ? d2 : d3; | |
} else { | |
distance = d1 >= d3 ? d1 : d3; | |
} | |
LOGVAL("distance=", distance); | |
return distance; | |
} | |
void sound(int t, int t2) { | |
for (int j = 0; j < t; ++j) { | |
for (int i = 0; i <t2; i++) // Wen a frequency sound | |
{ | |
digitalWrite (buzzerPin, HIGH) ;// send voice | |
delay (1) ;// Delay 1ms | |
digitalWrite (buzzerPin, LOW) ;// do not send voice | |
delay (1) ;// delay ms | |
} | |
for (int i = 0; i <t2+20; i++) // Wen Qie out another frequency sound | |
{ | |
digitalWrite (buzzerPin, HIGH) ;// send voice | |
delay (2) ;// delay 2ms | |
digitalWrite (buzzerPin, LOW) ;// do not send voice | |
delay (2) ;// delay 2ms | |
} | |
} | |
} | |
boolean obstacle = false; | |
int speedBeforeStop = 0; | |
void check_obstacle() { | |
if (obstacle || (motorACurrentDir == FORWARD && motorACurrentSpeed > 0)) { | |
obstacle = dist_from_us() < stopRange; | |
} else { | |
obstacle = false; | |
} | |
} | |
void loop() { | |
t.update(); | |
if (obstacle && motorACurrentSpeed > 0) { | |
speedBeforeStop = motorACurrentSpeed; | |
stop(); | |
return; | |
} else { | |
if (speedBeforeStop > 0) { | |
rotate(FORWARD, speedBeforeStop); | |
speedBeforeStop = 0; | |
} | |
} | |
if (irrecv.decode(&results)) { | |
//sound(3, 80); | |
on_command(get_command(results.value)); | |
irrecv.resume(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment