Hello,
Firstly i am a newbie to Arduino code, i tried figuring this out myself, a bit of googling, and a forum post, turned out i just stumped myself.
OK, firstly i am using a Uno, ( i have it connected to a 16,2 LCD screen) and i want it to do the Following.
Switch on/Reset
Display on the LCD "Message01"
wait
if it detects a 5v signal on pin A0, then set digital pin 2 to HIGH, and clear the LCD, then display "Message02" on the LCD in place of "Message01. (Signal on A0 will not be continuous, only a momentary signal)
Wait in this state until it detects another momentary 5v signal, this time on pin A5, and then restart the process. (if it receives this signal on A5)
Please could someone help me with the code for this, i have tried to write my own code from scratch, then get help from this great forum making it work, but i ended up getting myself, and everyone on the forum confused, so i thought i would start again, so here we are.
post the code that you tried that did not work and you should already know that when posting code to use code tags. Post images of your project showing the wiring, and will this post be moved because it's going to be marked as being a duplicate post?
On an Uno I'd not use A5 or A4 until I am absolutely sure the project won't use I2C and why use an analog pin to just detect 5V?
It's not 100% clear to me what that means. I'm guessing, but you need to clarify, that means make digital pin 2 low, change the lcd back to "Message01" and wait for A0 to go high momentarily again?
I didn't hook up an LCD, so the messages are to the serial monitor. I used pin 13 not 2 for the high/low to see the builtin led change.
The pins with the buttons need to be wired to ground through the buttons- I used input pullup for those.
It's a state machine (see variable "systemState"), so it ignores re-presses of the button that took you to either state: in either state it only monitors the "other" button.
// https://forum.arduino.cc/t/help-noob-with-code-pls/937420
// uses "state change detection" from here https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
// (but changed for input pullup)
// outputs to serial no an lcd for testing
// and to internal led not pin 2
// input pins are wired to ground through their buttons
byte startPin = A5;
byte resetPin = A0;
byte runningPin = 13; //the internal led
bool startPinState;
bool lastStartPinState;
bool resetPinState;
bool lastResetPinState;
bool systemState = 0;
void setup()
{
Serial.begin(9600);
Serial.print("setup ...");
pinMode (startPin, INPUT_PULLUP); // wire pin to ground through button
pinMode (resetPin, INPUT_PULLUP); // wire pin to ground through button
pinMode (runningPin, OUTPUT);
Serial.println(" done");
Serial.println("Message01");
digitalWrite(runningPin, LOW);
}//setup
void loop()
{
if (systemState == 0) readStartButton();
if (systemState == 1) readResetButton();
}//loop
void readStartButton()
{
// read the pushbutton input pin:
startPinState = digitalRead(startPin);
// compare the buttonState to its previous state
if (startPinState != lastStartPinState)
{
if (startPinState == LOW) // if the current state is LOW, button was pressed
{
Serial.println("Message02");
digitalWrite(runningPin, HIGH);
systemState = 1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastStartPinState = startPinState;
} //readStartButton
void readResetButton()
{
// read the pushbutton input pin:
resetPinState = digitalRead(resetPin);
// compare the buttonState to its previous state
if (resetPinState != lastResetPinState)
{
if (resetPinState == LOW) // if the current state is LOW, button was pressed
{
Serial.println("Message01");
digitalWrite(runningPin, LOW);
systemState = 0;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastResetPinState = resetPinState;
} //readResetButton
To help complete the thread, here is the final code, edited to work with an LCD etc...
Quick note, the phrase "Message" in the code just refers to text the Arduino prints to the LCD and the serial monitor, i replaced the actual text with "Message" to make the code easier to read, all the blab i had in there for my project would have made it terribly confusing...
// input pins are wired to ground through their buttons
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
byte startPin = A5;
byte resetPin = A0;
byte redledPin = 2; //the alarm led
byte buzzPin = 4; //the alarm led
bool startPinState;
bool lastStartPinState;
bool resetPinState;
bool lastResetPinState;
bool systemState = 0;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Message");
lcd.setCursor(0, 1);
lcd.print("Message");
Serial.print("Message");
pinMode (startPin, INPUT_PULLUP); // wire pin to ground through button
pinMode (resetPin, INPUT_PULLUP); // wire pin to ground through button
pinMode (redledPin, OUTPUT);
pinMode (buzzPin, OUTPUT);
delay(2000);
lcd.clear();
lcd.print("Message");
lcd.setCursor(0, 1);
lcd.print("Message");
Serial.println("Message");
delay(2000);
lcd.clear();
lcd.print("Message");
Serial.println("Message01");
digitalWrite(redledPin, LOW);
}//setup
void loop()
{
if (systemState == 0) readStartButton();
if (systemState == 1) readResetButton();
}//loop
void readStartButton()
{
// read the pushbutton input pin:
startPinState = digitalRead(startPin);
// compare the buttonState to its previous state
if (startPinState != lastStartPinState)
{
if (startPinState == LOW) // if the current state is LOW, button was pressed
{
Serial.println("Message02");
lcd.clear();
lcd.print("Message02");
lcd.setCursor(0, 1);
lcd.print("Message");
digitalWrite(redledPin, HIGH);
digitalWrite(buzzPin, HIGH);
systemState = 1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastStartPinState = startPinState;
} //readStartButton
void readResetButton()
{
// read the pushbutton input pin:
resetPinState = digitalRead(resetPin);
// compare the buttonState to its previous state
if (resetPinState != lastResetPinState)
{
if (resetPinState == LOW) // if the current state is LOW, button was pressed
{
Serial.println("Message01");
lcd.clear();
lcd.print("Message01");
digitalWrite(redledPin, LOW);
digitalWrite(buzzPin, LOW);
systemState = 0;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastResetPinState = resetPinState;
} //readResetButton