Reading digital pin output during digital pin button presses(bit of rant )

Not sure how to name problem I have. Straight to the point I am making game with 7 blinking lights, going up and down from left to right to left and back again. Once you push a button and if first light is on, that light stays on, then if you push a button when second light is blinking it stays up, and so on until all 7 lights are lit up. I want to make that if you push a button when light that supposed to go up isnt blinking at that time your previous light goes out.

Problem I am facing is whole process of reading value from pins

if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH &&  gameState)
    {
      if (lightState[getLightState()] == HIGH)
      {
        Serial.println(buttonState);
        buttonPushCounter++;
        Serial.print("positive ");
        //Serial.println(getLightState());
        nrOfLights++;
        positive = true;        
      }
      if (lightState[getLightState()] == LOW &&!positive)
      {
        Serial.println(buttonState);
        buttonPushCounter++;
        Serial.print("negative  ");
        //Serial.println(getLightState());
        if (nrOfLights >= 1)
        {
          nrOfLights--;
        }

      }
    }

no matter what I do both statements trigger even if they both cant trigger, even added extra boolean to prevent second going off if first statement goes off, but it still fires off randomly, not to mention that after few pushes all my methods just dies , probably because of nrOfLights changing when it shouldnt
digital pin values can both be negative and positive as it seems, button value can go off randomly too, arduino doesnt follow any sort of logic pastern either, its just driving me nuts, I spent far too much time combating 2000 issues thrown at me and very little doing actual programming, and when I overcome one thing I get another 5 thrown in my face i cant even fathom.

There must be some trick to have if statements that wont fire at same time to check if pin is high do this, if pin is low do this.

ps ( I am not using delay for lights, only delay I have is small 50 ms delay for button states ,so one press doesnt produce 325235523 presses

How are the switches wired?

Post your code.

const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0;

boolean gameState = false;
// first and last light numbers
int first = 5, last = 12;
int currentLight = 5;
int interval = 1000;
unsigned long previousMove = millis();
boolean forward = true;
int lightState[7];
int lights[7];
unsigned int nrOfLights = 0;
boolean positive = false;

void setup()
{
  // initializing lights
  for (int i = first; i < last; i++)
  {
    lights[i - 5] = i;
    pinMode(lights[i - 5], OUTPUT);

  }
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop()
{

  // getting light state (high/low)
  for (int i = 0; i < 7; i++)
  {
    lightState[i] = digitalRead(lights[i]);

  }
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH &&  gameState)
    {
      if (lightState[getLightState()] == HIGH)
      {
        Serial.println(buttonState);
        buttonPushCounter++;
        Serial.print("positive ");
        //Serial.println(getLightState());
        nrOfLights++;
        positive = true;
        delay(50);
        
      }
      if (lightState[getLightState()] == LOW &&!positive)
      {
        Serial.println(buttonState);
        buttonPushCounter++;
        Serial.print("negative  ");
        //Serial.println(getLightState());
        if (nrOfLights >= 1)
        {
          nrOfLights--;
        }

      }
    }

    if (!gameState && buttonState == HIGH  )
    {
      gameState = true;
    }

    // Delay a little bit to avoid bouncing
    delay(50);
    positive = false;
  }

  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

  if (gameState)
  {
    if (millis() - previousMove >= interval)
    {
      // blinking forwards
      if (forward)
      {
        previousMove = millis(); //Remember when we switched LED
        blinkingLights(currentLight);//Update the light position.
        currentLight++; //increment the Light position.
        if (currentLight == last)
        {
          forward = false;
        }
      }
      // blinking backwards
      if (!forward)
      {
        previousMove = millis(); //Remember when we switched LED
        currentLight--; //increment the Light position.
        blinkingBack(currentLight);//Update the light position.

        if (currentLight == first)
        {
          forward = true;
          //preventing first light stay up twice as long
          currentLight = first + 1;
        }
      }
    }
  }
  lightsOn(getLightState() + 5);

}




void blinkingLights(int lightPos)
{
  for (int i = first; i < last; i++)
  {
    digitalWrite(i, LOW);
  }
  digitalWrite(lightPos , HIGH);
}

void blinkingBack(int lightPos)
{

  for (int i = 11 ; i > first; i--)
  {
    digitalWrite(i, LOW);
  }
  digitalWrite(lightPos , HIGH);

}

// getting state to check
int getLightState()
{
  int current = 0;

  if (nrOfLights == 0)
  {
    return current;
  }
  if (nrOfLights != 0)
  {
    current += nrOfLights;
  }
  return current;
}
// lighting up lights permamently
void lightsOn(int lastLight)
{
  for (int i = 5; i < lastLight; i++)
  {
    digitalWrite(i, HIGH);
  }
  first = lastLight;
}

code is far from pretty tbh
attached photo for wiring, wired following an example

anyone?

Hi, can you post a CAD or pic of a hand drawn circuit of your project, it looks as though the wiring around the switch is a bit confusing.
If you are aiming at high on input when switch is pressed then you should have.
A 10K resistor from gnd to input.
A switch from input to 5V.

I see in the sketch that you turn pull ups on as well.
If you use pull ups then the switch will have to be wired from input to gnd, and pressing the switch will pull input LOW, you don't need the 10K resistor.
Which is it?

Your pic doesn't let us see where the wires go as they go out of shot.
Also it is good to use a standard, black is gnd wires, red is 5v, another colour as the switched or switching wires.
At least use black for gnd circuits only.

Tom...Hope to help..... :slight_smile:

IF you zoom in(click on photo) you can see where each connection goes to, all I really want is to have a button that works, and 2 if statements where one depends if button is pushed and another if it isnt. I realized now that its always either low or hight, but shouldnt button state prevent constant button change?

Another problem I have that game start on its own constantly now, ( at first it wasnt ) when i have statement not to start until button is pushed, another problem I have that arduino reads button as constantly pressed for periods of time, I will plug in arduino and button is constantly pushed, hour latter it goes away, and I am not even sure why, at one point I unplugged everything, just arduino board and ran simple button example test, to blink inbuilt led on the board, and it still was firing like mad, while there was no button, no connection or anything just empty arduino board, does that means its faulty or what can be the cause?
I am studying programming and this is frankly horrible learning experience that seems to be based around IDE thats horrible and electronic hardware thats either faulty or just bad.., at least now I understand why we arent required fully working project and a prototype will suffice...

as I am typing my game played it self, started on its own, pressed on correct lights till all of them lit up ( well it always saw button as pushed, same thing ) in a hour it will be back to normal where button will be working somewhat, wiring is made following lecturers example, might be wrong but there arent any real examples online either

here is how I wire my switches. I usually delete the 10K pull up and enable the internal pullup. The cap I will use for a switch that is counted, othewise if just high or low, I’ll delete it, too. Note LOW is switch pushed.

Hi, sorry, but just a pic does not cut it, you must have a circuit diagram of your project, can you post it,even if it is a hand drawn effort that you have photographed.
In particular how you have wired the switch, as you could have a conflict between pull ups and pull downs.
Can you measure for us with meter.

  1. Voltage between grn and arduino input pin2 with switch not pressed.
  2. Voltage between grn and arduino input pin2 with switch pressed.

Tom… :slight_smile: