You are doing a Serial.println(int); which does output the value as a character string followed by a CR LF, but on the Core side you are reading the individual characters into a number, so you will never get the original value read into sensor.
Try Serial.write(int) on the Arduino side and read the incoming value byte by byte into the correct bytes of your sensor var, but be aware, that a Arduino int might be shorter than a Core int. You might be better using int16_t on both sides, to make sure the two are alike.
char inString[6];
int inByte=0;
int stringPos = 0;
int16_t sensor = 0;
void setup() {
Serial1.begin(9600);
Spark.variable("sensor", &sensor, INT);
}
void loop() {
if((inByte >= '0') && (inByte <= '9')){
inString[stringPos] = inByte;
stringPos ++;
}
if(inByte == '\r'){ // if there is a line end character, this string is done
sensor = atoi(inString); //convert string to int
}
}
Now we have the opposite problem.
With Serial.write(int) you are sending the binary value of the integer, so you will receive a binary value and not a character string, which you are now trying to read.
Before you sent out a character string and were trying to receive a binary value.
Missed it. That’s what happens with copy/paste. Sorry, I am still a beginner.
So if I change the Arduino code back to Serial.println it should work?
Thanks for helping.
No it won’t - sorry
You seem not to read any data from Serial1 in the last example.
And since you are not resetting stringPos you’ll run out of inString[] corrupting other memory.
And since you are not terminating your string with '\0' once you drop out of your digit acquisitioning if-statement, your atoi will probably fail or give you wrong results.
But if I were you, I’d not go down the string route anyway, unless you want to present the data transmitted data in human readable form.
Since you are using the transmitted value as a number anyway, I’d stick with write() and receive the bytes directly into the int variable. And I’d read both bytes in one iteration of loop(). You are pushing out both bytes in one iteration of the Arduino loop(), too.
@coconut, give this a go for binary value transmission.
int16_t sensor = 0;
void setup() {
pinMode(D7, OUTPUT);
Serial.begin(115200); // setup USB-Serial
Serial1.begin(9600); // setup RX/TX
while(!Serial.available()) // wait for anything to arrive via USB-Serial
Particle.process();
}
void loop() {
digitalWrite(D7, !digitalRead(D7)); // toggle blue LED
while(Serial1.available() >= sizeof(sensor)) // we are expecting at least
// two byte to compose a int16_t
sensor = Serial1.read() | (Serial1.read() << 8); // read first byte, then
// combine with second byte
// which gets left shifted
// by 8 bits
Serial.println(sensor, HEX); // print result to USB-Serial#
if (digitalRead(D7)) { // one way to transmit an int16_
Serial1.write(0x1234); // first send LSB (least significant byte)
Serial1.write(0x1234 >> 8); // then right shift 8 bit to send MSB
}
else {
sensor <<= 1; // multiply by two - the nerdy (binary) way ;-)
Serial1.write((const uint8_t*)&sensor, sizeof(sensor)); // another way to do
// the same thing
}
delay(500);
}
Flash this to your Core, connect RX to TX of your Core to let it talk to itself and watch the USB output
Hello
In case anyone is interested, I found 2 ways of reading an integer with unknown length from arduino via serial communication.
With a loop:
int inByte = -1;
char inString[6];
int stringPos = 0;
int sensor =0;
void setup() {
Serial1.begin(9600);
Particle.variable("sensor", sensor);
}
void loop() {
inByte = Serial1.read();
if((inByte >= '0') && (inByte <= '9')){
inString[stringPos] = inByte;
stringPos ++;
}
//if there is a line end character, this string is done.
//clear the string when done
if(inByte == '\r'){
sensor = atoi(inString); //convert string to int
//clear the values from inString
for (int c = 0; c < stringPos; c++){
inString[c] = 0;
}
stringPos = 0;
}
}