Hi!
I'm using a 433MHz RF receiver and transmitter to send two values (0-1023) from one Arduino nano to another.
I'm trying to use the value (divided by 4) to set the brightness of a LED on the receiving end.
I'm able to receive the sent values properly, but anytime I use analogWrite() to use a PWM pin, I no longer receive new messages. Everything inside ... if (driver.recv(buf, &buflen)) { blablaba }... no longer works, everything outside that part keeps running.
Maybe helpful extra info:
- When I disconnect the LED pin, I start receiving messages again. When I reconnect, new messages stop.
- The problem does not occur when I write analog value 0 or 255.
- The problem also occurs when I send from an Arduino Nano to an Arduino Uno.
- The problem also occurs at 315MHz
- I've tried different LED pins, without success: I am aware that RadioHead library uses timer1, so I cannot use pins 9 and 10.
- I've tried using Timer2 by adjusting the library, also without success.
Any ideas on how to solve this? Thanks for your help in advance!
Receiver code
//Tranceiver pin = 12
//Receiver pin = 11 (HIER)
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
RH_ASK driver;
int ledPin = 6;
int brightness;
struct dataStruct{
int value1;
int value2;
unsigned long counter;
}myData;
void setup(){
Serial.begin(9600); // Debugging only
pinMode(ledPin, OUTPUT);
if (!driver.init())
Serial.println("init failed");
}
void loop() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) { // Non-blocking
int i;
// Message with a good checksum received, dump it.
driver.printBuffer("Got:", buf, buflen);
memcpy(&myData, buf, sizeof(myData));
Serial.println("");
Serial.print("Value 1:");
Serial.println(myData.value1);
Serial.print("Value 2: ");
Serial.println(myData.value2);
Serial.print("Counter: ");
Serial.println(myData.counter);
brightness = myData.value1 / 4;
Serial.print("Brightness: ");
Serial.println(brightness);
}
Serial.println(brightness);
analogWrite(ledPin, brightness);
}
Transmitter code
//Tranceiver pin = 12 (HIER)
//Receiver pin = 11
const int inp_value1 = A0;
const int inp_value2 = A1;
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
struct dataStruct{
int value1;
int value2;
unsigned long counter;
}myData;
byte tx_buf[sizeof(myData)] = {0};
void setup(){
Serial.begin(9600); // Debugging only
pinMode(0, INPUT);
pinMode(inp_value2, INPUT);
if (!driver.init())
Serial.println("init failed");
}
void loop(){
myData.value1 = analogRead(inp_value1);
myData.value2 = analogRead(inp_value2);
Serial.print("Value 1: ");
Serial.println(myData.value1);
memcpy(tx_buf, &myData, sizeof(myData));
byte len = sizeof(myData);
driver.send((uint8_t *)tx_buf, len);
// driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
myData.counter++;
delay(500);
}
!