Witam, napisałem program Arduino, ale nie wiem czy dobrze. Bardzo proszę o pomoc i ewentualne poprawienie tego programu. Ma działać on tak, że Arduino losuje liczbę od 1 do 50, ja encoderem mam tak jakby wpisać moją jakąś wymyśloną liczbę przez kręcenie w prawo i w lewo, zatwierdzić ją klikiem na encoderze, a program ma na wyświetlaczu LCD pokazać czy to co wpisałem jest za mało, czy za dużo, czy to jest ta liczba losowana przez program. Dodaję mój program
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
long randNumber;
LiquidCrystal_I2C lcd(0x3F,16,2);
#define outputA 2
#define outputB 3
#define przycisk 4
int counter=0;
int aState;
int aLastState;
void setup()
{
pinMode(outputA, INPUT);
pinMode(outputB, INPUT);
pinMode(przycisk, INPUT);
aLastState = digitalRead(outputA);
randomSeed(analogRead(0));
}
void loop()
{
lcd.init();
lcd.backlight();
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("Losuje liczbe");
lcd.setCursor(0,1);
lcd.print("od 1 do 50");
int randNumber;
randNumber = random(1,51);
lcd.setCursor(0,0);
delay(7500);
lcd.clear();
lcd.print("Pomysl o liczbie");
lcd.setCursor(0,1);
lcd.print("od 1 do 50");
delay(6000);
int mojaLiczba=0;
while (randNumber!=mojaLiczba)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("wpisz liczbe");
lcd.setCursor(0,1);
aState = digitalRead(outputA);
if (aState != aLastState){
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("");
Serial.println(counter);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(counter);
}
aLastState = aState;
if (przycisk == HIGH){
mojaLiczba = counter;
if (mojaLiczba>randNumber) {
lcd.clear();
lcd.print("To za duzo");
delay(4000);
}
else if (mojaLiczba<randNumber){
lcd.clear();
lcd.print("To za malo");
delay(4000);
}}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Gratuluje to");
lcd.setCursor(0,1);
lcd.print("jest ta liczba");
delay(8000);
}