I have a variable doing something I can’t explain and it’s a bit anoying ;D
I’ve got a joystick (2 pots) hooked up to one arduino, which has a cheap transmitter attached, transmitting the X and Y values.
Another arduino with receiver attached is displaying the received value, this is where things get a bit strange…
The transmitter arduino has both values going from -128 to 127 however the reciever has the value going from 128 to 127, explained better in hex, 80 to 7F, better still is this image -
needless to say, this is a bit strange…
I’m still in the happy hacking stage of playing with VirtualWire, which is why the code is a bit messy, but it’s not too hard to follow
radio_rx -
#include <VirtualWire.h>
#define xAxisPIN 4
#define yAxisPIN 5
const char *msg = "Test";
char str[30];
char xAxis = -1;
char yAxis = -4;
char oldXaxis = -1;
char oldYaxis = -4;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_tx_pin(3);
vw_setup(2000); // Bits per sec
}
void loop()
{
doJoystick();
}
void doJoystick(){
getXaxis();
getYaxis();
if(xAxis != oldXaxis || yAxis != oldYaxis){
str[0] = xAxis;
Serial.println(analogRead(xAxisPIN));
Serial.println(str[0], DEC);
str[1] = yAxis;
Serial.println(analogRead(yAxisPIN));
Serial.println(str[1], DEC);
Serial.println();
sendData();
}
}
void getXaxis(){
int axisBuff = 0;
oldXaxis = xAxis;
axisBuff = analogRead(xAxisPIN);
delay(1);
axisBuff += analogRead(xAxisPIN);
delay(1);
axisBuff += analogRead(xAxisPIN);
delay(1);
axisBuff += analogRead(xAxisPIN);
delay(1);
axisBuff += analogRead(xAxisPIN);
xAxis = axisBuff/5/4-128;
}
void getYaxis(){
int axisBuff = 0;
oldYaxis = yAxis;
axisBuff = analogRead(yAxisPIN);
delay(1);
axisBuff += analogRead(yAxisPIN);
delay(1);
axisBuff += analogRead(yAxisPIN);
delay(1);
axisBuff += analogRead(yAxisPIN);
delay(1);
axisBuff += analogRead(yAxisPIN);
yAxis = axisBuff/5/4-128;
}
void sendData(){
digitalWrite(13, HIGH); // Flash a light to show transmitting
vw_send((uint8_t *)str,strlen(str));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, LOW);
}
radio_rx -
#include <VirtualWire.h>
#include <roughLCD.h> // My rough soft serial LCD library
#include <NewSoftSerial.h> // Soft serial port for LCD
NewSoftSerial lcdSerial(9, 8); // Rx, Tx
boolean running = false;
void setup()
{
lcdSerial.begin(19200);
Serial.begin(9600); // Debugging only
Serial.println("setup");
vw_set_rx_pin(14);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
clearLcd();
lcdSerial.print("Ready");
}
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(running == false){
clearLcd();
lcdSerial.print("Got: ");
running = true;
}
int i;
digitalWrite(13, HIGH); // Flash a light to show received good message
// Message with a good checksum received, dump it.
clearLcd();
lcdSerial.print(buf[0], HEX);
lcdSerial.print(" ");
lcdSerial.print(buf[1], HEX);
Serial.print("X Axis - ");Serial.println(buf[0], DEC);
Serial.println();
Serial.print("Y Axis - ");Serial.println(buf[1], DEC);
Serial.println("");
digitalWrite(13, LOW);
}
}
VirtualWire is of course doing the fancy stuff with the radios (which works rather well)