Skip to content

Instantly share code, notes, and snippets.

@conoro
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conoro/8430ec8753317883c2b7 to your computer and use it in GitHub Desktop.
Save conoro/8430ec8753317883c2b7 to your computer and use it in GitHub Desktop.
RC Car Control Receiver with simple 433MHz modules
// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1, 1KHz pwm
AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2, 1KHz pwm
AF_DCMotor motor3(3, MOTOR34_1KHZ); // create motor #3, 1KHz pwm
AF_DCMotor motor4(4, MOTOR34_1KHZ); // create motor #4, 1KHz pwm
const int X_THRESHOLD_LOW = 505;
const int X_THRESHOLD_HIGH = 530;
const int Y_THRESHOLD_LOW = 500;
const int Y_THRESHOLD_HIGH = 510;
int x_position;
int y_position;
int x_direction;
int y_direction;
int current_x_direction;
int current_y_direction;
uint8_t motor1_speed;
uint8_t motor2_speed;
uint8_t motor3_speed;
uint8_t motor4_speed;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
motor1_speed = 255;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 255;
motor1.setSpeed(0); // set the speed to 200/255
motor2.setSpeed(0); // set the speed to 200/255
motor3.setSpeed(0); // set the speed to 200/255
motor4.setSpeed(0); // set the speed to 200/255
motor1.run(RELEASE); // turn it on going backwards
motor2.run(RELEASE); // turn it on going backwards
motor3.run(RELEASE); // turn it on going backwards
motor4.run(RELEASE); // turn it on going backwards
// Due to lack of access to Digital Pins with Motor Shield in place, using Analogue A0 (Arduino Pin 14) as Digital
vw_set_rx_pin(14);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
current_x_direction = 0;
current_y_direction = 0;
x_direction = 0;
y_direction = 0;
Serial.println("setup done");
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
int j;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.println("");
digitalWrite(13, false);
// Format is XnnnnYnnnnAnBnCnDn
// X is X position of joystick as 4 digits
// Y is Y position of joystick as 4 digits
// A is A button on joystick. 0= Not pressed. 1 = pressed
// B is B button on joystick. 0= Not pressed. 1 = pressed
// C is C button on joystick. 0= Not pressed. 1 = pressed
// D is D button on joystick. 0= Not pressed. 1 = pressed
//Serial.println("");
x_position = (1000 * (buf[1] - 48)) + (100*(buf[2] - 48)) + (10*(buf[3] - 48)) + (buf[4] - 48);
y_position = (1000 * (buf[6] - 48)) + (100*(buf[7] - 48)) + (10*(buf[8] - 48)) + (buf[9] - 48);
Serial.println(x_position);
Serial.println(y_position);
// Forwards
if (y_position > 600){
y_direction = 1;
}
// Backwards
else if (y_position < 400){
y_direction = -1;
}
// Stop
else {
y_direction = 0;
}
// Now sort out turning
// Right Forwards
if ((x_position > 600) && (y_direction == 1)){
x_direction = 1;
motor1_speed = 125;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 125;
Serial.println("Right Forwards");
}
// Left Forwards
else if ((x_position < 400) && (y_direction == 1)){
x_direction = -1;
motor1_speed = 255;
motor2_speed = 125;
motor3_speed = 125;
motor4_speed = 255;
Serial.println("Left Forwards");
}
// Straight Forwards
else if ((x_position > 400) && (x_position < 600) && (y_direction == 1)){
x_direction = 0;
motor1_speed = 255;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 255;
Serial.println("Straight Forwards");
}
// Right Backwards
else if ((x_position > 600) && (y_direction == -1)){
x_direction = 1;
motor1_speed = 255;
motor2_speed = 125;
motor3_speed = 125;
motor4_speed = 255;
Serial.println("Right Backwards");
}
// Left Backwards
else if ((x_position < 400) && (y_direction == -1)){
x_direction = -1;
motor1_speed = 125;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 125;
Serial.println("Left Backwards");
}
// Straight Backwards
else if ((x_position > 400) && (x_position < 600) && (y_direction == -1)){
x_direction = 0;
motor1_speed = 255;
motor2_speed = 255;
motor3_speed = 255;
motor4_speed = 255;
Serial.println("Straight Backwards");
}
// Stop
else {
x_direction = 0;
motor1_speed = 0;
motor2_speed = 0;
motor3_speed = 0;
motor4_speed = 0;
Serial.println("Stop");
}
if ((y_direction == 1) && (current_y_direction == 1)){
Serial.println("Still Forwards");
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
}
if ((y_direction == -1) && (current_y_direction == -1)){
Serial.println("Still Backwards");
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
}
else if ((y_direction == -1) && ((current_y_direction == 1) || (current_y_direction == 0))){
current_y_direction = -1;
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
motor1.run(BACKWARD); // turn it on going backwards
motor2.run(BACKWARD); // turn it on going backwards
motor3.run(BACKWARD); // turn it on going backwards
motor4.run(BACKWARD); // turn it on going backwards
Serial.println("Newly Backwards");
}
else if ((y_direction == 1) && ((current_y_direction == -1) || (current_y_direction == 0))){
current_y_direction = 1;
motor1.setSpeed(motor1_speed);
motor2.setSpeed(motor2_speed);
motor3.setSpeed(motor3_speed);
motor4.setSpeed(motor4_speed);
motor1.run(FORWARD); // turn it on going forwards
motor2.run(FORWARD); // turn it on going forwards
motor3.run(FORWARD); // turn it on going forwards
motor4.run(FORWARD); // turn it on going forwards
Serial.println("Newly Forwards");
}
else if (y_direction == 0){
current_y_direction = 0;
motor1.run(RELEASE); // turn it on going backwards
motor2.run(RELEASE); // turn it on going backwards
motor3.run(RELEASE); // turn it on going backwards
motor4.run(RELEASE); // turn it on going backwards
Serial.println("Stop");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment