Pages: [1]   Go Down
Author Topic: Comparing data input with itself?  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Chicago
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Thanks,
Brian
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

VALTYPE values[NUM_VALUES];
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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8937
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 631
Posts: 34496
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: