Measuring time

Hello fellow arduinos,

I am a newbie to the arduino world, but c++ is something I used to love during my hughschool years (13 years ago …). I am currently working on a project for college gradution.

I am working with adressable LEDs and beginning to understand it.
I need help on the following topic :

by default, when my program starts, I have to light up a certain led module (4 WS2812B leds on a sigle circuit board ) in a certain colour. The LED module is phisically attached to a pressable lamp. On the back side of the lamp, a normal open contact closes when the lamp is pressed and commands the led module to power off. I have to measure the time ( in miliseconds ) that passes from the moment the led is lit to the moment the contact closes and print it to the serial monitor for now. Later on, I will have to connect the project to a client by bluetooth and expand functionality.

Please help !
thanks in advance !

here is my sketch for now :

  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    //digitalWrite(ledPin, HIGH);
        strip.setPixelColor(0, 0, 255, 0);
//        delay(1000);
        strip.setPixelColor(1, 0, 255, 0);
        strip.setPixelColor(2, 0, 255, 0);
        strip.setPixelColor(3, 0, 255, 0);
//        delay(1000);
        strip.show();
        
      } 
  else {
    // turn LED off:
    //digitalWrite(ledPin, LOW);
        strip.setPixelColor(0, 0, 0, 0);
//        delay(1000);
        strip.setPixelColor(1, 0, 0, 0);
        strip.setPixelColor(2, 0, 0, 0);
        strip.setPixelColor(3, 0, 0, 0);
//        delay(1000);
        strip.show();
  }

You can use a variable in which you store the value of the millis() function when the led is switched on in your code.

Next when you find that the button is pressed to switch the LED off, you subtract the previously recorded value from the current millis() to get the duration.

Note
Please edit your post and add
** **[code]** **
before your code and
** **[/code]** **
after your code.

The suggestions above are on point...

However, a major flaw in your code means it will only be checking if the switch is closed for a couple of microseconds every two seconds.
Read up on millis() timing, and ‘state-change’ detection for the switch.

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