I've made a board with UNO that is basically 3 buttons and 4 leds, as 3 of them activated by pressing the corresponding button. The fourth led is activated by pressing twice any button (counter >> 1).
I don't understand why but pressing three or four times (alternatively), one button after the other it activates the fourth led . I guess it should not doing that...
You can see the schem and the code below. If anybody guessing what's happened...
const int push_A = 7;
const int push_B = 4;
const int push_C = 2;
const int pushButtons[] = {push_A,push_B,push_C};
const int led_1 = 14;
const int led_2 = 15;
const int led_3 = 16;
int leds[] = {led_1,led_2,led_3};
int buttonPushCounter[] = {0,0,0}; // counter for the number of button presses
int buttonState[3]; // current state of the button
/**********************************************************************/
void setup()
{
pinMode(push_A, INPUT_PULLUP);
pinMode(push_B, INPUT_PULLUP);
pinMode(push_C, INPUT_PULLUP);
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(led_3, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
/**********************************************************************/
void resetAllLeds()
{
for(int i=0; i<3; i++)
{
digitalWrite(leds[i], LOW);
}
digitalWrite(13, LOW);
}
/**********************************************************************/
void resetCounter()
{
for(int j=0; j<3; j++)
{
buttonPushCounter[j]= 0;
}
}
/**********************************************************************/
void loop() //ok
{
for(int i=0; i<3; i++)
{
buttonState[i] = digitalRead(pushButtons[i]);
switch (buttonState[i])
{
case 1:
NULL;
break;
case 0:
buttonPushCounter[i]++;
resetAllLeds();
digitalWrite(leds[i], HIGH);
delay(210);
if(buttonPushCounter[i] > 1)
{
resetCounter();
resetAllLeds();
digitalWrite(13, HIGH);
}
break;
}
}
}
I think the problem you describe may be because your code does not detect when a button becomes pressed, only when it is pressed. In other words it does not detect the button changing state. There is an example sketch in the IDE menu which demonstrates this principal, called "state change" or similar.
const int push_A = 7;
const int push_B = 4;
const int push_C = 2;
const int pushButtons[] = {push_A,push_B,push_C};
const int led_1 = 14;
const int led_2 = 15;
const int led_3 = 16;
int leds[] = {led_1,led_2,led_3};
In this case it is passably forgivable as it is reasonably unlikely that the same people will or indeed can view both threads!
The ire against double posting is that those answering on one thread are not privy to what has been suggested on the other and may be effectively wasting their time duplicating thought and advice. It is not quite so obvious here.
cross post violates the forum etiquette.
as described in the how-to-post topic: Don't cross-post!
Even if one would only read one threat - answering questions which are already answered in another threat is a waste of member time and waste of technical resources.
Paul__B:
In this case it is passably forgivable as it is reasonably unlikely that the same people will or indeed can view both threads!
The ire against double posting is that those answering on one thread are not privy to what has been suggested on the other and may be effectively wasting their time duplicating thought and advice. It is not quite so obvious here.
Exactly this is wasting time for helpers... not very ethical. And he did get similar feedback on both sides (and many French members read and post on both sides)
But bottom line, it’s against forum’s etiquette... so should not be done. May be wait a week and if no answer go post elsewhere and mention an link to the other thread / cross reference so that everyone is aware.