Using RCswitch to send 433Mhz code...

Hello all! Please help as I'm wrecking my brain, but can't get it....

This bit of code where I read a number sent to the Arduino via serial, and then try to send it out fails in all sorts of weird ways. It just won't create a proper set of number for the library to send...

It's clearly wrong, but why..... :'(

while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      inString += (char)inChar;
    }
    if (inChar == '#') {
      int code = atoi(inString);
      mySwitch.send(code, 24);
      Serial.println(code);
      inString = "";
      inChar = "";
    }
  }
  
  delay(10);

Please see this page. http://snippets-r-us.com/

Then come back and give us something complete to work with.

dimmslider: This bit of code where I read a number sent to the Arduino via serial, and then try to send it out fails in all sorts of weird ways.

Like what. What is the symptom? What do you mean by fails in weird ways? That doesn't really tell someone trying to help you debug much information.

Hey Delta_G. Thanks for replying. The issue I have is that no matter what number I send via serial console gets turned to a zero....

int code = atoi(inString);
      mySwitch.send(code, 24);
      Serial.println(code);

This bit here when I try to print back what it sent, I get a zero...

OK, I asked for complete code and I get another meaningless snippet with no context. Please see this website and I think you will learn something important.

Here’s the full program, sorry :slight_smile:

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
String inString = "";

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  mySwitch.enableTransmit(10);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Power UP completed...");
}

void loop() {
  if (mySwitch.available()) {
    int value = mySwitch.getReceivedValue();    
    if (value == 0) {
      Serial.println("Unknown encoding");
    } else {
        Serial.println( mySwitch.getReceivedValue() );
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        blinker();
    }
    mySwitch.resetAvailable();
    
}

  // Read serial input:
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      inString += (char)inChar;
    }
    if (inChar == '#') {
      int code = atoi(inString[1]);
      mySwitch.send(code, 24);
      blinker();
      Serial.println(code);
      inString = "";
    }
  }
  
  delay(10); 
}


void blinker() {
        digitalWrite(LED_BUILTIN, HIGH); 
        delay(30);                       
        digitalWrite(LED_BUILTIN, LOW);
        delay(30);
        digitalWrite(LED_BUILTIN, HIGH);
        delay(30);                       
        digitalWrite(LED_BUILTIN, LOW);
        delay(30);
}
int code = atoi(inString[1]);

Is that a legal use of the String class? I don't use String much because I mostly code for microcontrollers when using C++ and String has a nasty reputation for crashing microcontrollers like Arduino.

Are you looking for the second character in the string or the first? What you have there appears to be looking for the second character. Remember, computers start counting at 0 and not 1.

I thought I had to put a number I send via serial into a string, and then convert to int for RCSwitch to send it…

Also, yes, that’s a point, I need the entire string, not just the second character in it…

I also tried like the below. But I get a weird number back. Attached the screenshot.

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
String inString = "";

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  mySwitch.enableTransmit(10);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Power UP completed...");
}

void loop() {
  if (mySwitch.available()) {
    int value = mySwitch.getReceivedValue();    
    if (value == 0) {
      Serial.println("Unknown encoding");
    } else {
        Serial.println( mySwitch.getReceivedValue() );
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        blinker();
    }
    mySwitch.resetAvailable();
    
}

  // Read serial input:
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      inString += (char)inChar;
    }
    if (inChar == '#') {
      //int code = atoi(inString[1]);
      int code = inString.toInt();
      mySwitch.send(code, 24);
      blinker();
      Serial.println(code);
      inString = "";
    }
  }
  
  delay(10); 
}


void blinker() {
        digitalWrite(LED_BUILTIN, HIGH); 
        delay(30);                       
        digitalWrite(LED_BUILTIN, LOW);
        delay(30);
        digitalWrite(LED_BUILTIN, HIGH);
        delay(30);                       
        digitalWrite(LED_BUILTIN, LOW);
        delay(30);
}

Got it working in the end. Thanks delta_g for making me look :slight_smile:

Just needed to use long, not int. Now it sends correctly.

Here’s working code, just in case anyone has the same problem:

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
String inString = "";

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);
  mySwitch.enableTransmit(10);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Power UP completed...");
}

void loop() {
  if (mySwitch.available()) {
    int value = mySwitch.getReceivedValue();    
    if (value == 0) {
      Serial.println("Unknown encoding");
    } else {
        Serial.println( mySwitch.getReceivedValue() );
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        blinker();
    }
    mySwitch.resetAvailable();
    
}

  // Read serial input:
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      inString += (char)inChar;
    }
    if (inChar == '#') {
      long code = inString.toInt();
      mySwitch.send(code, 24);
      blinker();
      Serial.println(code);
      inString = "";
    }
  }
  
  delay(10); 
}


void blinker() {
        digitalWrite(LED_BUILTIN, HIGH); 
        delay(30);                       
        digitalWrite(LED_BUILTIN, LOW);
        delay(30);
        digitalWrite(LED_BUILTIN, HIGH);
        delay(30);                       
        digitalWrite(LED_BUILTIN, LOW);
        delay(30);
}