Go Down

Topic: add number to long int (Read 730 times) previous topic - next topic

smallpoul

Sep 10, 2013, 08:23 pm Last Edit: Sep 10, 2013, 08:30 pm by smallpoul Reason: 1
Hi,
    all i want is to add a number before the code that the sensor send. here the code:
Code: [Select]
/*
  Simple example for receiving
 
  http://code.google.com/p/rc-switch/
 
  Need help? http://forum.ardumote.com
*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
long int sensorcode;
long int type_sensor = 1;

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
  Serial.println("No RF");
}

void loop() {
  if (mySwitch.available()) {
   
    int value = mySwitch.getReceivedValue();
   
    if (value == 0) {
      Serial.print("Unknown encoding");
    } else {
      sensorcode = type_sensor , (mySwitch.getReceivedValue());
      Serial.println (sensorcode) ;
    }
    delay(3000);
      Serial.println("No_rf");
    mySwitch.resetAvailable();
  }
}


Probably very stupid but it only show 1 without the rest of the code

What i want is if code is 45454545 change it to 145454545
Thanks
Mic

Delta_G

What is this line supposed to do?  What's that comma supposed to mean?

Code: [Select]
sensorcode = type_sensor , (mySwitch.getReceivedValue());

Since type_sensor = 1, right now it is setting sensorcode to 1.  Which is what you say you see in the Serial Monitor.  So that looks like it is doing exactly what you told it to.

If you just want to add a 1 in front of 45454545, then try 45454545 + 100000000.  That equals 145454545 all day long in any language.

econjack

What is this line supposed to do:

Code: [Select]
      sensorcode = type_sensor , (mySwitch.getReceivedValue());

KeithRB

Not quite, Delta_G.
The comma operator "A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand." (From K&R)

So, sensorcode will equal (mySwitch.getReceivedValue()).

Delta_G


Not quite, Delta_G.
The comma operator "A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand." (From K&R)

So, sensorcode will equal (mySwitch.getReceivedValue()).


So it is.  I had that backwards.  I thought it kept the one on the left.

Still, it isn't doing any good for the OP.

smallpoul

The thing is that the code length is not always equal, so 1000000 + sensor code does not work, is there a function that i could use. It simple, all i want is to add 1 before the code to identify what kind of sensor is it.

1ChicagoDave


The thing is that the code length is not always equal, so 1000000 + sensor code does not work, is there a function that i could use. It simple, all i want is to add 1 before the code to identify what kind of sensor is it.


So, what Is the range of digits possible?

PaulS

Quote
all i want is to add 1 before the code to identify what kind of sensor is it.

What is going to happen when you have 10 of these sensors?

Printing "1," or "27, " before the sensor value makes more sense than trying to manipulate the sensor value.

1ChicagoDave


Quote
all i want is to add 1 before the code to identify what kind of sensor is it.

What is going to happen when you have 10 of these sensors?

Printing "1," or "27, " before the sensor value makes more sense than trying to manipulate the sensor value.


Nice catch. I totally missed the "identify" comment!  :smiley-red:

So just add this
Code: [Select]


Serial.print("1");


Before this
Code: [Select]

      Serial.println(sensorcode) ;

KeithRB

Code: [Select]

if (sv < 10) {
  sv = sv+10;
} else if (sv < 100) {
  sv = sv+100;
} else if (sv < 1000) {
  sv = sv+1000;
} else if (sv< 10000) {
// ...
} else {
  sv = sv + 1000000;
}
[\code]

holmes4

1000000 needs to be long or you will get all kinds of odd things happening.

Try the following.

Code: [Select]
void setup() {
  // put your setup code here, to run once:
  int i = 1000000;
  Serial.begin(9600);
  Serial.println(i);
  Serial.println(1000000);
  Serial.println(1000000);
}

void loop() {
  // put your main code here, to run repeatedly:

}


Mark

Go Up