Hit Counter - Problem with "HIGH" command/code

Hey guys,

I’ve been working on making a hit counter for the game “Operation” and have run into a problem. Ideally, the counter (which uses a SainSmart LCD Shield) will increase by 1 when a circuit changes from open to closed. However, there seems to be enough voltage in the system when the circuit is open to activate the counter. It even counts when I touch the correct pin with my finger.
I have added a reset button to the LCD shield that resets hits to zero when pressed. Here is the code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

int hits = 0;

int switchState = 0;

int switchState1 = 0;

int prevSwitchState = 0;

int prevSwitchState1 = 0;

const int switchPin = A1;

void setup()
{
  lcd.begin(16,2);
  pinMode(switchPin, INPUT);
  lcd.print("  Open Surgery  ");
  lcd.setCursor(0,1);
  lcd.print("    Simulator   ");
  delay(4000);
  lcd.clear();
}

void loop()
{
  switchState = analogRead (0);
  switchState1 = digitalRead(switchPin);
  
  if (switchState != prevSwitchState){
    if (switchState < 800 && switchState > 600)
      hits = 0;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hits:");
      lcd.setCursor(6,0);
      lcd.print(hits);
      delay(50);
  }


  
  if (switchState1 != prevSwitchState1)
   {
     if (switchState1 == HIGH)
        hits = hits + 1;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Hits:");
        lcd.setCursor(6,0);
        lcd.print(hits);
        delay(10);}
   prevSwitchState1 = switchState1;
 }

My question is, is there a way to make the “HIGH” command only true when the input into the pin reaches a certain voltage such as 9V? Or in other words can you set a certain threshold for the “HIGH” command? I believe that if I could do this then small voltages that are created by a finger or by the open circuit would not activate the counter.

Thanks

Have you got those pins wired with pullup/pulldown resistors? If neither, then when the switch is open, its state is not guaranteed, exactly as you are finding.

Have a look here. Built-in pullups are easiest to implement: code only, no extra hardware.

I gave the Pull-Up resistor a try but when I did that the hit counter no longer counted. The number of hits started at 1 and would not count even when a 9v battery was attached.

My question is, is there a way to make the “HIGH” command only true when the input into the pin reaches a certain voltage such as 9V?

Please don’t put anything more then 5 volts max. to your Arduino or you will burn it!

ich002:
I gave the Pull-Up resistor a try but when I did that the hit counter no longer counted. The number of hits started at 1 and would not count even when a 9v battery was attached.

Best show the circuit for that, and as steinie44 points out, don’t be putting 9V in an Arduino other than as power, def not in an i/o pin.

(Pullups invert your logic btw: the pin is high normally, and goes to ground (low) when the switch is closed)