Hi
I have created three pages for my glcd and they look alright
but what is the simplest way to step through the with a push of a button
cheers Peter
Hi
I have created three pages for my glcd and they look alright
but what is the simplest way to step through the with a push of a button
cheers Peter
I'd use a digitalRead on the pin connected to the button... that seems pretty easy to me... but maybe I missed the intent of your question...
Ok
how do i do a simple a simple step loop
i mean buton push goto and stay there
next push goto and stay there..
/Peter
Ok got it to work but not as good as I like it to..
here is the code please advise on checking button state without delays
#include <ks0108.h>
#include <Arial14.h>
#include "SystemFont5x7.h"
#include "Wag120x32.h"
const int buttonPin = 2;
int buttonState = 0;
int pag = 0;
void setup(){
pinMode(buttonPin, INPUT);
GLCD.Init(NON_INVERTED);
GLCD.ClearScreen();
GLCD.SelectFont(System5x7);
}
void loop(){
switch (pag) {
case 0:
introScreen();
break;
case 1:
page2();
break;
case 2:
page3();
break;
case 3:
page4();
break;
case 4:
reset();
break;
}
}
void introScreen(){
// GLCD.ClearScreen();
GLCD.DrawBitmap(Wag120x32, 4,1, BLACK);
delay(1000);
GLCD.CursorTo(3,4);
GLCD.Puts("CPS");
// delay(1000);
GLCD.CursorTo(14,7);
GLCD.Puts("FW 1.01");
button();
}
void page2(){
// GLCD.ClearScreen();
GLCD.CursorTo(3,4);
GLCD.Puts("Page 2");
button();
}
void page3(){
// GLCD.ClearScreen();
GLCD.CursorTo(3,4);
GLCD.Puts("Page 3");
button();
}
void page4(){
// GLCD.ClearScreen();
GLCD.CursorTo(3,4);
GLCD.Puts("Page 4");
button();
}
void reset(){
pag = 0;
}
void button(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
pag++;
GLCD.ClearScreen();
delay(1000);
}
}
Your code spends a lot of time spinning around re-drawing the same content. You might want to take a look at the attachInterrupt function and use that to detect a button press, and then display your content once. You could then get rid of the delays and your loop() function would be reduced to very little.
Cheers,
Can someone please post an little example code besides the one i found references
helps me learn quicker..
thanks
Peter
Something along the lines of...
int currentPage = 0;
#define MAX_PAGES 4
void setup()
{
...
attachInterrupt( 0, buttonFunc, HIGH );
...
}
void buttonFunc()
{
currentPage = ( currentPage + 1 ) % MAX_PAGES;
showCurrentPage();
}
void showCurrentPage()
{
switch ( currentPage )
{
}
}
In showCurrentPage you call one of your existing functions, but you modify them to remove the delays and the call to your button() function.
Hope that helps,
I get an error on this line
currentPage = ( currentPage + 1 % ) MAX_PAGES;
LOG
sketch_mar01a.cpp: In function 'void buttonFunc()':
sketch_mar01a:19: error: expected primary-expression before ')' token
sketch_mar01a:19: error: expected `;' before numeric constant
sketch_mar01a.cpp: In function 'void showCurrentPage()':
sketch_mar01a:39: error: 'reset' was not declared in this scope
cheers
Sorry, the mod (%) operator goes outside the brackets. I obviously didn't compile any of that!
hmm is not void loop() needed ?
/Peter
Yes of course! I didn't write the whole thing for you!
Of curse not ![]()
can you explain what hapends at "currentPage = ( currentPage + 1 ) % MAX_PAGES;"
It simply increments currentPage and when it gets to the maximum page index (+1) the mod operator forces it back to zero. The mod operator is similar to how you learnt division at school with "remainders" (well, it's how I learnt it anyway). So mod says to divide currentPages by MAX_PAGES and return the remainder. So once currentPages = MAX_PAGES the result is 0. It sounds more complicated than it is.
Cheers,