I've made a scetch to illustrate my idea, it compiles, but it is not tested.
This is my train of thought:
every second, the number of detected presses, that second gets added to the number of detected presses the previous three seconds.
If the sum of these counters is greater than or equal to the set limit, a led is turned on (and two seconds after, its turned off.)
/
array will be written as { 0 , 0 , 0 } if all indexes are equal to the number 0, if { 1 , 2 , 3 } the number at index 2 == 3
currentcount will be written as cc
Second old+1 presort: cc = 4 { 0 , 0 , 0 } sum 4
Second old+1 postsort: cc = 0 { 0 , 0 , 4 }
Second old+2 presort: cc = 5 { 0 , 0 , 4 } sum 9
Second old+2 postsort: cc = 0 { 0 , 4 , 5 }
Second old+3 presort: cc = 4 { 0 , 4 , 5 } sum 11
Second old+3 postsort: cc = 0 { 4 , 5 , 4 }
Second old+4 presort: cc = 6 { 4 , 5 , 4 } sum 21 //trigger led
Second old+4 postsort: cc = 0 { 5 , 4 , 6 }
#define LED_PIN 13 // choose the pin for the LED
#define INPUT_PIN 1 // choose the input pin (for a pushbutton)
#define LIMIT_COUNT 15
#define INTERVAL_SECONDS 4
#define LED_ON_SECONDS 2
unsigned char ucaCountBuffer[INTERVAL_SECONDS-1];
unsigned char usCurrentCount = 0;
unsigned char ucLast = 0;
unsigned char ucLastLedOn = 0;
boolean bLedOn = false;
void setup()
{
pinMode(LED_PIN, OUTPUT); // declare LED as output
pinMode(INPUT_PIN, INPUT); // declare pushbutton as input
digitalWrite(INPUT_PIN,HIGH); // default to pin to high (internal pullup) [ gnd - switch - inputpin ]
}
void loop()
{
/*
|| if switch is pressed more than LIMIT_COUNT per INTERVAL_SECONDS set LED_PIN HIGH for LED_ON_SECONDS
*/
if( digitalRead(INPUT_PIN) == 0 )
{
usCurrentCount++;
delay(20); //prevent bouncing
}
if( ucLast+1000 >= (millis()/1000) )
{
ucLast = (millis() / 1000);
if( getCount(usCurrentCount) >= LIMIT_COUNT )
{
digitalWrite(LED_PIN,HIGH);
ucLastLedOn = (millis()/1000);
bLedOn = true;
resetArray();
}
rearrange(usCurrentCount);
}
if(bLedOn && (ucLastLedOn + LED_ON_SECONDS >= (millis()/1000)))
{
digitalWrite(LED_PIN,LOW);
bLedOn = false;
}
}
//
unsigned char getCount( unsigned char ucCurrentCount )
{
for( unsigned char i=0; i<INTERVAL_SECONDS-1; i++)
{
ucCurrentCount + ucaCountBuffer*;*
- }*
- return ucCurrentCount;*
}
void rearrange( unsigned char ucCurrentCount )
{
- for( unsigned char i=0; i<INTERVAL_SECONDS-1; i++)*
- {*
- if(i==INTERVAL_SECONDS-2)*
- {*
_ ucaCountBuffer = ucCurrentCount; //push last seconds presses in to buffer_
* ucCurrentCount = 0; //reset counter, preparing it for the next second*
* }*
* else*
* {*
_ ucaCountBuffer + ucaCountBuffer[i+1]; //put the number of hits for the next second into this second.
* }
}
}
[/quote]
PostScriptum:
*_</em> <em><em>*void resetArray() { for( unsigned char i=0; i<INTERVAL_SECONDS-1; i++) { ucaCountBuffer[i] = 0; } }*</em></em> <em>_*
*_