Go Down

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


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) {
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com



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 Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131