4

I'm using App Inventor to continuously send orientation values (as strings) to my Arduino, which are read using Serial.parseInt(). Using simple IF statements (if value > threshold), I'm trying to turn on an LED and otherwise turn it off.

What I'm getting however, is an LED that goes on and off along with these values, which alternate between the actual and desired values and extreme unwanted values as shown in the picture:

enter image description here

Given that my orientation values, as they are, alternate between -180 to 180, I'm using a simple IF statement that seems to work reasonably well, except when the unwanted readings give inaccurate values that are within my range.


My Arduino code:

#include <SoftwareSerial.h>

const int TXD = 3;
const int RXD = 4;
SoftwareSerial mrBlue(TXD, RXD);

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  mrBlue.begin(9600);
  Serial.setTimeout(20);
  mrBlue.setTimeout(20);
}

void loop() {
  // put your main code here, to run repeatedly:

  if (mrBlue.available() > 0) {

    int data = mrBlue.parseInt();
    if (data > 180 || data < -180) {

    }
    else {
      Serial.println(data);
    }
  }    
} 
3
  • Not sure if this works for your needs, but I usually smooth noisy data by keeping a small buffer of the last 3-5 values and averaging them. A larger buffer gives more stable values at the cost of latency after a legit change. Jul 16, 2016 at 8:29
  • 1
    This data is terribly noisy. My first approach would be to understand why, and then decide whether I could fix it, or have to live with it. It's easy to filter out the out of range data, but why is it there? Is it the sensor? If the sensor is analog, is the noise to due electrical pickup in its leads? Until you know, how can you trust even the apparently "good" data?
    – JRobert
    Jul 16, 2016 at 13:31
  • 1
    @JamesMcManus: With this kind of “wide tail” noise, a running median filter is way more efficient than a running average. Jul 16, 2016 at 20:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.