#include <LiquidCrystal.h>
LiquidCrystal lcd (12,11,5,4,3,2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup(){
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
lcd.print(" Ask the");
lcd.setCursor(0,1);
lcd.print("Crystal ball");
}
void loop(){
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState ){
if (switchState == LOW){
reply = random(8);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("The ball says :");
lcd.setCursor(0,1);
switch(reply){
case 0:
lcd.print("Yes");
break;
case 1:
lcd.print("Most likely");
break;
case 2:
lcd.print("Certainly");
break;
case 3:
lcd.print("Outlook good");
break;
case 4:
lcd.print("Unsure");
break;
case 5:
lcd.print("Negative");
break;
case 6:
lcd.print("Doubtful");
break;
case 7:
lcd.print("No");
break;
}
}
}
prevSwitchState = switchState;
}
What I am confused about is the last line
prevSwitchState = switchState
I initially thought that this would set the value of PSS (prevSwitchState) to low so that when it loops the if statements requirements are filled, so I set PSS = 0; but that didn't work either. I've researched it and it says enables me to track changes in the switch next time the loop runs.
This still doesn't make sense, I try to visualise it but I cannot. If you could go through what's actually happening that would be very helpful. Thanks in advance
Just to show how I understand the code,
Firstly the initial state of the button is low, when pressed it becomes high so the first requirement is filled, after letting go it becomes low and prints the random text.
I don't understand the significance of that last line, surely it loops back to the start of the function and does it again?
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState )
{
if (switchState == LOW)
{
This code checks whether the switch state has changed and, if it has, whether it is now LOW. In order to work the previous state of the switch pin needs to be known, hence
prevSwitchState = switchState;
at the end of loop(), ie before the current state is read at the start of loop()
The principle is known as State Change Detection and it is used mainly when you want to know that a switch has become pressed rather than when it is pressed. There is a simple example in the IDE.
Thank you, but for me, I cannot understand it until I can visualise it.
I understand that it is the switch becoming pressed rather than when it is pressed. But what I don't understand is whilst going through the code, the last state of the button is low and we have already defined the previous state to be low. I can see that the previous state of the button is needed however if the state is always low after the button is pressed surely it is not needed.
Like I said before, I would appreciate it if you could talk me through whats actually happening.
Thanks
One more thing, I changed the the prevSwitchState at the beginning to 1, this somehow made it skip the setup and go straight to void loop by printing different answers onto the screen. Why?
I can see that the previous state of the button is needed however if the state is always low after the button is pressed surely it is not needed.
How would the program know that the state had changed unless it knows the previous state ?
If the previous state and current state are the same then the state has not changed. If they are different then the state has changed and the program then determines whether the current state needs to be acted upon.
How I try to grasp whats going on in the sketch is to get some scratch paper. Go through each line, 1 by 1, imagining that you are going to hit the button. write down the numbers that get stored in the variables. When you get to the end (remember it is a loop) start back at the beginning of your loop() and fill in the values you wrote down on paper.
Try that imagining hitting the button, and then not hitting the button.hitting
Oh, sorry. I understand now. I kept on thinking that wherever in the code we will always know the state of the button. However, we only call it once at the start of void loop.
Thank you, turns out it was much simpler than I made it out to be