Problems with making a reflex test

We are trying to make a reflex test with an arduino. We got 3 lights which will turn on randomly. When one will turn on, you have to press a button and your reflex time will be printed to a LCD.
Our code is currently not working, there seems to be a problem with the code but we can't find where exactly. We are using the I2C libary by Frank de Brabander for the LCD.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
long randNumber;
long randdelay;
unsigned long starttijd = millis();
int knop1=2;
int knop2=3;
int knop3=4;
int lamp1=11;
int lamp2=12;
int lamp3=13;
int  knopStatus1 = digitalRead(knop1);
int  knopStatus2 = digitalRead(knop2);
int  knopStatus3 = digitalRead(knop3);


void setup() {  
  randomSeed(analogRead(0));
  pinMode(2, INPUT); //de inputs van het knopjes
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(11, OUTPUT); //lampjes
  pinMode(12, OUTPUT);  
  pinMode(13, OUTPUT);
   
  }
void loop(){
  randNumber = random(lamp1,lamp3);

  delay(random(2000,4000));
  digitalWrite(randNumber, HIGH);
while ((knopStatus1) && (lamp1) == HIGH || (knopStatus2) && (lamp2) == HIGH || (knopStatus3) && (lamp3) == HIGH);
{
  unsigned long eindtijd = millis();
  digitalWrite(lamp1, LOW);
  digitalWrite(lamp2, LOW);
  digitalWrite(lamp3, LOW);
  unsigned long ElapsedTime = eindtijd - starttijd;
  lcd.init();
    lcd.clear();          
    lcd.backlight();      
    lcd.setCursor(2,0);    
    lcd.print("time is:");
    lcd.setCursor(2,1);    
    lcd.print(ElapsedTime);
    starttijd = millis(); }
while (knopStatus1 == LOW || knopStatus2 == LOW || knopStatus3 == LOW);{ }
}

Increase the chance of getting help by saying what's not happening that should be, and /or what's happening that shouldn't be...

For a start, read here and see that random's upper limit is exclusive, so 1 less than the value given...

Parameters

min: lower bound of the random value, inclusive (optional).
max: upper bound of the random value, exclusive.

Returns

A random number between min and max-1. Data type: long.

How can we help if you dont say what parts you are using? Provide a link for the LCD please, and explain what is "not working" ie the "lamps" or the LCD display.

Shouldn't this be in loop()?

We are using a 16x2 LCD. We got the code from: https://lastminuteengineers.com/i2c-lcd-arduino-tutorial/
We tried checking if the arduino loops fully trough the program by using the serial monitor. It does go trough the loop but no light comes on. I suspect its a fault in the code but it might also be a fault in the wiring as im not that experienced. The LCD is not working either but that might be a result of a faulty code.
Also I dont think the variables for knopstatus should be in a loop because those variables only have to be created once but i will give it a try next time i get the chance to work on it

Does that also apply when the random function includes variables because if i would use
'''' randNumber = random(lamp0,lamp4);
I dont think it would work because the variables lamp0 and lamp 4 are never mentioned

Think again.

Think what their values might be.

You have some semicolons in places that while syntactically correct make the program function differently to what (I think) you intend.

while (condition) { do stuff}

is the pattern you want.

while (condition); { do stuff}

is seen by the compiler and sharp eyes IRL as

while (condition);


{do stuff}

if the condition is true, it hangs forever at the while statement.

If the condition is false, the {do stuff} block is executed once.

HTH

a7

oh yes now i get it. Thanks, I will try next time I get the chance to work on it

Thanks alot, I think this is what was going wrong because in earlier versions of the code, everything worked except for the functions. I will update you when I can work on it.

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