Skip to content

Instantly share code, notes, and snippets.

@smching
Created February 28, 2016 09:36
Show Gist options
  • Save smching/e5585af1945d25383756 to your computer and use it in GitHub Desktop.
Save smching/e5585af1945d25383756 to your computer and use it in GitHub Desktop.
Wireless Router Remote Control Car
http://ediy.com.my/projects/item/99-wireless-router-remote-control-car
////////// blink LED variables
#define BLINK_LED_PIN 13 //the number of the LED pin
int ledState = LOW; //ledState used to set the LED
long previousMillis = 0; //will store last time LED was updated
long interval = 500; //interval at which to blink (milliseconds)
////////// motor variables
#define MOTOR_ENABLE_PWM_PIN 6 // H-bridge enable (pin 1 & pin 9)
#define LEFT_MOTOR_LOGIC1_PIN 7 // H-bridge leg 1 (pin 2, 1A)
#define LEFT_MOTOR_LOGIC2_PIN 8 // H-bridge leg 2 (pin 7, 2A)
#define RIGHT_MOTOR_LOGIC1_PIN 9 // H-bridge leg 1 (pin 10, 3A)
#define RIGHT_MOTOR_LOGIC2_PIN 10 // H-bridge leg 2 (pin 15, 4A)
#define CAR_HORN_PIN 2
#define HEADLIGHTS_PIN 3
#define INITIAL_SPEED 20
#define DELAY_BETWEEN_MOTOR_CHANGE_DIRECTION 200
int Current_speed, speeds = 0;
////////// use to determine car action
#define FORWARD 1
#define LEFT 2
#define STOP 3
#define RIGHT 4
#define BACKWARD 5
byte car_action, previous_car_action = STOP;
////////// serial variables
boolean Rxenable = false;
String inputString = ""; //a string to hold incoming data
boolean stringComplete = false; //whether the string is complete
void setup()
{
// set the digital pin as output:
pinMode(BLINK_LED_PIN, OUTPUT);
pinMode(LEFT_MOTOR_LOGIC1_PIN, OUTPUT);
pinMode(LEFT_MOTOR_LOGIC2_PIN, OUTPUT);
pinMode(MOTOR_ENABLE_PWM_PIN, OUTPUT);
pinMode(RIGHT_MOTOR_LOGIC1_PIN, OUTPUT);
pinMode(RIGHT_MOTOR_LOGIC2_PIN, OUTPUT);
pinMode(CAR_HORN_PIN, OUTPUT);
pinMode(HEADLIGHTS_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Wifi RC car");
car_stop();
}
void loop()
{
blinkLED(); //blink LED every second
if (stringComplete) {
inputString.toUpperCase();
process_command();
inputString = ""; //clear the string:
stringComplete = false;
Rxenable = false;
}
}//end loop
void process_command()
{
char command=inputString[1];
switch (command) {
case '2':
car_forward();
break;
case '3':
car_headLight();
break;
case '4':
car_left();
break;
case '5':
car_stop();
break;
case '6':
car_right();
break;
case '7':
car_horn();
break;
case '8':
car_backward();
break;
case 'S':
//extract from first character until end
//eg. if inputString=@S20 then speeds=20
speeds = stringToInt(inputString.substring(2));
car_speed();
break;
}
}
void load_car_speed()
{
//if the car is not moving then run the car with INITIAL_SPEED
if (previous_car_action == STOP) {
speeds = INITIAL_SPEED;
car_speed();
}
if ((car_action == FORWARD && previous_car_action == BACKWARD)
|| (car_action == BACKWARD && previous_car_action == FORWARD)
|| (car_action == LEFT && previous_car_action == RIGHT)
|| (car_action == RIGHT && previous_car_action == LEFT))
{
leftMotor_stop();
rightMotor_stop();
delay(DELAY_BETWEEN_MOTOR_CHANGE_DIRECTION);
}
previous_car_action = car_action;
}
void car_stop()
{
car_action = STOP;
leftMotor_stop();
rightMotor_stop();
}
void car_forward()
{
car_action = FORWARD;
load_car_speed();
leftMotor_run();
rightMotor_run();
}
void car_backward()
{
car_action = BACKWARD;
load_car_speed();
leftMotor_backward();
rightMotor_backward();
}
void car_left()
{
car_action = LEFT;
load_car_speed();
rightMotor_run();
leftMotor_stop();
}
void car_right()
{
car_action = RIGHT;
load_car_speed();
leftMotor_run();
rightMotor_stop();
}
void car_horn()
{
// Serial.print("Horn");
if (digitalRead(CAR_HORN_PIN)==LOW) {
digitalWrite(CAR_HORN_PIN, HIGH);
} else {
digitalWrite(CAR_HORN_PIN, LOW);
}
}
void car_headLight()
{
// Serial.print("Head Light");
if (digitalRead(HEADLIGHTS_PIN)==LOW) {
digitalWrite(HEADLIGHTS_PIN, HIGH);
} else {
digitalWrite(HEADLIGHTS_PIN, LOW);
}
}
void car_speed()
{
Current_speed = speeds;
int speeds_mapped = map(Current_speed, 0, 100, 80, 255);
analogWrite(MOTOR_ENABLE_PWM_PIN, speeds_mapped);
}
//////////////////////// leftt motor
void leftMotor_run()
{
digitalWrite(LEFT_MOTOR_LOGIC1_PIN, LOW);
digitalWrite(LEFT_MOTOR_LOGIC2_PIN, HIGH);
}
void leftMotor_backward()
{
digitalWrite(LEFT_MOTOR_LOGIC1_PIN, HIGH);
digitalWrite(LEFT_MOTOR_LOGIC2_PIN, LOW);
}
void leftMotor_stop()
{
digitalWrite(LEFT_MOTOR_LOGIC1_PIN, LOW);
digitalWrite(LEFT_MOTOR_LOGIC2_PIN, LOW);
}
//////////////////////// right motor
void rightMotor_run()
{
digitalWrite(RIGHT_MOTOR_LOGIC1_PIN, LOW);
digitalWrite(RIGHT_MOTOR_LOGIC2_PIN, HIGH);
}
void rightMotor_backward()
{
digitalWrite(RIGHT_MOTOR_LOGIC1_PIN, HIGH);
digitalWrite(RIGHT_MOTOR_LOGIC2_PIN, LOW);
}
void rightMotor_stop()
{
digitalWrite(RIGHT_MOTOR_LOGIC1_PIN, LOW);
digitalWrite(RIGHT_MOTOR_LOGIC2_PIN, LOW);
}
////////// convert string to integer
int stringToInt(String str)
{
char char_string[str.length()+1];
str.toCharArray(char_string, str.length()+1);
return atoi(char_string);
}
////////// blink LED without delay
void blinkLED()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(BLINK_LED_PIN, ledState);
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read(); // get the new byte:
if (inChar == '@') { // Can start recording after @ symbol
Rxenable = true;
}
if (Rxenable==true) {
inputString += inChar; // add it to the inputString:
// if the incoming character is a carriage return, set a flag
// so the main loop can do something about it:
if (inChar == '\r') {
stringComplete = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment