#include <VirtualWire.h> // you must download and install the VirtualWire.h to your hardware/libraries folder
//Undefine unneded types
#undef abs
#undef double
#undef float
#undef round
//Define constants
#define FORWARD '8'
#define BACKWARD '5'
#define STOP '0'
#define LEFT '4'
#define RIGHT '6'
#define STRAIGHT '/'
#define TIMEOUT 500UL
//Set motor pins
int motor1Pin1 = 4; // pin 2 on L293D
// pin 7 on L293D
int enablePin1 = 10; // pin 1 on L293D
int motor2Pin1 = 6; // pin 10 on L293D
// pin 15 on L293D
int enablePin2 = 11; // pin 9 on L293D
//motor speed variables
int motor1Speed = 255;
int motor2Speed = 255;
int angle = 0; // angle defined
//Counter variable
unsigned long counter;
void setup()
{
//Set pin modes
pinMode(motor1Pin1, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
Serial.begin(115200);
//Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(4500); // Bits per sec
vw_set_rx_pin(13); // We will be receiving on pin 23 (Mega) ie the RX pin from the module connects to this pin.
vw_rx_start(); // Start the receiver
}
void loop()
{
//variables for Virtual Wire
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
//If we got a message from the serial port
if (Serial.available())
{
//Reset the timeout counter
counter = millis();
//Drive the requested motor
drive(Serial.read());
//Otherwise check for a message from the reciever
} else if (vw_get_message(buf, &buflen)) // check to see if anything has been received
{
//Reset timeout counter
counter = millis();
int i;
Serial.println("recieved"); //Debug message
// Message with a good checksum received.
//For each command recieved
for (i = 0; i < buflen; i++)
{
drive(buf[i]); //Drive requested motors
}
//If the counter is greater than the timeout (No signal recieved) then stop the car.
} else if(millis()-counter >= TIMEOUT) {
drive(STOP);
drive(STRAIGHT);
}
}
//The drive function
void drive(int dir) {
//Check which direction is requested (same for below)
if (dir == FORWARD) {
Serial.println("Forward");//Used for debugging
//Set motor accordingly (same for below)
digitalWrite(motor1Pin1, LOW);
analogWrite(enablePin1, 255);
digitalWrite(motor2Pin1,LOW);
analogWrite(enablePin2, 255);
} else if (dir == BACKWARD) {
Serial.println("Backward");
digitalWrite(motor1Pin1,HIGH);
analogWrite(enablePin1, 255);
digitalWrite(motor2Pin1, HIGH);
analogWrite(enablePin2, 255);
} else if (dir == STOP) {
Serial.println("Stop");
digitalWrite(motor1Pin1, LOW);
analogWrite(enablePin1, 0);
digitalWrite(motor2Pin1, LOW);
analogWrite(enablePin2, 0);
} else if(dir == STRAIGHT) {
Serial.println("Straight");
digitalWrite(motor1Pin1, HIGH);
analogWrite(enablePin1, 0);
digitalWrite(motor2Pin1, HIGH);
analogWrite(enablePin2,0);
}else if (dir == LEFT) {
Serial.println("Left");
digitalWrite(motor1Pin1, LOW);
analogWrite(enablePin1, 255);
digitalWrite(motor2Pin1, HIGH);
analogWrite(enablePin2, 255);
}else if (dir == RIGHT) {
Serial.println("Right");
digitalWrite(motor1Pin1, HIGH);
analogWrite(enablePin1, 0);
digitalWrite(motor2Pin1, LOW);
analogWrite(enablePin2, 255);
}
}
Comments
Please log in or sign up to comment.