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);{ }
}
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.
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
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.