Ok a little update for people who don't want to read trough the entire thread:
Goal: Send a 6-digit code via 433Mhz to an Arduino, the code should be split up in three pieces (123456 would be split up in 12, 34 and 56) these pieces each represent a color in my RGB setup.
Problem:
the code i must use is an long integer, in order to slice it up i want to convert it to an Char array, i tried doing this by using:
-sscanf
-.toCharArray
-sprintf
all of these codes prevent the code from running multiple times (they can do it once successfully but won't run a second time)
Question:
So the question is:
- Why do these functions mess up my sketch, they work perfectly besides the fact they can only be used one time.
- Is there an alternative? or a way to split an integer like this?
Thank you for reading this and my apologies for my lack of knowledge as i'm completely new to this
Here is my current 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();
int length = floor(log10(value)+1);
if (length == 6){
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];
----------Here should the value of value be copied to data ----------------
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]
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");
}
}
If i include one of the functions listed above this is the result, no matter how many codes i keep sending: (first code i send was 222222 in this case)
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
-----------------------------Outdated-Info--------------------------------------
Ok so i have a piece of code right here:
void loop() {
if (mySwitch.available()) {
Serial.println("Test1");
int value = mySwitch.getReceivedValue();
String code = String(value);
String Sblue = code.substring(0,3);
sscanf(Sblue.c_str(), "%d", &blue);
Serial.print(blue);
if (blue != Cblue) {
analogWrite(ledPinR, blue);
Cblue = blue;
Serial.println("blauw");
}
mySwitch.resetAvailable();
Serial.println("reset");
}
}
Serial monitor with sscanf:
Test1
100blauw
reset
(only works 1 time)
Serial monitor without sscanf:
Test1
0blauw
reset
(works for ever)
I can send a code once and it wil work perfectly, but when i send a second one the first if statement wil not act as if it is TRUE, while it is! When i remove: sscanf(Sblue.c_str(), "%d", &blue);
it does repeat the proces correctly but obviously renders my code useless. If somebody could help me over here i would be most grateful, i already asked a friend with a lot of experience but as he specialises in Java he couldn't solve it.
Thanks in advance!
p.s. This is an Example that came with the library.
/*
Simple example for receiving
http://code.google.com/p/rc-switch/
Need help? http://forum.ardumote.com
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
}
mySwitch.resetAvailable();
}
}