Need help with a finished reaction test code

hi guys, i'm a beginner and i don't understand the code completely. for example, why do you use a "!" in front of digitalRead? can someone explain this to me? thanks.

#include <Arduino.h>

#define pinLatch 4    // Pin for latch signal of 7-segment display
#define pinClk 7      // Pin for clock signal of 7-segment display
#define pinData 8     // Pin for data signal of 7-segment display

#define mainLED 13    // Pin for main LED
#define secLED 11     // Pin for secondary LED

#define button1 A1    // Pin for button 1
#define button2 A2    // Pin for button 2

long reaction;        // Variable to store the reaction start time
long reactionTime;    // Variable to store the calculated reaction time
long reactionPoint;   // Variable to store the random reaction point

const byte segmentMap[] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0X80, 0X90 };  // Segment patterns for digits 0-9
const byte digitSegMap[] = { 0xF1, 0xF2, 0xF4, 0xF8 };  // Digit selection for each segment position

void setup() {
  pinMode(pinLatch, OUTPUT);
  pinMode(pinClk, OUTPUT);
  pinMode(pinData, OUTPUT);

  pinMode(mainLED, OUTPUT);
  pinMode(secLED, OUTPUT);

  randomSeed(analogRead(A5));
  writeDigitToSegment(3, 0);  // Display initial digit 0 on the 7-segment display

  reactionPoint = random(2000, 5001);  // Generate random reaction point between 2000ms and 5000ms

  // Turn off all LEDs
  digitalWrite(mainLED, HIGH);
  digitalWrite(secLED, HIGH);
}

void loop() {
  if (!digitalRead(button1)) {

    digitalWrite(mainLED, LOW);

    reaction = millis();

    // Wait for the reaction time or until button2 is pressed
    while (reaction + reactionPoint > millis() && digitalRead(button2)) {}

    if (!digitalRead(button2)) {
      // Calculate the reaction time and make it positive
      reactionTime = ((millis() - reaction) - reactionPoint) * -1;

      // Display the negative reaction time indefinitely (before the LED turned ON)
      while (true) {
        writeDigitToSegment(0, reactionTime / 1000);
        writeDigitToSegment(1, (reactionTime / 100) % 10);
        writeDigitToSegment(2, (reactionTime / 10) % 10);
        writeDigitToSegment(3, reactionTime % 10);
      }
    }

    digitalWrite(mainLED, HIGH);
    digitalWrite(secLED, LOW);

    reactionTime = millis();

    // Wait until button2 is pressed
    while (digitalRead(button2)) {}

    // Calculate the reaction time (after the LED turned ON)
    reactionTime = millis() - reactionTime;

    // Display the positive reaction time indefinitely
    while (true) {
      writeDigitToSegment(0, reactionTime / 1000);
      writeDigitToSegment(1, (reactionTime / 100) % 10);
      writeDigitToSegment(2, (reactionTime / 10) % 10);
      writeDigitToSegment(3, reactionTime % 10);
    }
  } else {
    return;
  }
}

void writeDigitToSegment(byte segment, byte value) {
  digitalWrite(pinLatch, LOW);
  // Shift out the segment pattern and digit selection to the 7-segment display
  shiftOut(pinData, pinClk, MSBFIRST, segmentMap[value]);
  shiftOut(pinData, pinClk, MSBFIRST, digitSegMap[segment]);
  digitalWrite(pinLatch, HIGH);
}

You use it to show you don't know that that you shouldn't use a Boolean operation on a non-Boolean value.
But it'll work. For now.

I have already tested it with a board and it works perfectly. But for example, why are the LEDs switched off with HIGH instead of LOW? and generally I don't understand everything from the loop onwards very well.

Why shouldn't they be?

What's the point of the assignment here?

thank you for the help.
The game is started with the first button.
The time until the LEDs change from mainLED (D13) to secLED (D11) two to five seconds after the start of the reaction test.
As soon as it is green, the player has to press the second button as quickly as possible.
The 7-segment display shows the response time in milliseconds.
If the player clicks too early, the time will be extended. So there is no point in simply clicking on it to see what appears to be a short response time.
The test is repeated with the reset button.

Let me put it another way: when is the assignment due to be submitted?

oh that's what you mean :smiley: does not have to be submitted.

OK. So explain why you think a pin should be HIGH to turn an LED on

I don't know. We always did it that way at school. however, we also used PULLUPs there

No, you used pullups on inputs.

Have another guess.

I mean Pulldown

Again, you used pulldowns on inputs.

You're running out of guesses.

I got it. The digitalRead() function reads the state of a digital input pin and returns either HIGH or LOW depending on the voltage level detected. However, the code is using a pull-down configuration for the buttons, which means the buttons are normally HIGH (not pressed) and go LOW when pressed.

To accommodate this configuration, the code checks if the button is not pressed by using !digitalRead(button1). This condition will evaluate to true when the button is pressed (because digitalRead(button1) returns LOW), and false when the button is not pressed (because digitalRead(button1) returns HIGH).

It should be pointed-out that you have no documented initialisation of the inputs, so they are just that; defaulted to inputs with no internal pullups or pulldowns.

You haven't shared a schematic, so let's assume they're floating.

You haven't said whether the switches are NO or NC either.

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