Pages: [1]   Go Down
Author Topic: add number to long int  (Read 568 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
    all i want is to add a number before the code that the sensor send. here the code:
Code:
/*
  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
« Last Edit: September 10, 2013, 01:30:06 pm by smallpoul » Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Cincinnati, OH
Online Online
God Member
*****
Karma: 42
Posts: 704
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What is this line supposed to do:

Code:
      sensorcode = type_sensor , (mySwitch.getReceivedValue());
Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()).
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Serial.print("1");

Before this
Code:
      Serial.println(sensorcode) ;
Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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]
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try the following.

Code:
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
Logged

Pages: [1]   Go Up
Jump to: