0

I am using the VirtualWire library on my Arduino Micro. I am having trouble in comparing the string of HEX received via rf module on pin 2 of the board. When reaching if stringOne == stringVal2, LED3 is always lit. I am not sure where to go from here or where to even begin on reading to figure out converting HEX to a readable comparator.

#include <VirtualWire.h>
#define RX 2
#define LED A0
#define LED2 PIND7
#define LED3 PIND6

void setup(){
  Serial.begin(9600);
  Serial.print("setup complete");
  pinMode(RX, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  vw_set_rx_pin(RX);
  vw_setup(2000);
  vw_rx_start();
}

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], HEX);
      //String stringVal = String('65'+'6c'+'6c'+'6f'+'20'+'*','\0');
      String stringVal2 = String('73'+'6F'+'20'+'6C'+'6F'+'6E'+'*', HEX);
      String stringOne = String(buf[i], HEX);
      Serial.print(stringOne);
      Serial.print(' ');
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED,LOW);
      delay(1000);
      if (stringOne == stringVal2){
        digitalWrite(LED2, HIGH);
      }
      else{
        digitalWrite(LED3, HIGH);
      }
    }
    Serial.println();

  }
}

Here is the code for my transmitter as well. Using Adafruit Trinket5v 8Mhz

#include <VirtualWire.h>

const int TX  = 3;
const int LED = 2;
const int buttonPin = 0; //Yellow Button
const int buttonPin2 =4; //Red Button
int buttonState = 0;
int buttonState2 = 0;

void setup(){
  vw_set_tx_pin(TX);
 // vw_set_ptt_pin(txpin);
 // vw_set_ptt_inverted(false);
  vw_setup(2000);
  pinMode(LED, OUTPUT); //Signals button press/transmission being sent
  pinMode(buttonPin, INPUT);
}

byte count = 1;

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(LED, HIGH);
    char msg[7] = {'h','e','l','l','o',' ','#'};
    msg[6] = count;
    vw_send((uint8_t *)msg, 7);
    vw_wait_tx();
    count = count + 1;  
  } 
  else {
    // turn LED off:
    digitalWrite(LED, LOW); 
  }

  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH){
    digitalWrite(LED, HIGH);
    char msg[7] = {'12','11','10','9','8','7','6'};
    msg[6] = count;
    vw_send((uint8_t *)msg, 7);
    vw_wait_tx();
    count = count + 1;
  }
  else {
    digitalWrite(LED, LOW);
  }
}
7
  • What is the result of println(stringVal2) and println(stringOne) just before that if? Commented Oct 27, 2014 at 11:57
  • println(stringVal2) yields 73 6F 20 6C 6F 6E println(stringOne) yields 73 6f 20 6c 6f 6e (count). The characters in stringOne are lowercase. If I use Serial.print(buf[i], HEX) instead of stringOne the characters are capitalized in serial monitor vs lowercase when printing as a string. I also included the transmitter code in my first post above so you can understand fully what is transpiring. Commented Oct 27, 2014 at 16:28
  • Is "(count)" part of the output? Looks like the formatting is wrong. You are just printing/storing characters. Commented Oct 27, 2014 at 17:28
  • Also, your hex converts to "so lon". Are you sending something different than the code mentions? Commented Oct 27, 2014 at 17:32
  • (count) is part of the output. it is the last bit in the message and is incremented every time a new message is sent. I think I have it figured out. My goal is to have each button press send a unique message and based on the message the receiver performs different tasks. I changed the message length to 'code' char msg[1] = {'55'}; 'code' then commented out msg[6] = count and changed the byte amount in vw_send to 1 and my if statements are occurring now. Commented Oct 27, 2014 at 17:57

1 Answer 1

0

You are comparing a string that is just text to an array of chars. These will never evaluate out properly

If you want to initialise the stringVal2 properly to compare it directly you need to do this:

char strVal[] = {0x73, 0x6f, 0x20, 0x6c, 0x6f, 0x6e};
String stringVal2 = String(strVal);

This prints out the same as the other string you brought in.

Also, since you are basically just catching a char array then you might as well use a char array directly to evaluate the results and convert to a string when you need to output to a human

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.