Loading...
Pages: [1]   Go Down
Author Topic: "if...don't send" serial question  (Read 256 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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5166
CMiYC
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not use a simple if-statement?

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

www.cmiyc.com - A guide to being an Enginerd

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: 50
Posts: 6540
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

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Pages: [1]   Go Up
Print
 
Jump to: