I know this probably isn't the proper way but I'm Looking for a way to use a delay in a while loop. at the minute when I try and add a delay it skips over it because I'm trying to put the delay between each while command rather than in the middle of the code. I need to do this because I'm trying do cause a debounce affect but even if I use debounce it doesn't read the code between it just skips straight to the next while loop. Any help will be greatly appreciated.
Thank you
void loop()
{
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print("===================="); // write this string on the top row
lcd.setCursor ( 2, 1 ); // go to the top left corner
lcd.print("-=Torsion Rig=-"); // write this string on the top row
lcd.setCursor ( 0, 3 ); // go to the top left corner
lcd.print("===================="); // write this string on the top row
delay(3000);
/*********************************************************/
delay(100);
lcd.clear();
while (buttonState1 == LOW) {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (buttonState3 == HIGH) {
force = force + 1;
delay(50);
}
if (buttonState4 == HIGH) {
force = force - 1;
delay(50);
}
if (force <= 0) {
force = 0;
}
if (force >= 4000) {
force = 4000;
}
String displayforce = String(force);
int len2 = displayforce.length() + 1;
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print("Select Force"); // write this string on the top row
lcd.setCursor ( 0, 1 ); // go to the top left corner
lcd.print("(Newtons)"); // write this string on the top row
lcd.setCursor ( 10, 2 ); // go to the top left corner
lcd.print(displayforce); // write this string on the top row
delay(50);
}
/************************************************************/
delay(100);
lcd.clear();
while (buttonState1 == LOW) {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (buttonState3 == HIGH) {
cycles = cycles + 1;
delay(50);
}
if (buttonState4 == HIGH) {
cycles = cycles - 1;
delay(50);
}
if (cycles <= 1) {
cycles = 1;
}
String displaycycles = String(cycles);
int len2 = displaycycles.length() + 1;
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print("Select Num Cycles"); // write this string on the top row
lcd.setCursor ( 0, 1 ); // go to the top left corner
lcd.print("(Each Way)"); // write this string on the top row
lcd.setCursor ( 10, 2 ); // go to the top left corner
lcd.print(displaycycles); // write this string on the top row
delay(50);
}
Please post the complete program.
You have lots of delay()s in the bit you have posted, most of them in while() loops. Which one(s) do you think it is skipping and how can you tell?
Steve
/*****************************************
name: I2C LCD2004
function: You should now see your I2C LCD2004 display "Hello,world!","IIC/I2C LCD2004"
"20 cols, 4 rows","www.sunfounder.com"
********************************/
//Email:service@sunfounder.com
//Website:www.sunfounder.com
/********************************/
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int buttonPin1 = 11; // the number of the pushbutton pin
const int buttonPin2 = 10;
const int buttonPin3 = 9;
const int buttonPin4 = 8;
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int force = 1000;
int cycles = 1;
/*********************************************************/
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
/*********************************************************/
void loop()
{
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print("===================="); // write this string on the top row
lcd.setCursor ( 2, 1 ); // go to the top left corner
lcd.print("-=Torsion Rig=-"); // write this string on the top row
lcd.setCursor ( 0, 3 ); // go to the top left corner
lcd.print("===================="); // write this string on the top row
delay(3000);
/*********************************************************/
delay(100);
lcd.clear();
while (buttonState1 == LOW) {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (buttonState3 == HIGH) {
force = force + 1;
delay(50);
}
if (buttonState4 == HIGH) {
force = force - 1;
delay(50);
}
if (force <= 0) {
force = 0;
}
if (force >= 4000) {
force = 4000;
}
String displayforce = String(force);
int len2 = displayforce.length() + 1;
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print("Select Force"); // write this string on the top row
lcd.setCursor ( 0, 1 ); // go to the top left corner
lcd.print("(Newtons)"); // write this string on the top row
lcd.setCursor ( 10, 2 ); // go to the top left corner
lcd.print(displayforce); // write this string on the top row
delay(50);
}
/************************************************************/
delay(100);
lcd.clear();
while (buttonState1 == LOW) {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (buttonState3 == HIGH) {
cycles = cycles + 1;
delay(50);
}
if (buttonState4 == HIGH) {
cycles = cycles - 1;
delay(50);
}
if (cycles <= 1) {
cycles = 1;
}
String displaycycles = String(cycles);
int len2 = displaycycles.length() + 1;
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the top left corner
lcd.print("Select Num Cycles"); // write this string on the top row
lcd.setCursor ( 0, 1 ); // go to the top left corner
lcd.print("(Each Way)"); // write this string on the top row
lcd.setCursor ( 10, 2 ); // go to the top left corner
lcd.print(displaycycles); // write this string on the top row
delay(50);
}
/************************************************************/
}
I tried to explain in this diagram below:
I have just sorted the problem. I needed to reread the button state. Thank you for the reply