Debounce (some) buttons
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
#include "Arduino.h"
|
||||
#include "FT18_STW_INIT.h"
|
||||
#include "Bounce2.h"
|
||||
#include "RotaryEncoder.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Bounce2.h>
|
||||
#include <RotaryEncoder.h>
|
||||
|
||||
volatile stw_data_type Stw_data = {0}; //alles mit 0 initialisieren
|
||||
volatile vehicle_data_type Vehicle_data = {0}; //alles mit 0 initialisieren
|
||||
@ -9,7 +10,8 @@ bool enc1PinALast,enc1PinANow,enc2PinALast,enc2PinANow;
|
||||
int led[] = {led1,led2,led3,led4,led5,led6,led7,led8,led9,led10,led11,led12,led13,led14,led15,led16};
|
||||
bool entprell;
|
||||
int buttons[] = {button1,button2,button3,button4,button5,button6,enc1PinS,enc2PinS};
|
||||
Bounce debouncer[8];
|
||||
constexpr size_t N_BUTTONS = sizeof(buttons)/sizeof(buttons[0]);
|
||||
Bounce2::Button debouncer[N_BUTTONS];
|
||||
double val = 0;
|
||||
double val2 = 0;
|
||||
RotaryEncoder encoder(enc1PinA,enc1PinB,1,1,50);
|
||||
@ -23,110 +25,41 @@ void set_pins(){
|
||||
for (int thisLed = 0; thisLed < sizeof(led)/sizeof(int); thisLed++) {
|
||||
pinMode(led[thisLed], OUTPUT);
|
||||
}
|
||||
/*pinMode(button1, INPUT);
|
||||
pinMode(button2, INPUT);
|
||||
pinMode(button3, INPUT);
|
||||
pinMode(button4, INPUT);
|
||||
pinMode(button5, INPUT);
|
||||
pinMode(button6, INPUT);*/
|
||||
pinMode(enc1PinA, INPUT);
|
||||
pinMode(enc1PinB, INPUT);
|
||||
//pinMode(enc1PinS, INPUT);
|
||||
pinMode(enc2PinA, INPUT);
|
||||
pinMode(enc2PinB, INPUT);
|
||||
//pinMode(enc2PinS, INPUT);
|
||||
//Stw_data.i=0;
|
||||
enc1PinALast=LOW;
|
||||
enc1PinANow=LOW;
|
||||
enc2PinALast=LOW;
|
||||
enc2PinANow=LOW;
|
||||
for(int i = 0; i < 8; i++){
|
||||
pinMode(buttons[i], INPUT);
|
||||
debouncer[i].attach(buttons[i]);
|
||||
for(int i = 0; i < N_BUTTONS; i++){
|
||||
debouncer[i].attach(buttons[i], INPUT);
|
||||
debouncer[i].interval(10);
|
||||
}
|
||||
}
|
||||
|
||||
void read_buttons(){
|
||||
/*entprell = digitalRead(button3);
|
||||
delay(10);
|
||||
if(digitalRead(button3)){
|
||||
Stw_data.Stw_auto_shift = entprell;
|
||||
for (int i = 0; i < N_BUTTONS; i++) {
|
||||
debouncer[i].update();
|
||||
}
|
||||
entprell = digitalRead(button2);
|
||||
delay(10);
|
||||
if(digitalRead(button2)){
|
||||
Stw_data.Stw_neutral = entprell;
|
||||
}
|
||||
entprell = digitalRead(button1);
|
||||
delay(10);
|
||||
if(digitalRead(button1)){
|
||||
Stw_data.buttonState1 = entprell;
|
||||
}
|
||||
entprell = digitalRead(button6);
|
||||
delay(10);
|
||||
if(digitalRead(button6)){
|
||||
Stw_data.Stw_shift_up = entprell;
|
||||
}
|
||||
entprell = digitalRead(button5);
|
||||
delay(10);
|
||||
if(digitalRead(button5)){
|
||||
Stw_data.Stw_shift_down = entprell;
|
||||
}
|
||||
entprell = digitalRead(button4);
|
||||
delay(10);
|
||||
if(digitalRead(button4)){
|
||||
Stw_data.buttonState4 = entprell;
|
||||
}
|
||||
entprell = digitalRead(enc1PinS);
|
||||
delay(10);
|
||||
if(digitalRead(enc1PinS)){
|
||||
Stw_data.buttonStateEnc1 = entprell;
|
||||
}
|
||||
entprell = digitalRead(enc2PinS);
|
||||
delay(10);
|
||||
if(digitalRead(enc2PinS)){
|
||||
Stw_data.buttonStateEnc2 = entprell;
|
||||
}*/
|
||||
Stw_data.Stw_auto_shift = digitalRead(button3);
|
||||
Stw_data.Stw_neutral = digitalRead(button2);
|
||||
|
||||
// These are only used to send them out via CAN, so they only need to be
|
||||
// high once.
|
||||
Stw_data.Stw_neutral = debouncer[1].rose();
|
||||
Stw_data.Stw_auto_shift = debouncer[2].rose();
|
||||
Stw_data.Stw_shift_down = debouncer[4].rose();
|
||||
Stw_data.Stw_shift_up = debouncer[5].rose();
|
||||
|
||||
// These are also used for GUI, so if we set them only at rising edge, they
|
||||
// might never be high when checked in the GUI.
|
||||
// TODO: Rewrite so we can use debounced values here as well
|
||||
Stw_data.buttonState1 = digitalRead(button1);
|
||||
Stw_data.Stw_shift_up = digitalRead(button6);
|
||||
Stw_data.Stw_shift_down = digitalRead(button5);
|
||||
Stw_data.buttonState4 = digitalRead(button4);
|
||||
Stw_data.buttonStateEnc1 = digitalRead(enc1PinS);
|
||||
Stw_data.buttonStateEnc2 = digitalRead(enc2PinS);
|
||||
|
||||
/*for(int i = 0;i < 8; i++){
|
||||
debouncer[i].update();
|
||||
}
|
||||
debouncer[2].update();
|
||||
if(debouncer[2].fell()){
|
||||
Stw_data.Stw_auto_shift = HIGH;
|
||||
}
|
||||
if(debouncer[1].fell()){
|
||||
Stw_data.Stw_neutral = digitalRead(button2);
|
||||
}
|
||||
if(debouncer[0].fell()){
|
||||
Stw_data.buttonState1 = digitalRead(button1);
|
||||
}
|
||||
if(debouncer[5].fell()){
|
||||
Stw_data.Stw_shift_up = digitalRead(button6);
|
||||
}
|
||||
if(debouncer[4].fell()){
|
||||
Stw_data.Stw_shift_down = digitalRead(button5);
|
||||
}
|
||||
if(debouncer[3].fell()){
|
||||
Stw_data.buttonState4 = digitalRead(button4);
|
||||
}
|
||||
if(debouncer[6].fell()){
|
||||
Stw_data.buttonStateEnc1 = digitalRead(enc1PinS);
|
||||
}
|
||||
if(debouncer[7].fell()){
|
||||
Stw_data.buttonStateEnc2 = digitalRead(enc2PinS);
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void read_rotary(){
|
||||
int enc = encoder.readEncoder();
|
||||
int enc2 = encoder2.readEncoder();
|
||||
|
||||
Reference in New Issue
Block a user