Hey, so i am writing a sketch for an rc car. I am using one of those cheap five dollar rf transmitter and receiver modules and am using virtual wire library in my sketch. I had gotten it to when i hit one of the four buttons, the remote control would wirelessly tell one of the two motors which direction to turn. However i cannot seem to figure out how to allow the car to multi task, for example turn right while going forward. I found that i needed to send two bytes at a time the first telling motor A what to do and the second telling motor B what to do. The wireless data is handled in a for loop when received thus meaning it is in an array. So i geuss i am stuck the two sketches below (transmitter and receiver) will happily control the direction of the first motor but just doesn't seem to work with the second. I know i did a terrible job of explaining so if needed i can provide additional information. Thanks so much. (I attatched the files below)
TRANSMITTER
// you must download and install the VirtualWire.h
// to your hardware/libraries folder
#include <VirtualWire.h>
const int Enable_Motor_A = 9;
const int P1_Motor_A = 11;
const int P2_Motor_A = 13;
const int Enable_Motor_B = 10;
const int P1_Motor_B = 5;
const int P2_Motor_B = 6;
byte msg[1];
void setup()
{
Serial.begin(9600);
pinMode(6, INPUT);
pinMode(13, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
vw_setup(2000);
vw_set_tx_pin(7);
}
void loop()
{
msg[0] = '0';
msg[0] = '0';
if(digitalRead(13) == HIGH) {
msg[0] = '1';
}
else if(digitalRead(6) == HIGH) {
msg[0] = '2';
}
if(digitalRead(8) == HIGH) {
msg[1] = '3';
}
else if(digitalRead(9) == HIGH) {
msg[1] = '4';
}
vw_send(msg, 1);
vw_wait_tx();
}
RECEIVER
#include <VirtualWire.h>
const int Enable_Motor_A = 9;
const int P1_Motor_A = 12;
const int P2_Motor_A = 13;
const int Enable_Motor_B = 10;
const int P1_Motor_B = 5;
const int P2_Motor_B = 6;
byte buf[1];
byte buflen;
void setup()
{
buflen = 1;
pinMode(Enable_Motor_A, OUTPUT); //Declares the three 'Motor A' pins as outputs.
pinMode(P1_Motor_A, OUTPUT);
pinMode(P2_Motor_A, OUTPUT);
pinMode(Enable_Motor_B, OUTPUT); //Declares the three 'Motor B' pins as outputs.
pinMode(P1_Motor_B, OUTPUT);
pinMode(P2_Motor_B, OUTPUT);
digitalWrite(Enable_Motor_A, LOW);
digitalWrite(P1_Motor_A, LOW);
digitalWrite(P2_Motor_A, LOW);
digitalWrite(Enable_Motor_B, LOW);
digitalWrite(P1_Motor_B, LOW);
digitalWrite(P2_Motor_B, LOW);
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop()
{
uint8_t buflen = VW_MAX_MESSAGE_LEN;
uint8_t buf[buflen];
if (vw_get_message(buf, &buflen))
{
for(int i = 0;i < buflen;i++)
{
if (buf[0] == '1') //FORWARDS BUTTON
{
digitalWrite(P2_Motor_A, LOW);
digitalWrite(P1_Motor_A, HIGH);
analogWrite(Enable_Motor_A, 255);}
else if (buf[0] == '2') //BACKWARDS BUTTON
{
digitalWrite(P1_Motor_A, LOW);
digitalWrite(P2_Motor_A, HIGH);
analogWrite(Enable_Motor_A, 255);}
else //NIETHER BUTTONS
{
digitalWrite(P1_Motor_A, LOW);
digitalWrite(P2_Motor_A, LOW);
digitalWrite(Enable_Motor_A, LOW);}
if (buf[1] == '3') //RIGHT BUTTON
{
digitalWrite(P2_Motor_B, LOW);
digitalWrite(P1_Motor_B, HIGH);
analogWrite(Enable_Motor_B, 255);}
else if (buf[1] == '4') //LEFT BUTTON
{
digitalWrite(P1_Motor_B, LOW);
digitalWrite(P2_Motor_B, HIGH);
analogWrite(Enable_Motor_B, 255);}
else //NIETHER BUTTONS
{
digitalWrite(P1_Motor_B, LOW);
digitalWrite(P2_Motor_B, LOW);
digitalWrite(Enable_Motor_B, LOW);}
}}
}