strcmp numerical values
Posted: Thu Jun 08, 2017 8:02 am
Hi,
Hopefully someone can help. Ive been beating my head against this for hours now. I am receiving data feeds via MQTT. Values ranging from 0.00 - 15.00.
I have the below code setup to compare the last read feed and my pre defined number. If the last read feed is above or below my desired number i would like it to turn on or off some outputs.
The issues i have at the moment is it appears the code is only comparing the first digit of the received value (e.g 3 if the received value was 31.08). If the value received was 31.08 it would only write outputs 2 & 13. Yet if the number was 9 outputs 2, 13, 4 & 5 would be on.
Im sure its something simple around how its looking at the numbers. Any help would be greatly appreciated,
Hopefully someone can help. Ive been beating my head against this for hours now. I am receiving data feeds via MQTT. Values ranging from 0.00 - 15.00.
I have the below code setup to compare the last read feed and my pre defined number. If the last read feed is above or below my desired number i would like it to turn on or off some outputs.
The issues i have at the moment is it appears the code is only comparing the first digit of the received value (e.g 3 if the received value was 31.08). If the value received was 31.08 it would only write outputs 2 & 13. Yet if the number was 9 outputs 2, 13, 4 & 5 would be on.
Im sure its something simple around how its looking at the numbers. Any help would be greatly appreciated,
Code: Select all
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &uv_index_feed) {
Serial.print(F("Got uv index: "));
Serial.println((char *)uv_index_feed.lastread);
} else if(subscription == &errors) {
Serial.print(F("ERROR: "));
Serial.println((char *)errors.lastread);
} else if(subscription == &throttle) {
Serial.println((char *)throttle.lastread);
} if (strcmp((char *)uv_index_feed.lastread,"0" ) >= 0) {
digitalWrite(2, LOW);
} if (strcmp((char *)uv_index_feed.lastread,"3") >= 0) {
digitalWrite(13, LOW);
} if (strcmp((char *)uv_index_feed.lastread,"3") < 0) {
digitalWrite(13, HIGH);
} if (strcmp((char *)uv_index_feed.lastread,"6") >= 0) {
digitalWrite(4, LOW);
} if (strcmp((char *)uv_index_feed.lastread,"6") < 0) {
digitalWrite(4, HIGH);
} if (strcmp((char *)uv_index_feed.lastread,"8") >= 0) {
digitalWrite(5, LOW);
} if (strcmp((char *)uv_index_feed.lastread,"8") < 0) {
digitalWrite(5, HIGH);
} if (strcmp((char *)uv_index_feed.lastread,"11") >= 0) {
digitalWrite(14, LOW);
} if (strcmp((char *)uv_index_feed.lastread,"11") < 0) {
digitalWrite(14, HIGH);
}
}