Go Down

Topic: Comparing data input with itself? (Read 722 times) previous topic - next topic


Hi I have a sensor input with a continuous random stream until it receives a good signal, which it then spits out a short burst of the same value.  I need to somehow use that burst to trigger an output. 

For example, if I wanted to check for 3 or 4 values in the input stream that were the same, that could trigger my output, to turn on an LED or whatever.  I'm stuck on this one.



Hmmm....neat problem. Thinking out loud:

Code: [Select]
#define NUM_VALUES 4   // How many values should be the same
#define VALTYPE uint8_t         // Replace with whatever type your values are

uint8_t valIndex = 0;

void addValue(VALTYPE value) {
  values[valIndex++] = value;
  if (valIndex == NUM_VALUES) valIndex = 0;

uint8_t checkValues(void)
  uint8_t i;

  for (i=0; i < NUM_VALUES-1; i++) {
    if (values[i] != values[i+1]) return 0;

  return 1;

In your main loop you would call addValue() whenever you got a new sensor reading, and checkValues() when you wanted to see if the last NUM_VALUES values were all the same.

The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons


Use "timer = millis()" to note when the signal last changed.  Look for (millis() - timer) > 100 to see if the signal has been steady for more then 0.1 seconds.  Adjust the threshold to whatever time interval distinguishes 'random' from  'a short burst'.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Another way is to subtract the current value from the last value. When this spits out say three zeros on the run you have your reading.

Go Up