triggerable counters

I used the millis counting sketch and found it counted the pulses I supplied to it.

Unfortunately it does this immediately after either pressing the reset button or uploading the sketch.

I would like to be able to have the sketch running and press a button to initiate counting as and when required.

I understand that this millis code fixes the period once in the void settup and this is then applied as the processor scans round and round the void loop until no longer true.

Have I hit up against a defined limitation of processors?

Do I need a full on computer to do this?(Lots of unrelated things going on at the same time capability).

your ref UK HeLiBob oct 2 2017: using millis for timing

I used the millis counting sketch and found it counted the pulses I supplied to it.

It is not clear which sketch you are referring to. Can you please post it here.

millis() is just a 'hidden' function that uses the timer0 overflow interrupt to increment a counter (timer0_millis). You can start and stop the counting by enabling/disabling the interrupt {TIMSK0 = 1;}/{TIMSK0 = 0;} and resetting timer0_millis any time you want. Just bear in mind all the other 'hidden' functions that use this value.

Here is your original code as requested.

unsigned long periodStartMillis;
unsigned long currentMillis;
const unsigned long period = 5000; //period during which button input is valid
const byte buttonPin1 = A1; //button on pin A1
byte currentButtonState;
byte previousButtonState;
int count = 0;
boolean printFinalMessage = true;
unsigned long debounceStartMillis;
unsigned long debouncePeriod = 20;
boolean debouncing = false;

void setup()
{
Serial.begin(115200);
pinMode(buttonPin1, INPUT_PULLUP);
Serial.println(“Press the button as many times a possible in 5 seconds”);
periodStartMillis = millis();
}

void loop()
{
currentMillis = millis();
if (currentMillis - periodStartMillis <= period) //true until the period elapses
{
previousButtonState = currentButtonState; //save the previous button state
currentButtonState = digitalRead(buttonPin1); //read the current state of the input
if (currentButtonState != previousButtonState) //if the button state has changed
{
debounceStartMillis = currentMillis; //save the time that the state change occured
debouncing = true; //flag that debouncing in progress
} //end state change check

if (currentMillis - debounceStartMillis >= debouncePeriod) //if the debounce period has elapsed
{
if (debouncing == true) //debouncing taking place
{
if (currentButtonState == LOW) //if the button is currently pressed
{
debouncing = false; //debouncing is finished
count++; //increment the count
Serial.println(count);
} //end count increment
} //end debouncing in progress check
} //end debounce time elapsed check
} //end timing period check
else //period has ended
{
if (printFinalMessage == true)
{
Serial.println(“Time is up”);
Serial.print("Button pressed count : ");
Serial.println(count);
printFinalMessage = false; //prevent the final message being displayed again
} //end printing final message
} //end final message check
}

periodStartMillis is initiated once in void setup. I am puzzling over weither the initiation of the count can be done with the program running rather than once in the void setup. My aim is to trigger counting when ever I want and as often as I want with the void loop running. As it is it runs the void setup once and then void loops ,counting, until the period expires. I have tried all sorts of programming gambits but it seems to come down to periodStartMillis being settup before the loop commences otherwise it will be re-initiated every time the void loop is run.

My aim is to trigger counting when ever I want and as often as I want with the void loop running.

One way to do this is to use a boolean variable to control whether counting is taking place. Set it to true when you want to count and false when you don't. This allows you to count only when you want to.

What sets the boolean to true to start counting is up to you. It could, for instance, be a button press, an external signal, the output of a sensor or time to name a few.