Pages: [1]   Go Down
Author Topic: input and if  (Read 822 times)
0 Members and 1 Guest are viewing this topic.
Sweden, Malmö
Offline Offline
Full Member
***
Karma: 2
Posts: 200
Rooky
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could anybody give me a hint on how to solve this.
Below you can find the virtual vire code for RX.
I am reciving the message "hello 6" and trying to do something when buf[6] is equal to 6.
Could it be that i am not converting the value so it can be compared?
When i serial print the buf[6] it prints 6 but when i compare it in the "if" code it does not see it as a 6.

Code:

 
#include <VirtualWire.h>

#undef int
#undef abs
#undef double
#undef float
#undef round


void setup()
{
Serial.begin(9600);
Serial.println("setup");
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}


void loop()
{
  
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) {
   int i;
   Serial.print("Got: ");
  
    for (i = 0; i < buflen; i++) {
     Serial.print(buf[i], BYTE);
     Serial.print(" ");
    }
  
   Serial.println("");
  
   Serial.println(buf[6]);
    if (buf[6] == 6) { Serial.println("ON"); }
    
  }
  
}
« Last Edit: August 12, 2009, 05:57:36 am by HULK » Logged

Sweden, Malmö
Offline Offline
Full Member
***
Karma: 2
Posts: 200
Rooky
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple solution, found it on my own.
The solution is to write:

Code:
if (buf[6] == '6') {
instead of

Code:
if (buf[6] == 6) {

I do not technically why but it works.
« Last Edit: August 12, 2009, 06:18:54 am by HULK » Logged

Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 218
You will become one with the Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
buf[6] == 6
is checking to see if the character has ASCII value 6.

Code:
buf[6] == '6'
is checking if the character is the character 6.
« Last Edit: August 12, 2009, 09:23:05 am by Spinlock » Logged

Pages: [1]   Go Up
Jump to: