Sscanf ruins my code :(

Thank you so much guys!! The final solution was to use a long instead of an unsigned int, you guys are amazing without your help i would never have succeeded, BUT i'm not there yet...

i stil get just this:

Setup was succesfull
mySwitch is available
This is mySwitch.getReceivedValue, it should contain 6 numbers:222222
This is value, it should contain 6 numbers:222222
This is data, it should contain 6 numbers:222222
This is blue, it is an char and should contain 2 numbers:22
This is Fblue, it is an int and should contain 2 numbers:22
22
Available is reseted

when sending these three codes:

pi@Willems ~ $ sudo ./codesend 222222
sending code[222222]
pi@Willems ~ $ sudo ./codesend 222221
sending code[222221]
pi@Willems ~ $ sudo ./codesend 111111
sending code[111111]

shouldn't it go trough the if statement again since blue and Pblue are not the same value?

here is my entire code:

#include <RCSwitch.h>
#include <sstream.h> // std::istringstream
#include <iostream.h>
#include <string.h>

RCSwitch mySwitch = RCSwitch();

int ledPinR = 9;
int ledPinG = 10;
int ledPinB = 11;
int Fblue;
char red [3] = {0,0,0};
char blue [3] = {0,0,0};
char green [3] = {0,0,0};
char Pred [3] = {0,0,0};
char Pblue [3] = {0,0,0};
char Pgreen [3] = {0,0,0};
String str;


void setup() {
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  Serial.println("Setup was succesfull");
}

void loop() {
  if (mySwitch.available()) {
    Serial.println("mySwitch is available");
    long value = mySwitch.getReceivedValue();
    Serial.print("This is mySwitch.getReceivedValue, it should contain 6 numbers:");
    Serial.println( mySwitch.getReceivedValue() );
    Serial.print("This is value, it should contain 6 numbers:");
    Serial.println(value);
    char data [7];
    sprintf(data,"%ld",value); //convert 'value' to a string which is padded with 0's to 6 characters.
    Serial.print("This is data, it should contain 6 numbers:");
    Serial.println(data);
    memcpy(red,data+0,2); //copy 2 bytes starting from data[0]
    memcpy(blue,data+2,2); //copy 2 bytes starting from data[2]
    memcpy(green,data+4,2); //copy 2 bytes starting from data[4]
    if (blue !=Pblue) != 0) {
      Serial.print("This is blue, it is an char and should contain 2 numbers:");
      Serial.println(blue);
      Fblue = atoi(blue); 
      Serial.print("This is Fblue, it is an int and should contain 2 numbers:");
      Serial.println(Fblue);
     analogWrite(ledPinB, Fblue);
     // Pblue = Fblue incompatible types in assignment of 'int' to 'char [3]
     strcpy (Pblue,blue);
     Serial.println(Pblue);
    }
    
    mySwitch.resetAvailable();
    Serial.println("Available is reseted");
    }
    

}

and again AWOL, Brad Burleson and especially Tom Carpenter thank you so much!!

if (blue !=Pblue) != 0) {

What is your intention here?

If blue is not equal to Pblue it should be true and run the statements in between the brackets. It thought it would be more efficient than constantly keep running the statements if only one of the 3 parts of the code would change.

You've gone back to not using strcmp(). Remember earlier I said you can't compare two c-strings directly like that.

Use:

if (strcmp(blue,Pblue)){
  //if the two strings aren't the same.
}

O sorry, i pressed backspace accidentally but in my real sketch i used (strcmp(blue,Pblue) != 0 so what else can be the problem?

i pressed backspace accidentally

You changed strcmp(blue,Pblue) != 0 to (blue !=Pblue) != 0)
with a backspace?

huh you're right thats impossible, but still my saved sketch still had the (strcmp(blue,Pblue) in it and just tested it and the problem is still there. This is so weird, i really don't understand this.

Why don't you post your code as it is now, and explain what it is doing that don't you expect it to, and what it is not doing that you expect it to do?

I want it to check the 3d and 4th digit of the received code with has 6 digits and see if it has changed, if it changed analogwrite the value and if it stayed the same do nothing. The problem is it won't run the code inside of the if statement a second time, no matter what value i send.

edit: already tried using: if (strcmp(blue,Pblue)) instead of strcmp(blue,Pblue) != 0 but did not solve it.

#include <RCSwitch.h>
#include <sstream.h> // std::istringstream
#include <iostream.h>
#include <string.h>

RCSwitch mySwitch = RCSwitch();

int ledPinR = 9;
int ledPinG = 10;
int ledPinB = 11;
int Fblue;
char red [3] = {0,0,0};
char blue [3] = {0,0,0};
char green [3] = {0,0,0};
char Pred [3] = {0,0,0};
char Pblue [3] = {0,0,0};
char Pgreen [3] = {0,0,0};
String str;


void setup() {
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  Serial.println("Setup was succesfull");
}

void loop() {
  if (mySwitch.available()) {
    Serial.println("mySwitch is available");
    long value = mySwitch.getReceivedValue();
    Serial.print("This is mySwitch.getReceivedValue, it should contain 6 numbers:");
    Serial.println( mySwitch.getReceivedValue() );
    Serial.print("This is value, it should contain 6 numbers:");
    Serial.println(value);
    char data [7];
    sprintf(data,"%ld",value); //convert 'value' to a string which is padded with 0's to 6 characters.
    Serial.print("This is data, it should contain 6 numbers:");
    Serial.println(data);
    memcpy(red,data+0,2); //copy 2 bytes starting from data[0]
    memcpy(blue,data+2,2); //copy 2 bytes starting from data[2]
    memcpy(green,data+4,2); //copy 2 bytes starting from data[4]
    if (strcmp(blue,Pblue) != 0) {
      Serial.print("This is blue, it is an char and should contain 2 numbers:");
      Serial.println(blue);
      Fblue = atoi(blue); 
      Serial.print("This is Fblue, it is an int and should contain 2 numbers:");
      Serial.println(Fblue);
     analogWrite(ledPinB, Fblue);
     // Pblue = Fblue incompatible types in assignment of 'int' to 'char [3]
     strcpy (Pblue,blue);
     Serial.println(Pblue);
    }
    
    mySwitch.resetAvailable();
    Serial.println("Available is reseted");
    }
    

}

Why did the sprintf line change back??

sprintf(data,"%06ld",value);

The 06 is what does the padding with 0's

I changed it but the problem described above still exists.

OK, you should know the drill by now; post the code, post the debug output, tell us what you expected to happen but didn't, tell us what happened that you didn't expect - we're suspicious, not psychic.

Nothing changed, code stayed the same except for that little change.

OK, good luck

Hey i'm sorry if i insulted you somehow, this was not my intention. If you want me to copy it i will, here:

#include <RCSwitch.h>
#include <sstream.h> // std::istringstream
#include <iostream.h>
#include <string.h>

RCSwitch mySwitch = RCSwitch();

int ledPinR = 9;
int ledPinG = 10;
int ledPinB = 11;
int Fblue;
char red [3] = {0,0,0};
char blue [3] = {0,0,0};
char green [3] = {0,0,0};
char Pred [3] = {0,0,0};
char Pblue [3] = {0,0,0};
char Pgreen [3] = {0,0,0};
String str;


void setup() {
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  Serial.println("Setup was succesfull");
}

void loop() {
  if (mySwitch.available()) {
    Serial.println("mySwitch is available");
    long value = mySwitch.getReceivedValue();
    Serial.print("This is mySwitch.getReceivedValue, it should contain 6 numbers:");
    Serial.println( mySwitch.getReceivedValue() );
    Serial.print("This is value, it should contain 6 numbers:");
    Serial.println(value);
    char data [7];
    sprintf(data,"%06ld",value); //convert 'value' to a string which is padded with 0's to 6 characters.
    Serial.print("This is data, it should contain 6 numbers:");
    Serial.println(data);
    memcpy(red,data+0,2); //copy 2 bytes starting from data[0]
    memcpy(blue,data+2,2); //copy 2 bytes starting from data[2]
    memcpy(green,data+4,2); //copy 2 bytes starting from data[4]
    if (strcmp(blue,Pblue) != 0) {
      Serial.print("This is blue, it is an char and should contain 2 numbers:");
      Serial.println(blue);
      Fblue = atoi(blue); 
      Serial.print("This is Fblue, it is an int and should contain 2 numbers:");
      Serial.println(Fblue);
     analogWrite(ledPinB, Fblue);
     // Pblue = Fblue incompatible types in assignment of 'int' to 'char [3]
     strcpy (Pblue,blue);
     Serial.println(Pblue);
    }
    
    mySwitch.resetAvailable();
    Serial.println("Available is reseted");
    }
    

}

Setup was succesfull
mySwitch is available
This is mySwitch.getReceivedValue, it should contain 6 numbers:222222
This is value, it should contain 6 numbers:222222
This is data, it should contain 6 numbers:222222
This is blue, it is an char and should contain 2 numbers:22
This is Fblue, it is an int and should contain 2 numbers:22
22
Available is reseted

just one time, i have to reboot the arduino to do it again.

Everything works perfect the only problem is that i have to reboot it in order to receive another value.

I really don't understand that library, it seems there is a major flaw in it - essentially if you receive a code between checking 'mySwitch.available()' and calling 'mySwitch.resetAvailable()' then you lose the code because nowhere in that library does it check to see if the last received code has been used by the user, instead just blindly overwrites it in an interrupt - it doesn't even keep count of how many received. It also starts comparing a non-pointer to NULL rather than 0 which is just bizarre.

What happens if you send one code and then wait for a few seconds (don't reset the Arduino), then send another?

If I send a second code with this sketch uploaded to the arduino it does nothing, no serial output and the value of the analog pin does not change, however if I put an serialprintln before the last } it keeps outputting it so the sketch is still running. When I write a sketch which just has to receive a value of two digits and output it, no problem either. Printing the received codes, no problem either, I can send just as many codes as I want but there is some fault in my sketch that doesn't allow the code inside of the if statement to run again, I just don't understand what the fault is.

edit: even if i put 1 == 1 as condition it still refuses to run

The problem is the sprintf, without it it can run multiple times

rutierut:
The problem is the sprintf, without it it can run multiple times

The thread title says sscanf is the problem.
Which is it?

.toCharArray
sprintf
sscanf

these three prevent my code from running multipe times, but i have no idea how to convert the value i receive, which is a long int, to an char array if these can't be used.