Go Down

Topic: I2C Display Problem (Read 112 times) previous topic - next topic

lukaszte

May 17, 2019, 03:07 am Last Edit: May 17, 2019, 04:54 am by lukaszte

Hey guys, so... Basically my code runs a stepper motor and displays when it's going up and down.

The problem is here:
 
Code: [Select]
  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:
Code: [Select]

#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.

J-M-L

I gave up trying to read the code on my mobile, not indented properly so can't trace well your if-else structures

Before claiming clear() dose not work, have you tested a simple program?

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.clear();
}

void loop() {
  lcd.print("Hello!");
  delay(1000);
  lcd.clear();
  delay(1000);
}

This should blink "Hello!" On the screen if all is wired right
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

lukaszte

I gave up trying to read the code on my mobile, not indented properly so can't trace well your if-else structures

Before claiming clear() dose not work, have you tested a simple program?

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.clear();
}

void loop() {
  lcd.print("Hello!");
  delay(1000);
  lcd.clear();
  delay(1000);
}

This should blink "Hello!" On the screen if all is wired right
The blink code works perfectly. Sorry, seems like my code is a bit messed up, I'm pretty new to all this stuff.

J-M-L

#3
May 17, 2019, 08:41 am Last Edit: May 17, 2019, 08:42 am by J-M-L
Ok no need to be sorry - we all started somewhere

Lesson learnt is that before thinking a library function is broken - esp one that is used so widely - it is better to challenge one's own code and logic :)

I would suggest you press ctrl-T in the IDE to correctly indent the code and have a look at your logic.

It's also easy with plenty of nested  if/else to have cases in the wrong place because you closed a } bracket in the wrong place.

You could also edit your first post to paste the indented code here,  will be easier to read for everyone

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up