LCD, Pressure Sensor, Switch, and Millis/Delay??

Hello! I am working on a project, and three of the main components are a three way switch, a pressure sensor and a LCD screen. How it works is that depending on the setting of the switch, the LCD screen displays certain things.

Switch is up - displays certain set of instructions
Switch is middle - shows certain screen (this is good, it’s the other two options that do not work)
Switch is down - displays a different set of instructions

There are a few problems with my current code:

If you switch the switch to the “up” position, the screen will not update until it cycles through completely (current code = 20 seconds). The pressure applied needs to update every half second, and the screen needs to be able to change instantaneously on the flick of the switch, and not be delayed 20 seconds. You can take a look at the code I have. I know I need to use millis for the LCD instructions, I’m just not sure how I should go about implementing it.

//LIBRARIES
#include <LiquidCrystal_I2C.h>
#include <Wire.h>  
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

//VARIABLES
int light = 5; //LED that lights up when too much pressure is applied
int up = 6; //switch pin
int down = 7; //switch pin
int pot = A1; //potentiometer pin
int potval; //value of poteniometer
const int maxpressure = 80; //Analog reading of most pressure allowed to be applied 

void setup() {
 
 //START SERIAL
 Serial.begin(9600);
 
 //DECLARATIONS
 pinMode(light, OUTPUT);
 pinMode(up, INPUT);
 pinMode(down, INPUT);
 
 //INITIAL STATES
 digitalWrite(up, HIGH);
 digitalWrite(down, HIGH);
 
 //INITIAL LCD DISPLAY
 lcd.begin(20,4);
 lcd.setCursor(0,0); 
 lcd.print("Hello");
 delay(5000);
}

void loop() {
 
 //PRINT POTENTIOMETER READING (initial testing, to be changed)   
 potval = analogRead(pot); 
 potval = map(potval,0,1023,0,100);
 Serial.println("Potentiometer Reading: "); 
 Serial.println(potval); 
 
 //DECLARE PRESSURE READINGS
 int pressure = analogRead(A0);
 pressure = map(pressure,0,1023,0,100);
 Serial.println("Pressure Reading: "); 
 Serial.println(pressure);
 
 //LED TURNS ON IF TOO MUCH PRESSURE
 if(pressure>maxpressure)
 {
   digitalWrite(light, HIGH);
 }
 else
 {
   digitalWrite(light,LOW);
 }

//GOING UP
lcd.clear();
 if(digitalRead(up)==LOW)
 {
   lcd.backlight();
   lcd.setCursor(0,0);
   lcd.print("Instructions for");
   lcd.setCursor(0,1); 
   lcd.print("going up");  
   lcd.setCursor(0,3);
   lcd.print("Pressure:"); 
   lcd.setCursor(10,3);
   lcd.print(pressure);
   lcd.setCursor(13,3);
   lcd.print("/");
   lcd.setCursor(14,3);
   lcd.print(maxpressure); 
   delay(5000);
   
     lcd.setCursor(0,0);
     lcd.print("                    ");
     lcd.setCursor(0,1); 
     lcd.print("                    ");
     lcd.setCursor(0,0);
     lcd.print("Place strong foot");
     lcd.setCursor(0,1); 
     lcd.print("on next stair");    
     delay(5000);
     
       lcd.setCursor(0,0);
       lcd.print("                    ");
       lcd.setCursor(0,1); 
       lcd.print("                    ");
       lcd.setCursor(0,0);
       lcd.print("Place weak foot");
       lcd.setCursor(0,1); 
       lcd.print("on next stair"); 
       delay(5000);
       
         lcd.setCursor(0,0);
         lcd.print("                    ");
         lcd.setCursor(0,1); 
         lcd.print("                    ");
         lcd.setCursor(0,0);
         lcd.print("Place cane");
         lcd.setCursor(0,1); 
         lcd.print("on next stair");     
         delay(5000);
 }

//GOING DOWN
lcd.clear();
 if(digitalRead(down)==LOW)
 {
   lcd.backlight();
   lcd.setCursor(0,0);
   lcd.print("Instructions for");
   lcd.setCursor(0,1); 
   lcd.print("Going down");  
   lcd.setCursor(0,3);
   lcd.print("Pressure:"); 
   lcd.setCursor(10,3);
   lcd.print(pressure);
   lcd.setCursor(13,3);
   lcd.print("/");
   lcd.setCursor(14,3);
   lcd.print(maxpressure); 
   delay(5000);
   
     lcd.setCursor(0,0);
     lcd.print("                    ");
     lcd.setCursor(0,1); 
     lcd.print("                    ");
     lcd.setCursor(0,0);
     lcd.print("Place cane");
     lcd.setCursor(0,1); 
     lcd.print("on next stair");    
     delay(5000);
     
       lcd.setCursor(0,0);
       lcd.print("                    ");
       lcd.setCursor(0,1); 
       lcd.print("                    ");
       lcd.setCursor(0,0);
       lcd.print("Place weak foot");
       lcd.setCursor(0,1); 
       lcd.print("on next stair"); 
       delay(5000);
       
         lcd.setCursor(0,0);
         lcd.print("                    ");
         lcd.setCursor(0,1); 
         lcd.print("                    ");
         lcd.setCursor(0,0);
         lcd.print("Place strong foot");
         lcd.setCursor(0,1); 
         lcd.print("on next stair");     
         delay(5000);
 }
 
//MIDDLE STATE
lcd.clear();
 if((digitalRead(up)==HIGH)&&(digitalRead(down)==HIGH))
 {
   lcd.setCursor(0,0);
   lcd.print("Pressure:");
   lcd.setCursor(10,0);
   lcd.print(pressure); 
   lcd.setCursor(13,0);
   lcd.print("/");
   lcd.setCursor(14,0);
   lcd.print(maxpressure); 
   lcd.setCursor(0,1);
   lcd.print("1st # = Pressure Now");
   lcd.setCursor(0,2);
   lcd.print("2nd # = Max Pressure");
   lcd.setCursor(0,3);
   lcd.print("Do not exceed 2nd #!");
   delay(800);
   //lcd.noBacklight();
 }
}

The UP and DOWN sections need some work. Print pressure needs to update every second and the delays need to be replaced so that screen can change instantly. I can supply anything else you might need!

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

bump

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks Tom.... :slight_smile:

Picture - sorry its my first time using CAD, hope you can decipher it.

Imgur

lcd.clear();
  if(digitalRead(up)==LOW)
  {
    currentTime= millis(); 
    if (currentTime-startTime > DESIRED) {
    startTime, currentTime = millis();
      while (currentTime-startTime <= DESIRED) {          
      lcd.backlight();
      lcd.setCursor(0,0);
      lcd.print("                    ");
      lcd.setCursor(0,1); 
      lcd.print("                    ");
      lcd.setCursor(0,0);
      lcd.print("Instructions for");
      lcd.setCursor(0,1); 
      lcd.print("going up");  
      lcd.setCursor(0,3);
      lcd.print("Pressure:"); 
      lcd.setCursor(10,3);
      lcd.print(pressure);
      lcd.setCursor(13,3);
      lcd.print("/");
      lcd.setCursor(14,3);
      lcd.print(maxpressure); 
      currentTime = millis();
      }
    startTime = millis(); //Reset start time to count next occurance
    }
    
      currentTime= millis(); 
      if (currentTime-startTime > DESIRED) {
      startTime, currentTime = millis();
        while (currentTime-startTime <= DESIRED) {   
        lcd.setCursor(0,0);
        lcd.print("                    ");
        lcd.setCursor(0,1); 
        lcd.print("                    ");
        lcd.setCursor(0,0);
        lcd.print("Place strong foot");
        lcd.setCursor(0,1); 
        lcd.print("on next stair");    
        }
      startTime = millis(); //Reset start time to count next occurance
      }
      
        currentTime= millis(); 
        if (currentTime-startTime > DESIRED) {
        startTime, currentTime = millis();
          while (currentTime-startTime <= DESIRED) {   
          lcd.setCursor(0,0);
          lcd.print("                    ");
          lcd.setCursor(0,1); 
          lcd.print("                    ");
          lcd.setCursor(0,0);
          lcd.print("Place weak foot");
          lcd.setCursor(0,1); 
          lcd.print("on next stair"); 
          }
        startTime = millis(); //Reset start time to count next occurance
        }

          currentTime= millis(); 
          if (currentTime-startTime > DESIRED) {
          startTime, currentTime = millis();
            while (currentTime-startTime <= DESIRED) {            
            lcd.setCursor(0,0);
            lcd.print("                    ");
            lcd.setCursor(0,1); 
            lcd.print("                    ");
            lcd.setCursor(0,0);
            lcd.print("Place cane");
            lcd.setCursor(0,1); 
            lcd.print("on next stair");     
            }
          startTime = millis(); //Reset start time to count next occurance
          }          
  }

Sorry, whoops. Wrong code.

//LIBRARIES
#include <LiquidCrystal_I2C.h>
#include <Wire.h>  
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

//VARIABLES
int light = 5; //LED that lights up when too much pressure is applied
int up = 6; //switch pin
int down = 7; //switch pin
int pot = A1; //potentiometer pin
int potval; //value of poteniometer
const int maxpressure = 80; //Analog reading of most pressure allowed to be applied 

void setup() {
  
  //START SERIAL
  Serial.begin(9600);
  
  //DECLARATIONS
  pinMode(light, OUTPUT);
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  
  //INITIAL STATES
  digitalWrite(up, HIGH);
  digitalWrite(down, HIGH);
  
  //INITIAL LCD DISPLAY
  lcd.begin(20,4);
  lcd.setCursor(0,0); 
  lcd.print("Hello");
  delay(5000);
}

void loop() {
  
  //PRINT POTENTIOMETER READING (initial testing, to be changed)   
  potval = analogRead(pot); 
  potval = map(potval,0,1023,0,100);
  Serial.println("Potentiometer Reading: "); 
  Serial.println(potval); 
  
  //DECLARE PRESSURE READINGS
  int pressure = analogRead(A0);
  pressure = map(pressure,0,1023,0,100);
  Serial.println("Pressure Reading: "); 
  Serial.println(pressure);
  
  //LED TURNS ON IF TOO MUCH PRESSURE
  if(pressure>maxpressure)
  {
    digitalWrite(light, HIGH);
  }
  else
  {
    digitalWrite(light,LOW);
  }

//GOING UP
lcd.clear();
  if(digitalRead(up)==LOW)
  {
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("Instructions for");
    lcd.setCursor(0,1); 
    lcd.print("going up");  
    lcd.setCursor(0,3);
    lcd.print("Pressure:"); 
    lcd.setCursor(10,3);
    lcd.print(pressure);
    lcd.setCursor(13,3);
    lcd.print("/");
    lcd.setCursor(14,3);
    lcd.print(maxpressure); 
    delay(5000);
    
      lcd.setCursor(0,0);
      lcd.print("                    ");
      lcd.setCursor(0,1); 
      lcd.print("                    ");
      lcd.setCursor(0,0);
      lcd.print("Place strong foot");
      lcd.setCursor(0,1); 
      lcd.print("on next stair");    
      delay(5000);
      
        lcd.setCursor(0,0);
        lcd.print("                    ");
        lcd.setCursor(0,1); 
        lcd.print("                    ");
        lcd.setCursor(0,0);
        lcd.print("Place weak foot");
        lcd.setCursor(0,1); 
        lcd.print("on next stair"); 
        delay(5000);
        
          lcd.setCursor(0,0);
          lcd.print("                    ");
          lcd.setCursor(0,1); 
          lcd.print("                    ");
          lcd.setCursor(0,0);
          lcd.print("Place cane");
          lcd.setCursor(0,1); 
          lcd.print("on next stair");     
          delay(5000);
  }

//GOING DOWN
lcd.clear();
  if(digitalRead(down)==LOW)
  {
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("Instructions for");
    lcd.setCursor(0,1); 
    lcd.print("Going down");  
    lcd.setCursor(0,3);
    lcd.print("Pressure:"); 
    lcd.setCursor(10,3);
    lcd.print(pressure);
    lcd.setCursor(13,3);
    lcd.print("/");
    lcd.setCursor(14,3);
    lcd.print(maxpressure); 
    delay(5000);
    
      lcd.setCursor(0,0);
      lcd.print("                    ");
      lcd.setCursor(0,1); 
      lcd.print("                    ");
      lcd.setCursor(0,0);
      lcd.print("Place cane");
      lcd.setCursor(0,1); 
      lcd.print("on next stair");    
      delay(5000);
      
        lcd.setCursor(0,0);
        lcd.print("                    ");
        lcd.setCursor(0,1); 
        lcd.print("                    ");
        lcd.setCursor(0,0);
        lcd.print("Place weak foot");
        lcd.setCursor(0,1); 
        lcd.print("on next stair"); 
        delay(5000);
        
          lcd.setCursor(0,0);
          lcd.print("                    ");
          lcd.setCursor(0,1); 
          lcd.print("                    ");
          lcd.setCursor(0,0);
          lcd.print("Place strong foot");
          lcd.setCursor(0,1); 
          lcd.print("on next stair");     
          delay(5000);
  }
  
//MIDDLE STATE
lcd.clear();
  if((digitalRead(up)==HIGH)&&(digitalRead(down)==HIGH))
  {
    lcd.setCursor(0,0);
    lcd.print("Pressure:");
    lcd.setCursor(10,0);
    lcd.print(pressure); 
    lcd.setCursor(13,0);
    lcd.print("/");
    lcd.setCursor(14,0);
    lcd.print(maxpressure); 
    lcd.setCursor(0,1);
    lcd.print("1st # = Pressure Now");
    lcd.setCursor(0,2);
    lcd.print("2nd # = Max Pressure");
    lcd.setCursor(0,3);
    lcd.print("Do not exceed 2nd #!");
    delay(800);
    //lcd.noBacklight();
  }
}

Try something like this :

unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long seconds = 0;


void setup() {

}

void loop()
{

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 1000) {
    previousMillis = currentMillis;
    seconds ++
  }

  if ( seconds <= 5 )            //
  {
    display_your first message
  }

  if ( seconds > 5 && seconds <= 10 )
    (
      display_your second message
  }

if ( seconds > 10 && seconds <= 15 )
{
  display_your third message
}

if (seconds >= 20)
{
  lcd.clear();
  seconds = 0 ;
}
}

@Waski

You solved one problem but caused another!

Solved: I can now switch screens at any time instantaneously

Problem: The screen keeps refreshing itself so you cant read what’s going on (you know like it blinks too fast because no delay)

Current code:

//GOING UP THE STAIRS 
lcd.clear();
  if(digitalRead(up)==LOW)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= 1000) 
    {
    previousMillis = currentMillis;
    seconds ++;
    }
    if ( seconds <= 5 ) 
    {
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("                    ");
    lcd.setCursor(0,1); 
    lcd.print("                    ");
    lcd.setCursor(0,0);
    lcd.print("Instructions for");
    lcd.setCursor(0,1); 
    lcd.print("going up");  
    lcd.setCursor(0,3);
    lcd.print("Pressure:"); 
    lcd.setCursor(10,3);
    lcd.print(pressure);
    lcd.setCursor(13,3);
    lcd.print("/");
    lcd.setCursor(14,3);
    lcd.print(maxpressure); 
    }
    
      if ( seconds > 5 && seconds <= 10 )
      {
      lcd.setCursor(0,0);
      lcd.print("                    ");
      lcd.setCursor(0,1); 
      lcd.print("                    ");
      lcd.setCursor(0,0);
      lcd.print("Place strong foot");
      lcd.setCursor(0,1); 
      lcd.print("on next stair");    
      }
      
        if ( seconds > 10 && seconds <= 15 )
        {
        lcd.setCursor(0,0);
        lcd.print("                    ");
        lcd.setCursor(0,1); 
        lcd.print("                    ");
        lcd.setCursor(0,0);
        lcd.print("Place weak foot");
        lcd.setCursor(0,1); 
        lcd.print("on next stair"); 
        }
        
          if ( seconds > 15 && seconds <= 20 )
          {
          lcd.setCursor(0,0);
          lcd.print("                    ");
          lcd.setCursor(0,1); 
          lcd.print("                    ");
          lcd.setCursor(0,0);
          lcd.print("Place cane");
          lcd.setCursor(0,1); 
          lcd.print("on next stair");     
          }
            if ( seconds >= 20)
            {
              lcd.clear();
              seconds = 0;
            }

This isn’t all of it, just a section from void loop

If I add a delay of around 800 milliseconds it seems to fix the blinking problem, I'll live with it unless there is a better solution

Do not cross-post. Thread locked.

You can finish the discussion here...