I tried to realize a solution with interrupt and single click.
But I can not get it to work.
My function works every second single click or every double click, but not on single clicks.
I want to realize a state machine that switches to the next state with every single click.
TasterTest3.txt
// Taster
#include "OneButton.h"
// The actions I ca do...
typedef enum {
ACTION_NONE, // Wertauswahl reset
ACTION_VORGABE, // Wertauswahl
ACTION_WAHL, // Wertübernahme
ACTION_A, // test
ACTION_B, // test
ACTION_C // test
}
MyActions;
// Setup a new OneButton on pin A1.
OneButton button(A1, true);
MyActions nextAction = ACTION_NONE; // no action when starting
//Setup-----------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// link the myClickFunction function to be called on a click event.
button.attachClick(myClickFunction);
// link the doubleclick function to be called on a doubleclick event.
//button.attachDoubleClick(myDoubleClickFunction);
// You may have to modify the next 2 lines if using another pin than A1
PCICR |= (1 << PCIE1); // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
PCMSK1 |= (1 << PCINT9); // This enables the interrupt for pin 1 of Port C: This is A1.
// set # millisec after safe click is assumed.
button.setDebounceTicks(50);
// set # millisec after single click is assumed.
button.setClickTicks(500);
// set # millisec after press is assumed.
//button.setPressTicks(1000); //z.Z. nicht genutzt
}
// Main------------------------------------------------------------------------------------------
void loop() {
Serial.print("Nextaction: ");
Serial.println(nextAction);
delay(900);
} // Ende Loop
// Taster
// this function will be called when the button was pressed 1 time and them some time has passed.
void myClickFunction() {
if (nextAction == ACTION_NONE) {
nextAction = ACTION_VORGABE;
Serial.println("VORGABE");
} else if (nextAction == ACTION_VORGABE) {
nextAction = ACTION_WAHL;
Serial.println("WAHL");
} else if (nextAction == ACTION_WAHL) {
nextAction = ACTION_A;
Serial.println("A");
} else if (nextAction == ACTION_A) {
nextAction = ACTION_B;
Serial.println("B");
} else if (nextAction == ACTION_B) {
nextAction = ACTION_C;
Serial.println("C");
} else if (nextAction == ACTION_C) {
nextAction = ACTION_NONE;
}
} // myClickFunction
/*
// this function will be called when the button was pressed 2 times in a short timeframe.
void myDoubleClickFunction() {
if (nextAction == ACTION_VORGABE) {
//l = zykvar[k-1];
nextAction = ACTION_WAHL;
} else if (nextAction == ACTION_A) {
//nextAction = ACTION_NONE;
//Do nothing
}
} // myDoubleClickFunction
*/
// The Interrupt Service Routine for Pin Change Interrupt 1
// This routine will only be called on any signal change on A1: exactly where we need to check.
ISR(PCINT1_vect) {
// keep watching the push button:
button.tick(); // just call tick() to check the state.
}
Could anyone help?
Regards
Volker