software restart not working from momentary switch

this is a small perimeter sensor type project that counts on bar graph how many times the beam has been broken..

when laser is not recognized, int graphLed increases by 1 and lights the next LED in the bar graph.

question is about software reset. I want to push switch and reset counter. I use pin 12 that I jump to reset pin on the board. If I declare pin type before setting it high or low, it will go low FIRST and get forever loop.

I read somewhere that you have to set PIN to HIGH prior to declaring type. I make pin HIGH prior to declaring a type which seems to work.

so when momentary switch (switchpin) is pressed, it sets switchpin 13 = 1 which sets pin 12 to LOW and then system restarts.

problem is when button is pressed it appears to go into a forever loop.

I have to unplug and plug back in to get to work again.

why would it work fine when first plugged in, but not work after a restart?

any, simple way around this?

can I wire the momentary switch (3 conductors, vcc, gnd, signal) somehow directly to the reset switch and not have a pin set another pin?

I just dont know what to do next to fix this.

I really want to use this particular switch.. they are cheap and work well.

thanks in advance!

Pete

int laserPin = 14;
int switchPin = 13;
int resetPin = 12;
int graphLed = 2;
void setup() {
  pinMode(2,OUTPUT); //led 1
  pinMode(3,OUTPUT); //led 2
  pinMode(4, OUTPUT); //led 3
  pinMode(5, OUTPUT); //led 4
  pinMode(6, OUTPUT); //led 5
  pinMode(7, OUTPUT); //led 6
  pinMode(8, OUTPUT); //led 7
  pinMode(9, OUTPUT); //led 8
  pinMode(10, OUTPUT); //led9
  pinMode(11, OUTPUT); //led 10
  pinMode(13, INPUT); //switchpin
  pinMode(14, INPUT);//laserpin
  pinMode(15, OUTPUT);//red activity LED
    digitalWrite(2,LOW); //led 1
    digitalWrite(3,LOW);//led 2
    digitalWrite(4,LOW);//led 3
    digitalWrite(5,LOW);//led 4
    digitalWrite(6,LOW);//led 5
    digitalWrite(7,LOW);//led 6
    digitalWrite(8,LOW);//led 7
    digitalWrite(9,LOW);//led 8
    digitalWrite(10,LOW);//led9
    digitalWrite(11,LOW);//led 10
    digitalWrite(13,LOW);//switchpin
    digitalWrite(14,LOW);//laserpin
    digitalWrite(15,LOW);//red activity LED
  delay(10); 
  digitalWrite(12,HIGH);  //resetpin
  delay(10);
  pinMode(12,OUTPUT);  // resetpin
  
}

void loop() {
 
int switchState = digitalRead(switchPin);
int laserState = digitalRead(laserPin);

if (graphLed == 12){
    digitalWrite(2,LOW); //led 1
    digitalWrite(3,LOW);//led 2
    digitalWrite(4,LOW);//led 3
    digitalWrite(5,LOW);//led 4
    digitalWrite(6,LOW);//led 5
    digitalWrite(7,LOW);//led 6
    digitalWrite(8,LOW);//led 7
    digitalWrite(9,LOW);//led 8
    digitalWrite(10,LOW);//led9
    digitalWrite(11,LOW);//led 10
    graphLed = 2;}
    
if (switchState == 1) {
   digitalWrite(resetPin,LOW);   
}
if (laserState == 0) {                        
  digitalWrite (graphLed, HIGH);
  graphLed = graphLed + 1;
  digitalWrite (15, HIGH);
    delay (2000);
  digitalWrite (15, LOW);}


   
 
}

Here are the components I used:

arduino pro mini

laser receiver module

10 Segment Digital Red LED Bar Graph
http://www.ebay.com/itm/111968067497?_trksid=p2060353.m2749.l2649

Hot Digital Touch Sensor Module Capacitive Switch
http://www.ebay.com/itm/291714864388?_trksid=p2060353.m2749.l2649

The reset pin on the Arduino is usually Held high by a pull-up resistor. Pulling it low (connecting it to ground) via the Hardware Switch should work perfectly. That's how the on-board reset Switch on the Arduino does it.

Why are you using the hardware reset from software?
Seems a bit unusual.
Dwight

Why do you think that you need to reset your Arduino? You can simply reset the counter when you press the button.

if(digitalRead(switchPin) == HIGH)
{
  graphLed = 2;
}

And as you already have something to clear the display as well

  if (graphLed == 12 || digitalREad(switchPin) == HIGH)
  {
    digitalWrite(2, LOW); //led 1
    digitalWrite(3, LOW); //led 2
    digitalWrite(4, LOW); //led 3
    digitalWrite(5, LOW); //led 4
    digitalWrite(6, LOW); //led 5
    digitalWrite(7, LOW); //led 6
    digitalWrite(8, LOW); //led 7
    digitalWrite(9, LOW); //led 8
    digitalWrite(10, LOW); //led9
    digitalWrite(11, LOW); //led 10
    graphLed = 2;
  }

Note: I hope you have a pull-down resitor connected to the switchPin. If not, the input is floting and cn randomly affect the behaviour of your application.

I guess I attempted to use the hardware reset through software because of my inexperience with Arduino..

Thanks to sterretje for a simple quick solution.

if I took my head out of the box for a minute, I may have seen it.

Works GREAT!!

Thanks.
Pete