Check button while printing text
Mob-Barley opened this issue · 1 comments
Dear bremme,
I want to thank you for this great library.
For my project, I want to add a button to have a small menu.
Now there is the problem, that the arduino is busy while printing the text and can't read the button pin.
I know, that multitasking isn't the strength of arduinos and this maybe gets to complex now.
Until now I tried to add a button check into the print funktion but always failed with the extern variables (I'm really not good at this).
Now before giving up, I want to ask you, if you have an idea how to realize this.
Best regards,
Mob
After sleeping one night about it, I found the solution. It was easier than I tought.
I replaced the scoll fuction with millis, now the button can be checked while the text is shown on the display and it can be interrupted.
Here is the sketch if someone is interested.
#include "SevenSegmentTM1637.h"
#include "OneButton.h"
//VAR
int str_lenght;
String print_str;
String str;
int i = 0;
OneButton button(12, true);
const byte PIN_CLK = 5;
const byte PIN_DIO = 4;
SevenSegmentTM1637 display(PIN_CLK, PIN_DIO);
//MILLIS
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 500;
//SETUP
void setup() {
display.begin();
display.setBacklight(50);
str = "----TEXT 111----TEXT 222----TEXT 333----";
str_lenght = str.length()-4;
startMillis = millis();
button.attachDoubleClick(doubleclick);
button.attachClick(singleclick);
button.attachLongPressStop(longclick);
}
//LOOP
void loop() {
button.tick();
currentMillis = millis();
if (currentMillis - startMillis >= period)
{
print_str = str.substring(i, i+4);
display.print(print_str);
i++;
if (i == str_lenght)
{
i = 0;
}
startMillis = currentMillis;
}
delay(10);
}
//BUTTON ACTIONS
void doubleclick()
{
display.print("DOUBLECLICK");
}
void singleclick()
{
display.print("SINGLECLICK");
}
void longclick()
{
display.print("LONGCLICK");
}