I have 4 buttons (pull-up), I want each button to transmit one sequence of 4 bits.
(ie. button1=0001, button2=0010)
I made something that seems to work, but it's not as responsive as I would like. Does anyone have any suggestions?
int button_1 = 5;
int button_2 = 6;
int button_3 = 7;
int button_4 = 8;
int bit_1;
int bit_2;
int bit_3;
int bit_4;
int debounceTime=20;
int lastButtonState = 1;
boolean function_executed = false;
long unsigned int lastPress;
//my four digit print function
//button variable selects which button to listen to
//b1,b2,b3,b4 are the four bits of data
void print_Binary(int button, int b1, int b2, int b3, int b4){
int buttonState = digitalRead(button);
if((millis() - lastPress) > debounceTime){
lastPress = millis();
if(buttonState == 0 && lastButtonState == 1 && function_executed == false){
bit_1 = b1;
bit_2 = b2;
bit_3 = b3;
bit_4 = b4;
Serial.print(bit_1);Serial.print(bit_2);Serial.print(bit_3);Serial.println(bit_4);
lastButtonState = 0;
function_executed = true;
}
//function won't loop until all buttons are released
if(digitalRead(button_1) == 1 && digitalRead(button_2) == 1 && digitalRead(button_3) == 1 && digitalRead(button_4) == 1){
function_executed = false;
lastButtonState = 1;
}
}
}
void setup() {
Serial.begin(9600);
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);
pinMode(button_3, INPUT);
pinMode(button_4, INPUT);
}
void loop() {
print_Binary(button_1, 0, 0, 0, 1);
print_Binary(button_2, 0, 0, 1, 0);
print_Binary(button_3, 0, 0, 1, 1);
print_Binary(button_4, 0, 1, 0, 0);
}
Eventually I want this sequence to be written to the EEPROM and stored as a passcode.
Thank you very much!