Basic project start

I am starting a project that requires that when a switch is clicked on 15 times or greater in 4 seconds then it turns an LED on for about 2 seconds. And if it is less than 15 times per 4 seconds then it remains off. I am wirelessly controling the switch using an RC helicopter. The reciever has its own power source. I cut the wires going from the receiver to the motor and placed one on ground and the other in Pin 1 . I have worked off the button example and got it to turn on and off in this order using the new switch I made connected to the remote.

int ledPin = 13; // choose the pin for the LED
int inputPin = 1; // choose the input pin (for a pushbutton)
int x =3; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}

void loop(){
x = digitalRead(inputPin); // read input value
if (x == LOW) { // check if the input is HIGH
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}

I am just confused on where and how to get the simple calculation to activate the LED using the ratio of times button pressed:seconds
Any help is greatly appreciated. This is my first time looking at this and I am very confused but working my way through it.

Thank you.

This is a lot more difficult than you might think and is defiantly not a beginners problem.

Having said that what you want is a buffer (long int array) noting the time, from millis(), every time the switch makes a transition, that is an edge is detected. This is different to just seeing a level logic level.

The array needs to be filled as a circular buffer. Then on each new entry you need to subtract the time value of now from the time value of the last entry in the buffer, that is 15 samples ago. If this exceeds 4 seconds turn your output off otherwise turn it on.

One cause for concern is if it a mechanical switch then contact bounce might be a problem.

Thank you so much.

Definitly helps me narrow down what I have to do. I am still alittle lost and was wondering if my switch would need to be put in as a analog or stay as a digital the way I have it written already. If it is a digital then where would I insert what you have given me or what should I replace.

Digital is fine. However if you look at the code you will see that each time round the loop it is turning the LED on or off every time, whether it needs altering or not. Therefore you need to rewrite most of your loop() function.

So the first thing to do is to write the code so that it just detects the change. This requires you to remember the state of the switch last time round the loop and only turn on (or off) when the current state of the switch is not the same as the last state.

Then you have a place to put your timer information. But get that bit going first and then get back with what you have. Note it will look like it is behaving exactly like the code you have now but you will only do the digitalWrite() when you need to.

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>_**_

Hi AlphaBeta,
The first thing I can see about your code is that if the button is held down continually it will just keep on counting.

The problem with the algorithm is that it divides the time into second slots, therefore if a rapid burst comes in half way through a second you will miss the significance of it.

The press-and-hold will indeed trigger multiple increments, this is however not a big problem. Only need one additional bool.

As far as the other comment

'if a rapid burst comes in half way through a second you will miss the significance of it.'

I do not understand how you reasoned to that conclusion.
Could you explain this into a bit more detail? I want to learn as much as possible, so it would've been greatly appreciated.

Thanks guys I really appreciate your help. I tried what alphabeta posted out on my board and it seems that it didn't work as accuarate. I tried moving things around but I failed.hahaha. So I am going to ask a question of grumpy_mike. See this is for a class I have for design. I am the only person attempting at arduino and let alone no prior lessons or knowledge. My teacher who is very knowledgable in Arduino is out of town until my presentation tomorrow. Which was not informed to me. So I am flying completely blind as you can see. So I am not the person to ask for this much help but I was wondering grumpy_mike if you could help me sketch up a program to get this thing up and running. It seems you know exactly what to do and where and it blows my mind how you know this. I really want to learn this program and maybe by seeing what you have sketched for a project that I understand inside and out I will finally understand how to do it in the future. I really appreciate all the help you guys have given to me. Thanks a lot. PLEASE HELP :cry:

OK, one thing I am reluctant to do is to do your assignment for you. I know I can do the assignment but that doesn't help you. Looking at what I have done will no doubt be educational for you but not nearly so educational as you doing it yourself. I am very willing to help you in this, that is I want to help but not do it for you. It's a fine line. I have done a sort of flow diagram of what you need to do. Basically it is what I told you but in a perhaps easer to understand form. Have a look at that and write some code and if it doesn't work I can point out where you are going wrong. I am sure you will learn a lot more that way. Here is the picture:-

or directly at:-
http://www.flickr.com/photo/3095398500_7b98117c28_o.jpg

The other point:-

I do not understand how you reasoned to that conclusion

Suppose that half way through a second you started to get pulses at a rate of 15 per second, you would only count 7 of them. When this was weighted into the average after three seconds there would be insufficient to tip the balance to light the lamp. What you would have to do with your code is have three full seconds of pulses at this rate to turn on the light even though the pulses have been going at that rate for three and a half seconds. If you don't consider that to be an issue consider the problem if the time was not three seconds but three minutes. Basically it would not respond as specified in the requirements.

given:
| = pulse
|---|---|--|-|-|-|||--|-|--|---|----|----|
first second 9 | next second 6

I think my idea would've responded correctly to increses in clicks per second, but it would split the registered amount over two seconds.
And that is not wrong, because as long as both halves of the burst is inside the 4 seconds limit both will be accounted for, but as soon as the first half is out of bounds it will not be accounted for. This happens in your solution as well. With great accurasy, mine operates as you know, at second resolution.

When all this is said, ofcourse the solution with a circular buffer is much more accurate, it is also much more complex and may be not too easy to implement, especially if one is new to the arduino.

If original poster want to pursue the circular buffered, more accurate solution, and this project is amongst you first. I would reccomand reading up on arrays, for/while/ loops and switch-case. All these topics can be found in the reference.

Good luck!

Thank you so much. I know I have to learn it on my own.Just got desperate because the product is due Thursday morning. Ok I still am totaly confused and I am having computer beginner trouble with just understand how to find the correct terms to use. so here is what I have so far from piecing together what I can. I understand that what you gave me is pretty much spilled out and I can follow it but I just can't figure out how to find the correct comman to place in the sketch. let me know

int ledPin = 13; // choose the pin for the LED
int inputPin = 1; // choose the input pin (for a pushbutton)
int x = 0; // variable for reading the pin status

int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}

void loop()
{
x = digitalRead(inputPin);

{
if (x == HIGH)
state = HIGH; //yes switchState equals lastSwitchState
else
state = LOW;
}
{
if (state == LOW) //if switchState ends up equaling LOW
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);

Good, I have made some changes to make it work. When posting code copy it from the Arduino environment, then click the hash box (9 from the right) and paste the code in between the bracket expressions that pop up.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 1;               // choose the input pin (for a pushbutton)
int x = 0;                      // variable for reading the pin status

int state = LOW;               // the current state of the output pin
int reading;                    // the current reading from the input pin
int previous = LOW;             // the previous reading from the input pin
long time, lastTime = 0;

void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop()
{
 x = digitalRead(inputPin);
 
 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 // just for now turn on LED if the push button was greater than 1 second from previous one
 if(time>1000) digitalWrite(ledPin,HIGH); else  digitalWrite(ledPin,LOW);
 // wait a second so as not to send massive amounts of data
 // delay(1000); no need for delay as we are not printing every time
}

Now this times how long it is between two successive presses of the switch and lights the LED if it is longer than 4 seconds. Or at least it should do I haven't run it.
Get that going, look at the flow diagram and see if you can figure out the next stage.

Hint for homework look up about arrays.

ok so I tried this out and got a lot from it. I changed somethings around got different effects to work and failed a lot also but I understand exactly what happened. Except when looking back at the flow chart when is says to enter numbers or count to 15 I am confused if the program should come back with values. I checked out the array tutorials and it seemed like the basic ones were repeating so i took a stab at repeating what you had sketched earlier. I changed it from 15HIGHS to 3HIGHS and 4seconds to 1seconds just to keep the numbers down(it can even be 2seconds if you think that is easy to learn from.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 1;               // choose the input pin (for a pushbutton)
int x = 0;                      // variable for reading the pin status

int state = LOW;               // the current state of the output pin
int reading;                    // the current reading from the input pin
int previous = LOW;             // the previous reading from the input pin
long time, lastTime = 0;

int result;
int sample [i} = 0;

void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop()
{
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time "); 
 //prints time since last low started
 Serial.println(time);
 time=sample[i];
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 if (x==previous) {
 
 }
   else {
     time=millis() - lastTime;
     lastTime =millis()-time;
     Serial.print("Time ");
   }
    if (x==previous){

      }
else {
     time=millis() - lastTime;
     lastTime =millis();
     Serial.print("Time ");

   result=millis()-sample[i];
   }
   
 // just for now turn on LED if the push button was greater than 1 second from previous one
 if(time>4000) digitalWrite(ledPin,HIGH); 
 else  
 digitalWrite(ledPin,LOW);
 // wait a second so as not to send massive amounts of data
 // delay(1000); no need for delay as we are not printing every time
}

Thanks again for all your help!!! Hopefully I will get this because I am running out of time. But I have a back up plan to fake it for the crit. Of course I will continue at it until I get it and understand every little detail, but i would sure love to show something that works on its own. I'm ready for your next direction of guidence.

whats does the comment after previous = x mean?  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning

whats does the comment after previous = x mean?

Make the variable "previous" the same as the current state (the variable X) so that next time round the loop if nothing else has changed then don't do anything.

Think of the loop() as always spinning round, what we have to do is to catch when things change and do stuff, otherwise keep the plates spinning (going round loop).

Now what you code does at:-

previous = x; // make previous as current state so you don't do this the next time through with nothing chaning
}
if (x==previous) {

Is wrong, that if statement is always true because if x is not equal to previous it will be set to be so by the line above. Try looking at the instructions step by step and see.

The code I posted was not meant to be an implementation of the flow diagram but a half way structure for you to code.

Now on to arrays. An array is a stack of variables what precise variable you access is controlled by another variable. So while result[2] will always access the same slot in memory or variable the statement result will access a variable determined by the variable i (I use i for index but it could be anything)
So to keep a track of results (the time a push button event occurred) that happen in the past we need a stack of variables. As we are storing time these need to be long integers and we want 15 of them.
So at the start let's declare an array and an index to access it:-
long result[15];
int count=0;
These can be accessed by a variable ranging over the values 0 to 14.
So each time we see the button down for the first time we can record it with:-
result[count]=millis();
now we have to move the counter on ready for the next time we see the button down, with:-
count = count + 1; // or count++; for short
Finally as we will run out of slots in our array if we don't do anything about it we use:-
if(count>14) count=0;
In that way count "wraps round" when it reaches the end of it's range and we have created what is called a circular buffer.
The clever thing about this is once we have wrapped round for the first time, ever after that the variable "count" points at the result we took 15 samples ago.
Therefore all we need to do every time round the loop is to see if the result we took 15 samples ago is different by greater than or less than four seconds and light our lamp accordingly.
So to summarise only record the time when we see the button down. But each time round the loop check that the time 15 "button down events" ago is longer than the interval we want to detect.
Have a go at coding that.
Best of luck :slight_smile:

i feel real stupid here is what I think is what you were talking about. I have been running on no sleep for the last 2 days trying to learn this program and make the model and get my boards ready. Here is one I tried but doesn't work and I will post another that I tried with is longer but also doesn't work maybe you could help me correct it. I guess where i get confused is where exactly to add features when reading it on the forum vs looking at it as a sketch.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 1;               // choose the input pin (for a pushbutton)
int x = 0;                      // variable for reading the pin status

int state = LOW;               // the current state of the output pin
int reading;                    // the current reading from the input pin
int previous = LOW;             // the previous reading from the input pin
long time, lastTime = 0;
long result[15];
int count = 0;


void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop()
{
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[i]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
  count++; 
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
 count++;
 result[i]=millis();
 
  count++;
 result[i]=millis();
 
 if(count>14) count=0;
 
  if(time>4000) digitalWrite(ledPin,HIGH); else  digitalWrite(ledPin,LOW);
delay(1000);
int ledPin = 13;                // choose the pin for the LED
int inputPin = 1;               // choose the input pin (for a pushbutton)
int x = 0;                      // variable for reading the pin status

int state = LOW;               // the current state of the output pin
int reading;                    // the current reading from the input pin
int previous = LOW;             // the previous reading from the input pin
long time, lastTime = 0;
long result[15];
int count = 0;


void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop()
{
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[0]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[1]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[3]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[4]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[5]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[6]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[7]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[8]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[9]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[10]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[11]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[12]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[13]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 }
 count++; 
 {
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 time = millis() - lastTime; // record how long it has been since the last push low
 lastTime = millis();
  Serial.print("Time: ");
 //prints time since last low started
 Serial.println(time);
 result[14]=millis();
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
 if(count>14) count=0;
 }

if(count>14) count=0;{
 // just for now turn on LED if the push button was greater than 1 second from previous one
 if(time>4000) digitalWrite(ledPin,HIGH); else  digitalWrite(ledPin,LOW);}
 // wait a second so as not to send massive amounts of data
 // delay(1000); no need for delay as we are not printing every time

Your not keeping the plates spinning, it's much easier that you think. Just check for the light at the end of the loop that's all you need to do. You only increment count when something as happened, that is only once in the loop.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 1;               // choose the input pin (for a pushbutton)
int x = 0;                      // variable for reading the pin status

int state = LOW;               // the current state of the output pin
int reading;                    // the current reading from the input pin
int previous = LOW;             // the previous reading from the input pin
long result[15];
int count = 0;


void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop()
{
 x = digitalRead(inputPin);

 if (x == previous) {
                     // switchState equals lastSwitchState so nothing to do here
 }
 else {    // something has changed since the last time round the loop so you can do something
 result[count] = millis(); // record what time it is for this push
  previous = x;  // make previous as current state so you don't do this the next time through with nothing chaning
  count++;
  if(count>14) count=0;
 }
// now calculate if you need to turn on the light
 
 if((millis() - result[count]) > 4000) {
  // turn light off
 }
else
{
  // turn light on
}
 
}