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.