Hi Guys/Girls,
I'am a total newbie with Arduino's but here is my problem.
I'am trying to read the value of my X and Y axis on my joystick connected to uno1 and send it through RF to control two dc motors connected to uno2
I'am using
virtualWire
1x XY-MK-5V 433Mhz RF Receiver
1x FS1000A/XY-FST RF 433Mhz Transmitter
2x arduino uno boards
I can read the X/Y values fine and even transmit them
but the receiver end cant distinguish between the signal for X or the signal for Y that's incoming and basically writes nearly all incoming values to X.
I would run both motors through transistors and all i need is a signal to connect transistor.
I have pasted the code below, and any help would be much appreciated.
Receiver Code:
#include <VirtualWire.h>
int led1 = 6;
int led2 = 9;
const int receive_pin = 3;
int joyxVal;
int joyyVal;
struct package
{
int joyxVal;
int joyyVal;
};
typedef struct package Package;
Package data;
void setup()
{
Serial.begin(9600);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_setup(500); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[sizeof(joyxVal)];
uint8_t buflen = sizeof(joyxVal);
uint8_t buf2[sizeof(joyyVal)];
uint8_t buflen2 = sizeof(joyyVal);
if (vw_have_message()) // Is there a packet for us?
{
vw_get_message(buf, &buflen);
memcpy(&joyxVal,&buf,buflen);
Serial.println();
Serial.println("xaxis");
Serial.println(joyxVal);
Serial.println();
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
}
if (vw_have_message()) // Is there a packet for us?
{
vw_get_message(buf2, &buflen2);
memcpy(&joyyVal,&buf2,buflen2);
Serial.println("yaxis");
Serial.println(joyyVal);
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
}
}
Transmitter Code
#include <VirtualWire.h>
//Connected Joystick to analog pins
int joyXaxis = A0;
int joyYaxis = A1;
struct package
{
int joyxVal ;
int joyyVal ;
};
typedef struct package Package;
Package data;
void setup()
{
//begin serial comm
Serial.begin(9600);
// Sets pin 3 as the TX pin
vw_set_tx_pin(3);
// Required for inverting signal
vw_set_ptt_inverted(true);
// Bits per sec
vw_setup(500);
}
void loop()
{
//read the analog value
int joyxVal = analogRead(joyXaxis);
//map x to digital values
joyxVal = map (joyxVal, 0 , 1023, 0, 255);
//read the analog value
int joyyVal = analogRead(joyYaxis);
//map y to digital value
joyyVal = map (joyyVal, 0, 1023, 0, 255);
vw_send((uint8_t *)&joyxVal, sizeof(joyxVal));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println("joystick X axis = ");
Serial.println(joyxVal);
Serial.println("data sent from x axis");
Serial.println("**************");
delay(1000);
vw_send((uint8_t *)&joyyVal, sizeof(joyyVal));
vw_wait_tx(); // Wait until the whole message is gone
Serial.println("joystick Y axis = ");
Serial.println(joyyVal);
Serial.println("data sent from y axis");
Serial.println("****************");
delay(1000);
}