r/ArduinoProjects • • 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 Upvotes

8 comments sorted by

View all comments

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.