Timer auto cut out

Hello there,

I am currently working on a project that is using mills to count between button presses, is there a way to have a high limit on that, say after 5000ms it automatically cycles the program as if the button was pressed?

void loop()
{
if (timerRunning == 0 && digitalRead(button) == HIGH){
startTime = millis();
timerRunning = 1;
}
if (timerRunning == 1 && digitalRead(button) == LOW){
endTime = millis();
timerRunning = 0;
duration = endTime - startTime;
lowspeed = analogRead(POTL);
highspeed = analogRead(POTH);
Serial.print("LOW ");
Serial.print(lowspeed);
Serial.print("\t\t");
Serial.print("HIGH ");
Serial.println(highspeed);Serial.print ("time in milliseconds: ");
Serial.println (duration);
}

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


Please explain a bit more.

include <Adafruit_DS3502.h>
Adafruit_DS3502 ds3502 = Adafruit_DS3502();
#define WIPER_VALUE_PIN A0

byte button = 2;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
byte timerRunning;
const int POTL = 1;  // Pot on Analog Pin 1
const int POTH = 2;  // Pot on Analog Pin 2
int lowspeed = 0;    // Low speed set
int highspeed = 0;   // High Speed set
int lowrpm = 0;      // Low RPM kick in
int highrpm = 0;     // High RPM kick in

void setup() {
  pinMode (button, INPUT);
  Serial.begin(9600);
  while (!Serial) {
    delay(1);
  }

  Serial.println("Adafruit DS3502 Test");

  if (!ds3502.begin()) {
    Serial.println("Couldn't find DS3502 chip");
    while (1);
  }
  Serial.println("Found DS3502 chip");
}
void loop()
{
  if (timerRunning == 0 && digitalRead(button) == HIGH) {
    startTime = millis();
    timerRunning = 1;
  }
  if (timerRunning == 1 && digitalRead(button) == LOW) {
    endTime = millis();
    timerRunning = 0;
    duration = endTime - startTime;
    lowspeed = analogRead(POTL);
    highspeed = analogRead(POTH);
    Serial.print("LOW ");
    Serial.print(lowspeed);
    Serial.print("\t\t");
    Serial.print("HIGH ");
    Serial.println(highspeed); Serial.print ("time in milliseconds: ");
    Serial.println (duration);
  }
}

Make/start a 5 second TIMER then when you set timerRunning = 1.

When the TIMER finishes, you can do what needs to be done.


unsigned long timerMillis;
. . .
//not tested

  if (timerRunning == 0 && digitalRead(button) == HIGH) {
    startTime = millis();
    timerRunning = 1;
    //start the 5 second TIMER
    timerMillis = millis(); 
  }

  if(timerRunning == 1 && millis() - timerMillis >= 5000)
  {
    timerRunning = 0;
    //do your stuff here

  }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.