Hello,
I am working on a project to build an RC toy vehicle that has 2 wheels.
I am trying to test a simple wireless communication between the controller and the vehicle.
(I am using 2 arduino uno's)
I fabricated a dc motor driver with 2 h-bridges (A953) that has pins to connect to an arduino to control 2 motors.
The motors work great.
I am using a 2-axis parallax joystick on another arduino uno and i want it to control the 2 motors.
For the RF part i am using this module :
I am trying VirtualWire as the library after trying other libraries which were less intuitive,
This is the transmitter code:
#include <VirtualWire.h>
int UD = 0;
int LR = 0;
char *controller;
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_setup(4000);// speed of data transfer Kbps
}
void loop() {
UD = analogRead(A0);
LR = analogRead(A1);
Serial.print("UD = ");
Serial.print(UD, DEC);
Serial.print(", LR = ");
Serial.println(LR, DEC);
if(UD>531)//Drive forward
{
controller="1";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(2000);
}
else if(UD<531)//Drive backwards
{
controller="2";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(2000);
}
else if(UD==531)
{
controller="0";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(2000);
}
else
{
digitalWrite(13,0);
}
}
This is the receiver code:
const int motor1PinA = 7; // H-bridge leg 1
const int motor1PinB = 8; // H-bridge leg 2
const int motor2PinA = 2; // H-bridge leg 1
const int motor2PinB = 3; // H-bridge leg 2
#include <VirtualWire.h>
void setup()
{
pinMode(motor1PinA, OUTPUT);
pinMode(motor1PinB, OUTPUT);
pinMode(motor2PinA, OUTPUT);
pinMode(motor2PinB, OUTPUT);
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(12);
vw_setup(4000); // Bits per sec
pinMode(13, OUTPUT);
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
if(buf[0]=='1'){
digitalWrite(motor1PinA, HIGH); // set leg 1 of the H-bridge low
digitalWrite(motor1PinB, LOW); // set leg 2 of the H-bridge high
digitalWrite(motor2PinA, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2PinB, HIGH); // set leg 2 of the H-bridge high
}
if(buf[0]=='2'){
digitalWrite(motor1PinB, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor1PinA, HIGH); // set leg 2 of the H-bridge high
digitalWrite(motor2PinB, HIGH); // set leg 1 of the H-bridge low
digitalWrite(motor2PinA, LOW); // set leg 2 of the H-bridge high
}
if(buf[0]=='0'){
digitalWrite(motor1PinB, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor1PinA, LOW); // set leg 2 of the H-bridge high
digitalWrite(motor2PinB, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2PinA, LOW); // set leg 2 of the H-bridge high
}
}
}
Nothing happens when i run it and try to move the joystick.
I did try the simple example in the library and it seemed to work (led's on each arduino blinked simultaneously).
Each of the settings work great separately (i.e motors on the arduino , joystick on an arduino)
I appreciate any help, if there is anymore info needed let me know
Thanks,
Roy.