Stopwatch beginner

Hello, I am going to make a "reaction test" when pressing a button, a led should light up and then change colour or something, when the light changes colour or turns off (idk what soulution) a timer should start and stop when the button is pressed again.

Right now I just want to get a working stopwatch, if it is possible i would want SecondsSeconds.Thousandths. But only hundreths would work too.

I am new but how do I get started, I want to learn coding not just copy pasting a code.

Help would be appreciated.

This seems to be becoming a FAQ.

The way to do it is as follows:

uint32_t nMillisTimer = 0;

void loop()
{
  if ((nMillisTimer == 0) && (digitalRead(nButtonPin) == HIGH))
  {
     nMillisTimer = millis() + 30000;
     digitalWrite(nLEDPin, HIGH);
}
else if (millis() >= nMillisTimer)
{
    digialWrite(nLEDPin, LOW);
    nMillisTimer = 0;
}

Okay but the coding behind it? Could you explain or add these //?

Victor_hv:
Okay but the coding behind it? Could you explain or add these //?

When is the assignment due?

Lucky me, I don't have an assignment, I just wanna learn and make it. But just pasting a code feels kind of boring.

Plus if I wanna make another program or develop this code right now I have no idea what everything menas. I knoow the very basic but not much more.

The millis command seems to be a frequent command (tbh idk if command is the right word..) but i honestly don't understand

millis() returns the number of milliseconds since the processor reset.
It just counts up, forever.

If you read and record the value of millis() NOW, and read and record the value of millis() NOW, then subtract the first value from the second, then the difference is the number of milliseconds between the two events.

Okay, I kinda starts to understand, if you could, could you add “comments” like // to the code someone sent in the thread or maybe you know/have a code that is something like this one that has “comments” so I maybe could see and understand?

This unadorned snippet does a basic stopwatch function:

uint16_t accValue;
uint32_t previousMillis;
const byte inputPin = 8;

void setup() {
  Serial.begin(115200);
  pinMode(inputPin, INPUT_PULLUP);
}

void loop() {

  if (digitalRead(inputPin) == true) { // pushbutton not pressed
    previousMillis = millis();
  }
  else if (previousMillis != millis()) {
    accValue++;
    Serial.println(accValue);
  }
}

Think of it as an event counter.

Start your education by reading up on the explanations of the keywords and functions used at the Arduino reference page.

There's an ocean of C++ info out there. Find a C++ tutorial/website you're comfortable with to refer to as you go along. Example: Reference - C++ Reference.

dougp:
This unadorned snippet does a basic stopwatch function:

uint16_t accValue;

uint32_t previousMillis;
const byte inputPin = 8;

void setup() {
  Serial.begin(115200);
  pinMode(inputPin, INPUT_PULLUP);
}

void loop() {

if (digitalRead(inputPin) == true) { // pushbutton not pressed
    previousMillis = millis();
  }
  else if (previousMillis != millis()) {
    accValue++;
    Serial.println(accValue);
  }
}

In what units does it measure time?

Okay Thx Will check it out