basic arduino project (Input counter) Is my code correct ?

Hey guys ,
so im working on my first Arduino project which is to make a wire loop game. The aim of the game is to guide a metal wand through a wire maze. When the metal wand touches the curved wire a buzzer will go off. Im working on a code to count the number of buzzes

When the wire touches the loop input is sent to pin2. So far im able to light up led pin 13 whenever the wire touches the loop with the following code

int buttonState = 0;
void setup() {
 pinMode(13, OUTPUT);
 pinMode(2, INPUT);
}
void loop(){
 buttonState = digitalRead(2);
 while (buttonState == LOW) {
 digitalWrite(13, LOW);
 buttonState = digitalRead(2);
 }
 for ( ; ; ) {
 digitalWrite(13, HIGH);
 }
}

My next step is count the number of times input is detected. To do this ive just edited the existing code with bits of code from ButtonStateDetection example.

Unfortunately i dont have my arduino with me currently so i cant test any of the below code. Im wondering if it looks right ? what am i doing wrong?

// this constant won't change:
const int  buttonPin = 2;    // Input
const int ledPin = 13;       // the pin that the LED is attached to


// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize pin13 as an output: 
 pinMode(ledPin, OUTPUT);
 // initialize  pin2 as a input
 pinMode(2, INPUT);
 // initialize serial communication:
  Serial.begin(9600);
}


void loop(){
  // read the input pin:
 buttonState = digitalRead(2);
 
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of touches:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      
      Serial.println(" ");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
 
 while (buttonState == LOW) {
 digitalWrite(13, LOW);
 buttonState = digitalRead(2);
 }
 for ( ; ; ) {
 digitalWrite(13, HIGH);
 }
}

After this is complete i would like to add a timer to my game with every touch being a time bonus. As in have a timer counting how long someone takes to finish the loop (Have a stop button) and with each touch (Pin2 input) add 2 seconds to the timer, would this be difficult to code ?

Nope, probably not hard to code something like that.

The only thing that's more than basic flow control and introductory programming (which sounds like where you're at) is making sure you only get one count from each touch ("debouncing" - lots of info on the forums, that's the keyword)

for ( ; ; ) {
digitalWrite(13, HIGH);
}

Is this what you want? When it reaches that, it will hang forever digitalWriting pin 13 high and never do anything else. HCF?

Also, re: the "no arduino" problem - you can "verify" code (which you may have already done) without the Arduino. And check ebay - there are insanely cheap chinese arduino clones available, and the DoA rate is pretty low. I left a few pro mini's at my parent's home, and out summer cottage, so I never have to sleep more than 10 feet from an AVR with optiboot on it. :wink:

DrAzzy:
for ( ; ; ) {
digitalWrite(13, HIGH);
}

Is this what you want? When it reaches that, it will hang forever digitalWriting pin 13 high and never do anything else. HCF?

This is the problem with not being able to test ! what id like it to do is light led while the wire is touching the loop and turn off when it is not touching. For the first code i understand most of it except a few lines in loop.

 for ( ; ; ) {
 digitalWrite(13, HIGH);
 }
}

would it be possible to get a explanation of what is going on here exactly.

also i have an Intel Galileo at home i just wont be home for a week and thought i could work on my project anyway !

Look at the state change detection example. You want to do something when the state changes from not touching to touching and you want to do something when the state changes from touching to not touching.