EDIT:
I figured out the problem. Apparently the pwn generator on pin 9 is broken, as my code works when I changed it to pin 3. Thank you for the people who still tried to help me, despite the problem not being related to code :)
Original text:
I'm having problems with a transmitter & receiver setup. Right now I have an Arduino Nano with a joystick controller with the X-coordinate connected to pin A0 on the Nano. The radio is connected to pin 12 (default pin for radiohead library). This setup works for the transmitter side, as it surpasses the "waitPacketSent" command and I've seen the receiver get the value sometimes.
The problem is the receiver side. Sometimes it can receive values if the joystick is pointed all the way up (>1000), but if it receives a value below 1000, it just completly stops and won't work. The radio receiver is set on pin A0. I did also try making aluminum foil antennas to see if that was an issue, but it changed nothing.
Transmitter on Arduino Nano:
#include <RH_ASK.h>
#include <SPI.h>
int joyxpin = A0;
int vcc_joy = 11;
RH_ASK rf_driver;
void setup() {
// put your setup code here, to run once:
rf_driver.init();
// Set pinmodes
pinMode(joyxpin, INPUT); // Set the joysticks X-coordinate to pin A0
pinMode(vcc_joy, OUTPUT); // Set 12 as output
// Set states
digitalWrite(vcc_joy, HIGH); // Set 12 to 5V
Serial.begin(9600);
}
int getXCoord(){
int xj = analogRead(A0);
if(xj > 485 && xj < 522){
xj = 512;
}
return xj;
}
void loop() {
int joyx = getXCoord();
char msg[6]; // Buffer to hold the string representation of the value
// Convert integer to string
itoa(joyx, msg, 10);
// Print the value being sent for debugging
Serial.print("Sending: ");
Serial.println(msg);
// Send the message
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(100);
}
Receiver on Arduino Uno:
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK rf_driver(2000, A0, 12); // Initialize with speed, rxPin (A0), and txPin (12)
// Define pin number for the MOSFET gate
const int MOSFET_GATE_PIN = 9;
int joyrec = 0; // Variable to store received joystick value
void setup() {
rf_driver.init();
Serial.begin(9600);
// Set the MOSFET gate pin as an output
pinMode(MOSFET_GATE_PIN, OUTPUT);
}
void loop() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; // Buffer to hold the received string
uint8_t buflen = sizeof(buf);
if (rf_driver.recv((uint8_t *)buf, &buflen)) { // If a message is received
buf[buflen] = '\0'; // Null-terminate the received string
rf_driver.printBuffer("Received:", buf, buflen);
int value = buf[0];
Serial.println(value);
// Convert received message (string) to integer
int joyrec = atoi((char *)buf);
int pwm_motor = map(joyrec, 0, 1023, 0, 255);
// Move the motor based on the received value
analogWrite(MOSFET_GATE_PIN, pwm_motor); // Set speed to the received value
}
delay(1);
}
int vcc_joy = 11;
and you have the following commentpinMode(vcc_joy, OUTPUT); // Set 12 as output
. Which pin is correct ? Anyway, you should create a minimal example which demonstrates the problem. Start by sending a simple 5 character message from the transmitter to the receiver. If that works, then pack an integer (16bit) into the message and unpack it at the other end. If that works, add in the joy stick.buflen
is based onsizeof[buf]
which is much longer than thestrlen
that the sender sent, so you are trying to receive more than it actually sent and you are null-terminated at the wrong place of the buf.