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);
}
1
u/EmielDeBil Dec 30 '23
random(1,2) will slways return 1 as the second argument is exclusive. Try random(1,3).
2
u/UnusualNarwhal6388 Dec 30 '23
thanks it works
4
u/EmielDeBil Dec 30 '23
Actually, it is cleaner to do random(0,2) and check for 0 and 1. In computers, we start counting from 0 and the 2 is then also more logical.
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.
1
u/dedokta Dec 30 '23
You should use a touch sensor as a switch so when you place your finger on the sensor it triggers an answer.
1
Dec 31 '23
I had some coding issues last week and I asked ChatGPT in Bing (they have the latest version). I explained the problem to it and gave my code. ChatGPT was surprisingly helpful and fast in helping me out, just a tip for if you run into coding issues further on in your learning.
4
u/Lopsided_Gas_181 Dec 30 '23
maybe just stop lying? :D