r/ArduinoProjects • u/UnusualNarwhal6388 • Dec 30 '23
Arduino code
I need help with a arduino code, i wanted to make a "Lie detektor" but it only makes the red led glow can some one help please.
// Include the necessary libraries
#include <LiquidCrystal.h>
// Define the pins used for the LCD, LED, and buzzer
const int lcd_rs = 12, lcd_en = 11, lcd_d4 = 5, lcd_d5 = 4, lcd_d6 = 3, lcd_d7 = 2;
const int red_led = 6;
const int green_led = 7;
const int buzzer = 8;
// Create an instance of the LCD
LiquidCrystal lcd(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7);
// Create a random number
int randomNumber = random(1, 2);
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
// Initialize the pins
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
// Check the random number
if (randomNumber == 1) {
// Turn on the red LED and buzzer
digitalWrite(red_led, HIGH);
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
// Display the message on the LCD
lcd.clear();
lcd.print("Du lugst");
} else {
// Turn on the green LED
digitalWrite(green_led, HIGH);
// Display the message on the LCD
lcd.clear();
lcd.print("Du lugst nicht");
}
delay(1000);
}
2
u/molon-labe1991 Dec 31 '23
The random() function is called only once in the setup, so the randomNumber variable doesn't change during the execution of the program. To fix this issue, you should move the randomNumber = random(1, 3); line inside the loop() function, ensuring that a new random number is generated in each iteration of the loop.