I am re-working an old project from about 9 years ago, to control/monitor the operation of a machine. I am using a 16x2 lcd with a UNO R3. A n.o. push button on the control box allows the operator to select a mode of operation, from a choice of 6, stepping through each one with a button press. These are labelled firstSequence, secondSequence etc. What I have so far is shown in the code;
#include <Wire.h>
#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int WhichSequence =1; // This variable stores the current Screen number
boolean hasChanged = true;
const int buttonPin = 8; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup()
{
pinMode(buttonPin, INPUT);
}
void loop()
{
if (hasChanged == true) {
switch(WhichSequence) {
case 1:
{
firstSequence();
}
break;
case 2:
{
secondSequence();
}
break;
case 3:
{
thirdSequence();
}
break;
case 4:
{
fourthSequence();
}
break;
case 5:
{
fifthSequence();
}
break;
case 6:
{
sixthSequence();
}
break;
case 0:
{
}
break;
}
}
//-------------------------------
// BEGIN of the switch debouncing code
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
hasChanged = true;
WhichSequence++;
}
} else {
hasChanged = false;
}
}
lastButtonState = reading;
// END of the switch Debouncing code
// --------------------------------------
if (WhichSequence > 6){
WhichSequence = 1;
}
}
void firstSequence()
{lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print(" Sequence 1");
lcd.setCursor(0,1);
lcd.print(" Count to 4");
}
void secondSequence()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("Sequence 2");
lcd.setCursor(0,1);
lcd.print("Count to 5");
}
void thirdSequence()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print(" Sequence 3");
lcd.setCursor(0,1);
lcd.print(" Count to 6");
}
void fourthSequence()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("Sequence 4");
lcd.setCursor(0,1);
lcd.print("Count to 7");
}
void fifthSequence()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print(" Sequence 5");
lcd.setCursor(0,1);
lcd.print(" Count to 8");
}
void sixthSequence()
{
lcd.clear();
lcd.setCursor(0,0); // Column, line
lcd.print("Sequence 6");
lcd.setCursor(0,1);
lcd.print("Count to 9");
}
During the operation, the machine moves to different positions, each position will actuate a n.o. sensor, some positions require another part of the machine to operate, this being carried out by turning on a solenoid valve controlled by the code.
Each sequence is different, for instance void thirdSequence() will be something like;
when machine is at -
position 1, lcd will show Sequence3 (line 1), position 1 (line2).
position 2, lcd will show Sequence3 (line 1), position 2 (line2).
position 3, lcd will show Sequence3 (line 1), position 3 (line2), plus a digital write output 1 (to operate solenoid 1)
position 4, lcd will show Sequence3 (line 1), position 4 (line2), plus a digital write output 2
(to operate solenoid 2)
position 5, lcd will show Sequence3 (line 1), position 5 (line2).
position 6, lcd will show Sequence3 (line 1), position 6 (line2).
Each activation of the sensor moves on the positions starting at 1 up to 6 and loops back to 1 again.
I now need to add further code to implement this operation.
I was thinking of adding switch cases within the void thirdSequence, each case incremented by the sensor,
or a void loop within the void thirdSequence something like;
if(count<6)
count +=1;
else
count=0;
}
lastState = ButtonState;
if (count==0)
{
digital write (whatever);
}
{
else if (count ==1)
{
digitalWrite(whatever);
}
{else if (count ==2)
{
digitalWrite(whatever);
}
// and so on...up to 6
Only thing is not sure if you can do either of these 2 methods. If not has anyone any ideas of how to code the 2nd part of the project.