Hi,
I have 3 buttons in my array. My goal is when button 1 is pressed i see for example 'button1' in the serial monitor, if button 2 is pressed 'button2' ..... And I want to see this when the button state is PRESSED in the switch function. Only one button is working right now.
I dont know exactly where its going wrong, I hope some u can help me. Thanks for the help!
This is my code:
const int BUTTONLEFT = 2;
const int BUTTONMID = 3;
const int BUTTONRIGHT = 4;
const int BUTTONPINARRAY[] = {BUTTONLEFT, BUTTONMID, BUTTONRIGHT};
int SIZEARRAY = sizeof(BUTTONPINARRAY) / sizeof(BUTTONPINARRAY[0]);
int buttonleft = 0;
int buttonmid = 1;
int buttonright = 2;
unsigned long buttonCurrentMillis, buttonPreviousMillis;
const int RELEASED = 0;
const int PRESSEDONCE = 1;
const int PRESSED = 2;
const int RELEASEDONCE = 3;
int buttonState, buttonEvent;
const int DELAYTIMEBUTTON = 50;
int counter;
void setup() {
buttonSetup();
counter = 0;
Serial.begin(9600);
buttonPreviousMillis = 0;
}
void loop() {
buttonLoop();
if(buttonCurrentMillis > buttonPreviousMillis + DELAYTIMEBUTTON)
{
buttonCurrentMillis = buttonPreviousMillis;
buttonLoop();
}
if (getButtonState() == 1) {
counter++;
Serial.println(counter);
}
}
void buttonSetup() {
for(int button = 0; button < SIZEARRAY; button++)
{
pinMode(BUTTONPINARRAY[button], INPUT);
}
buttonState = RELEASED;
buttonEvent = 0;
}
int getButtonState() {
int returnValue;
if (buttonEvent == 1) {
returnValue = 1;
buttonEvent = 0;
}
else {
returnValue = 0;
}
return returnValue;
}
void buttonLoop() {
if (digitalRead(BUTTONPINARRAY[2,1,0])) { // button released detected
switch (buttonState) {
case PRESSED: { // previous pressed
Serial.print("111111111111111111");
buttonState = RELEASEDONCE; // wait another cycle to be released
break;
}
case RELEASEDONCE: { // previous release pressed
buttonState = RELEASED; // finally released
break;
}
case PRESSEDONCE: { // previous pressed detected
buttonState = RELEASED; // now released, so bounce found
break;
}
default: // buttonState RELEASED stays RELEASED
break;
}
}
else { // button pressed detected
switch (buttonState) {
case RELEASED: { // previous not pressed
buttonState = PRESSEDONCE; // wait another cycle to be pressed
break;
}
case PRESSEDONCE: { // previous not pressed
buttonState = PRESSED; // finally pressed
buttonEvent= 1;
break;
}
case RELEASEDONCE: { // previous release pressed
buttonState = PRESSED; // now pressed, so bounce found
break;
}
default: // buttonState PRESSED stays PRESSED
break;
}
}
}