Blame | Last modification | View Log | RSS feed
/*
433Mhz Reviever Connections
View from front
__________________________
| |
|_A_G_G_V__________V_D_D_G_|
| | | | | | | |
^ ^ ^
5V D2 Gnd
MotorShield Connections
M1___G__M2_
|o|o||o||o|o|
^ ^ ^ ^
| | | |
+R -R -L +L
R = Right motor, L = Left motor
*/
#include <VirtualWire.h>
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_2KHZ); //Right
AF_DCMotor motor2(2, MOTOR12_2KHZ); //Left
const int rxPin = 2;
const int rxBps = 2000;
const int MRIGHT = 0;
const int MLEFT = 1;
const int MFORWARD = 1;
const int MREVERSE = 0;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
vw_set_rx_pin(rxPin);
vw_setup(rxBps); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
// Start motors idle
motor1.setSpeed(0);
motor2.setSpeed(0);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
digitalWrite(13, true); // Flash a light to show received good message
digitalWrite(13, false);
/*
int i;
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.println("");
*/
int mspd[2] = {0, 0}; //Speed of right track
int options = 0; // Options
if (buf[0]) // Read the options
options = buf[0] - 1;
if (buf[1]) // Right speed
mspd[MRIGHT] = buf[1] -1;
if (buf[2]) // Left speed
mspd[MLEFT] = buf[2] -1;
Serial.print("options "); Serial.print(options, BIN);
Serial.print(" r_spd "); Serial.print(mspd[MRIGHT], DEC);
Serial.print(" l_spd "); Serial.print(mspd[MLEFT], DEC);
Serial.println("");
// Start the tracks rolling
if (mspd[MRIGHT] > 0) {
motor1.setSpeed(mspd[MRIGHT]);
if (bitRead(options, MRIGHT))
motor1.run(FORWARD);
else
motor1.run(BACKWARD);
} else
motor1.run(RELEASE);
if (mspd[MLEFT] > 0) {
motor2.setSpeed(mspd[MLEFT]);
if (bitRead(options, MLEFT))
motor2.run(FORWARD);
else
motor2.run(BACKWARD);
} else
motor2.run(RELEASE);
}
}