Go Down

Topic: input and if (Read 968 times) previous topic - next topic

HULK

Aug 12, 2009, 12:56 pm Last Edit: Aug 12, 2009, 12:57 pm by HULK Reason: 1
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: [Select]


#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"); }
   
 }
 
}

HULK

#1
Aug 12, 2009, 01:18 pm Last Edit: Aug 12, 2009, 01:18 pm by HULK Reason: 1
Simple solution, found it on my own.
The solution is to write:

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

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

I do not technically why but it works.

Spinlock

#2
Aug 12, 2009, 04:22 pm Last Edit: Aug 12, 2009, 04:23 pm by Spinlock Reason: 1
Code: [Select]
buf[6] == 6
is checking to see if the character has ASCII value 6.

Code: [Select]
buf[6] == '6'
is checking if the character is the character 6.

Go Up