Hi all,
I'm having a bit of trouble getting my display to hold a message once I have pressed the push button. How would I be able to make a push button react as an on off switch? Basically I want one button to control a scroll function so it will go through the loop displaying the next message until it starts over and over again.
Please READ THIS POSTto help you get the best out of the forum. SIMPLE TROUBLESHOOTING. These steps may also save you some unnecessary time and effort in the forum.
#include <Wire.h>
#define LCD05 0x63 // LCD05 address
char stringBuf[20];
byte buffer[4];
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 11; // the number of the pushbutton pin
const int buttonPin2 = 10;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
delay(100); // Delay to wait for everything to power up
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
Wire.begin();
buffer[0] = 0; // Clear the screen
buffer[1] = 12;
buffer[2] = 19;
buffer[3] = 31;
buffer[4] = 10;
Wire.beginTransmission(LCD05);
Wire.write(buffer,5);
Wire.endTransmission();
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (buttonState == LOW) {
Wire.begin();
buffer[1] = 19;
buffer[2] = 3;
buffer[3] = 2;
buffer[4] = 3;
Wire.beginTransmission(LCD05);
Wire.write(buffer,5);
Wire.endTransmission();
delay(50);
String message1 = " {SELECT FORCE}"; // The message to be put on the screen
int len1 = message1.length() + 1; // Length of the message
message1.toCharArray(stringBuf, len1); // Convert the message to a car array
stringBuf[0] = 0; // First byte of message to 0 (LCD05 command register)
Wire.beginTransmission(LCD05);
Wire.write(stringBuf, len1);
Wire.endTransmission();
}
else{
Wire.begin();
buffer[1] = 12;
Wire.beginTransmission(LCD05);
Wire.write(buffer,2);
Wire.endTransmission();
Wire.begin();
buffer[1] = 19;
buffer[2] = 3;
buffer[3] = 4;
buffer[4] = 3;
Wire.beginTransmission(LCD05);
Wire.write(buffer,5);
Wire.endTransmission();
String message2 = " {IN PROGRESS}"; // The message to be put on the screen
int len2 = message2.length() + 1; // Length of the message
message2.toCharArray(stringBuf, len2); // Convert the message to a car array
stringBuf[0] = 0; // First byte of message to 0 (LCD05 command register)
Wire.beginTransmission(LCD05);
Wire.write(stringBuf, len2);
Wire.endTransmission();
delay(50);
delay(2000);
buffer[1] = 12;
Wire.beginTransmission(LCD05);
Wire.write(buffer,2);
Wire.endTransmission();
}
// =============================
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (buttonState2 == HIGH) {
buffer[1] = 12;
Wire.beginTransmission(LCD05);
Wire.write(buffer,2);
Wire.endTransmission();
Wire.begin();
buffer[1] = 19;
buffer[2] = 3;
buffer[3] = 1;
buffer[4] = 3;
Wire.beginTransmission(LCD05);
Wire.write(buffer,5);
Wire.endTransmission();
String message4 = " {12345678910}"; // The message to be put on the screen
int len4 = message4.length() + 1; // Length of the message
message4.toCharArray(stringBuf, len4); // Convert the message to a car array
stringBuf[0] = 0; // First byte of message to 0 (LCD05 command register)
Wire.beginTransmission(LCD05);
Wire.write(stringBuf, len4);
Wire.endTransmission();
delay(2000);
buffer[1] = 12;
Wire.beginTransmission(LCD05);
Wire.write(buffer,2);
Wire.endTransmission();
}
}
You have the push buttons wired to connect to Vcc when pushed.
But when the button is not pushed, the pin will be connected to nothing, and hence is "floating" - there's nothing to put it into a defined state, so it will pick up ambient electrical noise and transition randomly (if the shape of the wire was carefully designed, and the pin it's connected to can handle interpreting RF signals, we would call that pin an "antenna")
There are two solutions - either
Add a 10k resistor between the button pins and ground - this will hold it LOW when the button isn't pressed
-or-
Change up the wiring so pressing the button connects the pin to Gnd instead of Vcc, change the pinMode() functions so the pin is set INPUT_PULLUP, and change the code so it considers a button that reads LOW as pressed instead of HIGH.
The second option is preferable for two reasons - you don't need the external resistor, and you don't need to run a Vcc line to each button (running ground everywhere is preferable, since ground is going to be routed all over anyway, and because accidentally shorting something to ground is less likely to be catastrophic than accidentally shorting something to Vcc)
OK, I've used 4x 10k resistors where the yellow is (attachment) but the buttons are only coming on when i hold the button like it should work. i need to find a way so that when I push the button it changes to the next page until it restarts the loop again. so i need something like saying: when push button 1 is pressed then skip to (location) or whatever for example. I'm sure theirs an easy way i just can't find it.
that's not so easy to read as there's a lot going on in the void loop.
In my void loop I'd have only the two button tests and if either was pressed than I'd call one of two functions for button or button2.
I'd temporarily cut the code down to bare minimum to get the first button working and then add the extra code such as the 'elses' in small sections, testing each section as I added it, to get the final code.