Pages: [1]   Go Down
Author Topic: "if...don't send" serial question  (Read 543 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not use a simple if-statement?

Code:
if (val > 0) {
  Serial.write(val);
}
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, it looks simple enough.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: