Ben nu bezig om de code van 2 projecten samen te voegen.
Namelijk :
lcd's : https://wokwi.com/projects/436553938972699649
lcd menu : https://wokwi.com/projects/437823988872739841
Nu vroeg ik me af of dit een goed plan was om de code voor het lcd scherm samen te voegen met de code voor de leds.
Plan :
// function prototypes for button functions
void buttonUp();
void selectChoice();
void buttonDown();
struct Button {
const uint8_t pin;
unsigned long lastDebounceTime;
uint8_t lastButtonState;
uint8_t currDebouncedState;
uint8_t prevDebouncedState;
void (*func)();
};
unsigned long long debounceDelay = 20;
// Create and initialize an array of 3 Button objects
Button buttonList[] = {
{ A3, 0, !ISPRESSED, !ISPRESSED, !ISPRESSED, buttonUp }, // buttomUp
{ A2, 0, !ISPRESSED, !ISPRESSED, !ISPRESSED ,selectChoice}, // select
{ A1, 0, !ISPRESSED, !ISPRESSED, !ISPRESSED, buttonDown }, // buttondown
};
Deze code verplaatsen naar input.cpp
LiquidCrystal LCD(12, 11, 10, 9, 8, 7);
Deze code verplaatsen naar pins.h
void showMenu(int counter) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("--->");
LCD.setCursor(5, 0);
LCD.print(menuItems[counter % NUMELEMENTS(menuItems)]);
LCD.setCursor(5, 1);
LCD.print(menuItems[(counter + 1) % NUMELEMENTS(menuItems)]);
};
Deze code verplaatsen naar output.cpp
void buttonUp()
{
if (choice > 0) {
choice--;
} else {
choice = 2;
}
}
void selectChoice()
{
selectedChoice = choice % NUMELEMENTS(menuItems) ;
Serial.print("U hebt gekozen voor ");
Serial.println(menuItems[selectedChoice]);
}
void buttonDown()
{
++choice;
Serial.print("Waarde counter : ");
Serial.println(choice);
}
void readButton(int index) {
buttonState = digitalRead(buttonList[index].pin);
if (buttonState != buttonList[index].lastButtonState) {
buttonList[index].lastDebounceTime = millis();
}
if ((millis() - buttonList[index].lastDebounceTime) > debounceDelay) {
buttonList[index].currDebouncedState = buttonState;
}
if (buttonState != buttonList[index].lastButtonState) {
buttonList[index].lastButtonState = buttonState;
}
}
Deze verplaatsen naar input.cpp
void loop()
{
for (uint8_t cnt = 0; cnt < NUMELEMENTS(buttonList); cnt++)
{
readButton(cnt);
if (buttonList[cnt].currDebouncedState != buttonList[cnt].prevDebouncedState)
{
buttonList[cnt].prevDebouncedState = buttonList[cnt].currDebouncedState;
// bug fgix
if (buttonList[cnt].currDebouncedState == ISPRESSED)
{
if (buttonList[cnt].func != nullptr) {
buttonList[cnt].func();
showMenu(choice);
}
}
else
{
}
}
}
}
Deze verplaatsen naar input.cpp onder een nog te bedenken naam.
en dan denk ik aanvragen via de .ino file ?
Is dit een goed plan of is er een betere manier ?