Я сделал контроллер передачи и код работает нормально, как есть. Но я хотел бы добавить в код функцию debounce, так как некоторые изменения не были бы распознаны при нажатии кнопок. Я далек от хорошего кодера, поэтому этот код очень прост, и я в нем все понимаю. Но я не могу понять, как добавить debounce к кнопкам, не слишком продвигая код, и я тоже не понимаю его.
Может ли кто-нибудь прийти с какой-либо помощью или советами, пожалуйста? Смотрите прикрепленный код.
int gear = 1;
// output pins for transistor controlled solenoids
int solA = 4;
int solB = 5;
int solC = 6;
int solD = 7;
int solE = 8;
int solTCC = 9;
int solLinePress = 10;
// input pins for up down / with pullup resistors
int gearup = 2;
int geardown = 3;
// Pin out for single 7 digit display
int a=22;
int b=23;
int c=24;
int d=25;
int e=26;
int f=27;
int g=28;
void setup() {
pinMode(solA, OUTPUT);
pinMode(solB, OUTPUT);
pinMode(solC, OUTPUT);
pinMode(solD, OUTPUT);
pinMode(solE, OUTPUT);
pinMode(solTCC, OUTPUT);
pinMode(solLinePress, OUTPUT);
pinMode(gearup, INPUT);
pinMode(geardown, INPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}
void loop() {
if(gear == -1){ // Reverse
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 0);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 0){ // Neutral
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 1);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if(gear == 1){ // 1th gear
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 0);
digitalWrite(solD, 1);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
if(gear == 2){ // 2nd Gear
digitalWrite(solA, 1);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 1);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if(gear == 3){ // 3rd Gear
digitalWrite(solA, 0);
digitalWrite(solB, 1);
digitalWrite(solC, 0);
digitalWrite(solD, 1);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
if(gear == 4){ // 4th Gear
digitalWrite(solA, 0);
digitalWrite(solB, 1);
digitalWrite(solC, 1);
digitalWrite(solD, 0);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 5){ // 5th Gear
digitalWrite(solA, 0);
digitalWrite(solB, 1);
digitalWrite(solC, 0);
digitalWrite(solD, 0);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 6){ / // 6th Gear
digitalWrite(solA, 0);
digitalWrite(solB, 0);
digitalWrite(solC, 0);
digitalWrite(solD, 0);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
if(gear == 7){ // 7th Gear
digitalWrite(solA, 1);
digitalWrite(solB, 0);
digitalWrite(solC, 0);
digitalWrite(solD, 0);
digitalWrite(solE, 1);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
if(gear == 8){ // 8th Gear
digitalWrite(solA, 1);
digitalWrite(solB, 0);
digitalWrite(solC, 1);
digitalWrite(solD, 0);
digitalWrite(solE, 0);
digitalWrite(solTCC, 0);
digitalWrite(solLinePress, 0);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
//takes care of gear switching
delay(150); //delay to prevent going through gears too quick from holding the button or pressing too long
gear += digitalRead(geardown) - digitalRead(gearup); // non debounced! But may not be a problem because of the delay by gear change
if(gear < -1) gear = -1;
if(gear > 8) gear = 8;
//limits to actual gearset
}