Programming Assistance

Why will this not let me take a wire to ground out pin 3 it requires me to turn it off in the serial monitor and back on.

#include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>



#include <Wire.h>

// this constant won't change:

const int  Down_buttonPin = 3;

// Variables will change:

int shotsLeftCheck = 35; // Check How Many Shots Left

int down_buttonState = 0;         // current state of the trigger

int down_lastButtonState = 0;     // previous state of the trigger

int Game_start = 4; // Starts Game

int Game_startState; // Shows Game On or Off

int pincheck[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int prevpincheck[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

bool bPress = false;

String pin_str = "";



void setup()

{

  Serial.begin(9600);
  
  pinMode( Down_buttonPin , INPUT_PULLUP);
  digitalWrite(Game_start, HIGH);
  pinMode(Game_start, OUTPUT);
  for (int ii = 2;ii<=sizeof(pincheck)/sizeof(pincheck[0]);ii++){
    pinMode(ii,OUTPUT);
  }
}

void loop()

{
    
    while (Serial.available()){
    char in_char = Serial.read();
    if (int(in_char)!=-1){
      pin_str+=in_char;
    }
    if (in_char=='\n'){
      int pin_num = pin_str.toInt();
      pincheck[pin_num] = !pincheck[pin_num];
      digitalWrite(pin_num, pincheck[pin_num]);
      Serial.print("Pin # ");
      Serial.print(pin_num);
      if (pincheck[pin_num]==0){
        Serial.println(" OFF");
      } else {
        Serial.println(" ON");
      }
      pin_str = "";
  
   pincheck[Game_start] = digitalRead(Game_start);
   
   if (pincheck[Game_start] == HIGH){
        delay(5);
        Serial.println("Press Play");
      } else {
        Serial.println("Begin");
        checkDown();       
        delay(5);
      }

      }
     }
    }

void checkDown()

{ 
  down_buttonState = digitalRead(Down_buttonPin);
    Serial.print("Down Button State ");
    Serial.println(down_buttonState);
    if(shotsLeftCheck <= 0 || shotsLeftCheck >3) {
      digitalWrite(Game_start , HIGH);
      shotsLeftCheck = 3;
    Serial.println("Shoft Left Check Set to 3");
    } else {

 Serial.println("Bang Check");
  
  // compare the buttonState to its previous state

  if (down_buttonState != down_lastButtonState) {

    // if the state has changed, increment the counter

    if (down_buttonState == LOW) {
      
      // if the current state is HIGH then the button went from off to on:
      
      Serial.println("Bang");

      Serial.print("Number Of Shots Remaining: ");

      Serial.println(shotsLeftCheck);

       shotsLeftCheck--;
    }

    // Delay a little bit to avoid bouncing

    delay(50);

  }

  // save the current state as the last state, for next time through the loop

  down_lastButtonState = down_buttonState;

}

}

Why do you only check the pin when there is serial data to read?

The main goal was to have this work for a digital shooting gallery. So pin 4 gets data from unity to start the game. then in the gun there will be a switch that when you pull the trigger you bring pin 3 to low when it goes low unity takes the serial response and displays that on the new UI for Number of shots left. Now the targets is each going to have an arduino since they will be placed far apart and they will when they are shot bring a pin back on the main controller to low to and add up the score. But I'm still working on that last part. Mainly I just need to see why pin 3 will not let me ground it, it seems it is being set to an output pin even though it is assigned INPUT_PULLUP.

jonathanrhcooper: Mainly I just need to see why pin 3 will not let me ground it, it seems it is being set to an output pin even though it is assigned INPUT_PULLUP.

What do you mean by this? How do you know?

You are 100% corrcect I went back and read your first statement and I found the issue thanks to you!!! The code was only being checked when Serial was being read. I took it out from there and resolve the issue! Now any ideas on how to just get the the serial print outs to only print when status of the Pin is changed?

if (pincheck[Game_start] == HIGH){
        delay(5);
        Serial.println("Press Play");
      } else {
        Serial.println("Begin");
        checkDown();       
        delay(5);
      }

The corrected code from first issue!!

 void loop()

{
     pincheck[Game_start] = digitalRead(Game_start);
   
   if (pincheck[Game_start] == HIGH){
        delay(5);
        Serial.println("Press Play");
      } else {
        Serial.println("Begin");
        checkDown();       
        delay(5);
      }
    while (Serial.available()){
    char in_char = Serial.read();
    if (int(in_char)!=-1){
      pin_str+=in_char;
    }
    if (in_char=='\n'){
      int pin_num = pin_str.toInt();
      pincheck[pin_num] = !pincheck[pin_num];
      digitalWrite(pin_num, pincheck[pin_num]);
      Serial.print("Pin # ");
      Serial.print(pin_num);
      if (pincheck[pin_num]==0){
        Serial.println(" OFF");
      } else {
        Serial.println(" ON");
      }
      pin_str = "";
      }
     }
    }

Just make sure any printing is controlled by this if:

   if (down_buttonState != down_lastButtonState)

Thanks you are a lifesaver!!!!