Hey guys, so... Basically my code runs a stepper motor and displays when it's going up and down.
The problem is here:
else if (!apertado2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("--------------");
}
The arduino is ignoring this part of the code and not clearing the display, every time my bool (apertado2) is false nothing happens (it tries to show both texts at the same time because lcd.clear(); doesn't work).
The entire code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
// pins
const int stepPin = 2;
const int dirPin = 6;
const int butPin = 8;
const int butPinl = 9;
//Button State
int butStater = 1;
int butStatel = 1;
int en = 4;
int temp, potVal;
bool apertado = false;
bool apertado2 = false;
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(butPinl,INPUT_PULLUP);
pinMode(butPin, INPUT_PULLUP);
pinMode(en, OUTPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear();
}
void loop() {
butStater = digitalRead(butPin);
if (butStater == LOW) {
apertado = !apertado;
digitalWrite(dirPin,HIGH); //Right
for(int x = 0; x < 200; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
}
else if (apertado == true){
lcd.setCursor(0,0);
lcd.print("Elev. UP");
}
else if (butStater == HIGH){
apertado = !apertado;
}
else if (!apertado){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("--------------");
}
butStatel = digitalRead(butPinl);
if (butStatel == LOW) {
apertado2 = !apertado2;
digitalWrite(dirPin,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
}
else if (apertado2 == true){
lcd.setCursor(0,0);
lcd.print("Elev. DOWN");
}
else if (butStatel == HIGH){
apertado2 = !apertado2;
}
else if (!apertado2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("--------------");
}
}
PS.: "Apertado" means pressed.