Go Down

Topic: "if...don't send" serial question (Read 594 times) previous topic - next topic

icek

I have a data range of 0-180 that is being read via analog sensors which I send via serial to another Arduino board.  I would like to reduce the traffic on the Serial connection by telling the board not to send data if it hits "0".  How would I write the Arduino code to tell it to not send data if 0 is being read?  I have the following so far which will also send "0" due to sending all values:

Code: [Select]
 val = analogRead(fsrOne);
 if (val >= 0 && val < 200) {
   val = map(val, 0, 199, 0, 0);
 }  else if (val >= 200 && val < 800) {
   val = map(val, 200, 799, 20, 120);
 } else if (val >= 800 && val < 1024) {
   val = map(val, 800, 1023, 120, 180);
 }
 
 Serial.write(val);     // the raw analog reading

James C4S

Why not use a simple if-statement?

Code: [Select]

if (val > 0) {
 Serial.write(val);
}
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

icek


zoomkat

I've done something similar in the past by setting the last value sent in a variable, and then comparing the new value to the previous value sent. If there is no change, then nothing was sent. Only a different value got sent to cut down on traffic.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up