Using millis() for a timer switch

Hi,
I am beinfg very stupid and cannot for the life of me figure out a bit of simple code. I want to read a sensor and if it is HIGH then start a millis() timer for a configurable number of seconds and switch something on then when the timer finishes switch the thing off.

Could any kind soul offer me some sample code.

Many thanks,
Flyhighmike

Let’s do it this way.

You show us your attempt so we can see where you are going astray.

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.

Split the project in 2 pieces.
Number one is to learn how to read the sensor.
Number 2 is to explore the example code "Blinking an LED using millis.

const unsigned long eventInterval = 10000;
unsigned long previousTime = 0;
int val = 0;

void setup() {
  Serial.begin(19200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  Serial.println("Restart");
}

void loop() {

  int sensorVal1 = digitalRead(2);
  int sensorVal2 = digitalRead(3);

  if (sensorVal1 == HIGH or sensorVal2 == HIGH) {
    Serial.println("Low");
    //Call here
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    //Do Nothing
    digitalWrite(LED_BUILTIN, LOW);
  }

  /* Updates frequently */
  unsigned long currentTime = millis();

  /* This is the event */
  if (currentTime - previousTime >= eventInterval) {
    /* Event code */
    Serial.println("Test Seconds = " + String(eventInterval / 1000) + "  -  " + String(sensorVal1));
    /* Update the timing for the next time around */
    previousTime = currentTime;
  }
}

Try this:

void loop()
{
  int sensorVal1 = digitalRead(2);
  int sensorVal2 = digitalRead(3);

  if (sensorVal1 == HIGH or sensorVal2 == HIGH)
  {
    // If not alrteady on...
    if (digitalRead(LED_BUILTIN) == LOW)
    {
      // ...turn on and start timer
      digitalWrite(LED_BUILTIN, HIGH);
      previousTime = currentTime;
    }
  }

  /* Updates frequently */
  unsigned long currentTime = millis();

  /* After scheduled time, turn off */
  if (digitalRead(LED_BUILTIN) == HIGH
      && currentTime - previousTime >= eventInterval)
  {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

Hi John,

/* Fastled Timer

  v0.01 - Basic Timer Setup 29Jan22
  v0.02 - Add Fastled Project code - tryout
*/

// Sensor & Timer variables
const unsigned long eventInterval = 15000;
unsigned long previousTime = 0;
unsigned long currentTime = 0;
int val = 0;

// Fastled variables
#include "FastLED.h"
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif

#define DATA_PIN    7
//#define CLK_PIN   4
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    95
#define BRIGHTNESS  100
CRGB leds[NUM_LEDS];


void setup()
//Sensor and timer setup
{
  Serial.begin(19200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  //Fastled Setup
  delay(3000); // 3 second delay for recovery
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
  .setCorrection(TypicalLEDStrip)
  .setDither(BRIGHTNESS < 100);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
  delay(1500);
  FastLED.clear();
  FastLED.show();

  Serial.println("Setup Complete");
}



void loop()
{
  int sensorVal1 = digitalRead(2);
  int sensorVal2 = digitalRead(3);

  if (sensorVal1 == HIGH or sensorVal2 == HIGH)
  {
    // If not alrteady on...
    if (digitalRead(LED_BUILTIN) == LOW)
    {
      // ...turn on and start timer
      digitalWrite(LED_BUILTIN, HIGH);
      previousTime = currentTime;
      Serial.println("Test Seconds = " + String(eventInterval / 1000) + String(currentTime));
      pride();
      FastLED.show();
    }
  }

  /* Updates frequently */
  currentTime = millis();

  /* After scheduled time, turn off */
  if (digitalRead(LED_BUILTIN) == HIGH
      && currentTime - previousTime >= eventInterval)
  {
    digitalWrite(LED_BUILTIN, LOW);
    FastLED.show();
    FastLED.clear();
    FastLED.show();
    Serial.println("Test Seconds = " + String(eventInterval / 1000) + String(currentTime));
  }
}

// This function draws rainbows with an ever-changing,
// widely-varying set of parameters.
void pride()
{
  static uint16_t sPseudotime = 0;
  static uint16_t sLastMillis = 0;
  static uint16_t sHue16 = 0;

  uint8_t sat8 = beatsin88( 87, 220, 250);
  uint8_t brightdepth = beatsin88( 341, 96, 224);
  uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  uint8_t msmultiplier = beatsin88(147, 23, 60);

  uint16_t hue16 = sHue16;//gHue * 256;
  uint16_t hueinc16 = beatsin88(113, 1, 3000);

  uint16_t ms = millis();
  uint16_t deltams = ms - sLastMillis ;
  sLastMillis  = ms;
  sPseudotime += deltams * msmultiplier;
  sHue16 += deltams * beatsin88( 400, 5, 9);
  uint16_t brightnesstheta16 = sPseudotime;

  for ( uint16_t i = 0 ; i < NUM_LEDS; i++) {
    hue16 += hueinc16;
    uint8_t hue8 = hue16 / 256;

    brightnesstheta16  += brightnessthetainc16;
    uint16_t b16 = sin16( brightnesstheta16  ) + 32768;

    uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
    uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
    bri8 += (255 - brightdepth);

    CRGB newcolor = CHSV( hue8, sat8, bri8);

    uint16_t pixelnumber = i;
    pixelnumber = (NUM_LEDS - 1) - pixelnumber;

    nblend( leds[pixelnumber], newcolor, 64);
  }
}

Thanks so much for the help. I have managed to get a lot further but am stuck again. As you will see from the code the project is driving FastLED neopixels. I have got it to switch on and off OK but all that happens is the lights turn on at whatever point they are in the FastLED Pride function and wait for the alloted time and then switch off. Do I need to change something for a while loop?```

  // When ON
  if (digitalRead(LED_BUILTIN) == HIGH)
  {
    pride();

    /* After scheduled time, turn off */
    if (currentTime - previousTime >= eventInterval)
   {
    digitalWrite(LED_BUILTIN, LOW);
    FastLED.show();
    FastLED.clear();
    FastLED.show();
    Serial.println("Test Seconds = " + String(eventInterval / 1000) + String(currentTime));
  }

Never! :astonished:

Thank you John - that is now working and very much appreciated