/*
Arduino | hardware-help
klausliker421 - Tuesday, March 24, 2026 8:31 PM
not to what i'm connecting it to
but the lcd works fine if the solenoids aren't plugged in
it's just that when they are it freaks out
so something about the extra stuff the solenoid/relay setup
does corrupts the data that the arduino sends to the lcd through the data pins
*/
#include <LiquidCrystal.h>
#include "StopwatchLib.h"
const int NUM_BTNS = 2;
const int BTN_PINS[] = {6, 5};
//const int BUTTON2 = 5;
const int RELAY1 = 4;
const int RELAY2 = 3;
const int DETECTOR1 = A0;
const int DETECTOR2 = A1;
const int RS = 12, EN = 11, D4 = 10, D5 = 9, D6 = 8, D7 = 7;
bool isRelay1 = false;
bool isRelay2 = false;
int score = 0;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
Stopwatch stopwatch;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
btnState[i] = digitalRead(BTN_PINS[i]); // check each button
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == 0) { // was just pressed
btnPressed = i + 1;
}
delay(20); // debounce
}
}
return btnPressed;
}
void showSplash() {
lcd.setCursor(1, 0);
lcd.print("klausliker421");
lcd.setCursor(5, 1);
lcd.print("Device");
delay(2000);
lcd.clear();
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(BTN_PINS[0], INPUT_PULLUP);
pinMode(BTN_PINS[1], INPUT_PULLUP);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
showSplash();
lcd.setCursor(0, 0);
lcd.print("Relay 1 ");
lcd.setCursor(0, 1);
lcd.print("Relay 2 ");
//stopwatch.Reset();
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber) {
if (btnNumber == 1) {
isRelay1 = !isRelay1;
} else if (btnNumber == 2) {
isRelay2 = !isRelay2;
}
digitalWrite(RELAY1, isRelay1);
digitalWrite(RELAY2, isRelay2);
lcd.setCursor(9, 0);
lcd.print(isRelay1 ? "On " : "Off");
lcd.setCursor(9, 1);
lcd.print(isRelay2 ? "On " : "Off");
}
}
/*
//Serial.println(temp);
//if(analogRead(DETECTOR1) >= 1023)
if (digitalRead(BUTTON1) == LOW)
{
stopwatch.Update();
if (stopwatch.GetElapsed() > 750000)
{
score++;
//lcd.setCursor(0, 1);
//lcd.print("a");
stopwatch.Reset();
}
digitalWrite(RELAY2, HIGH);
//delay(50);
}
else
{
digitalWrite(RELAY2, LOW);
}
//if(analogRead(DETECTOR2) >= 1023)
if (digitalRead(BUTTON2) == LOW)
{
stopwatch.Update();
if (stopwatch.GetElapsed() > 750000)
{
score++;
//lcd.setCursor(0, 1);
//lcd.print(score);
stopwatch.Reset();
}
digitalWrite(RELAY1, HIGH);
//delay(50);
}
else
{
digitalWrite(RELAY1, LOW);
}
//Serial.println(analogRead(DETECTOR1));
if (analogRead(DETECTOR1) >= 1015)
{
lcd.clear();
lcd.setCursor(0, 0);
int rand = random(0, 1000);
lcd.print("game over");
lcd.print(rand);
//delay(25);
}
//lcd.setCursor(0,1);
//lcd.print(analogRead(DETECTOR1) + "-------");
}
*/