Hello everybody,
I want to use my Arduino Uno to receive commands from my Raspberry Pi to control some LEDs using 433 mHz receivers and transmitters.
The commands sent by the Raspberr Pi are long values following a structured syntax:
number 1 --> start number --> program starts if number equals one
number 2-3 --> port number --> port we want to control
number 4-6 --> value --> value which is written to the port.
e.g.
103255 --> sets port 03 to HIGH
103000 --> sets port 03 to LOW
103125 --> should set the port 03 to 125, but this isn't working
If i use 000 or 255 as the value, the program works fine, but when I start to use 125 as the given value the arduino crashes an doesnt do anything. I tested the port by just writing the values from 0 to 255 with a for loop - and it is working fine. But whenever I want to write a value different than 000 or 255, it crashes. The arduino won
t receive any new data sent by the Raspberry.
I also tested whether the Raspberry is sending the value. Because he really sends it, it must be a problem of the arduino.
Below you can see my code.
Thanks for helping
JanF_04
# include <RCSwitch.h>
const int PORTS = 20; // digital and analog ports
const int PWM[] = {3, 5, 6, 9, 10, 11};
const int INTERRUPT_PIN = 2;
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
for (int pin = 0; pin < PORTS; pin = pin + 1) {
if (pin != INTERRUPT_PIN) {
pinMode(pin, OUTPUT);
Serial.println(pin);
}
}
// enables receiving codes from 433 mhz sensor
mySwitch.enableReceive(0);
}
void loop() {
if (mySwitch.available()){ //if data is avaiable
//getting the data of the 433 receiver
long data = mySwitch.getReceivedValue();
if (data == 0) {
Serial.println("unknown encoding");
} else {
// print the received data and some properties
data = mySwitch.getReceivedValue() ;
Serial.println("Received:");
Serial.print(" Value: ");
Serial.println(mySwitch.getReceivedValue() );
Serial.print(" BitLength:");
Serial.println(mySwitch.getReceivedBitlength() );
Serial.print(" Protocol: ");
Serial.println(mySwitch.getReceivedProtocol() );
// extracting the command informations
int start = (data / 100000U) % 10;
int pin = (((data / 10000U) % 10) * 10) + ((data / 1000U) % 10);
int value = (((data / 100U) % 10) * 100) + (((data / 10U) % 10) * 10) + (data % 10);
// printing the command informations
Serial.print(data);
Serial.print(" - ");
Serial.print(start);
Serial.print(" - ");
Serial.print(pin);
Serial.print(" - ");
Serial.println(value);
Serial.println("-------------------------------");
//processing the command
if (start == 1) {
if (ElementOfPWM(pin)) {
analogWrite(pin, value);
} else {
if (value > 127) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
}
}
}
// reset receiver value
mySwitch.resetAvailable();
}
}
bool ElementOfPWM(int number) {
for (int i = 0; i < sizeof(PWM); i = i + 1) {
if (PWM[i] == number) {
return true;
}
}
return false;
}