Wire Loop Game

I am using the following as a basis for a wire loop game (something like )

int speakerOut=9;
int LEDPin=13;
int handle=2;

void setup() {
  pinMode(handle, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);

}

void loop() {
  int sensorVal = digitalRead(2);
    
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
    analogWrite(speakerOut, 255);
  }
  else {
    digitalWrite(13, HIGH);
    analogWrite(speakerOut, 0);
    delay(250);
    analogWrite(speakerOut, 255);
  }
}

Question: Without adding a clock, what is the best way to include a timer that can record how long someone takes.

The arduino has built-in timers that you can use, and the simplest one are millis() and micros() functions.
To not cause a misunderstanding, the millis() and micros() function don't tell you the elapsed time, without first recording the current time(start of game) and finding the difference in time at the end of the game.

unsigned long TimeDiff, EOG, SOG;
...
SOG = millis(); // once you begin, record the current time
/* Play the game */
EOG = millis(); // game is over, get the new current time
TimeDiff = (EOG - SOG); // find the difference

If the game monitors the wire as touched or not, then how will the arduino know when the person has completed the game correctly (no cheating) ?

Thanks… yes I am still trying to work out the logic ie start and finish. I am thinking of using two more pull up pins connected to a start end end washer. When you move off the start washer the timer starts and when you touch the end one the timer stops. If you hold the record then a sound plays or another LED flashes… something like that.

Schematic1.png

Or just cables that act as start and finish brushes ie you brush past them

The washers sound rather good. Go with it.

Just being playing a little. I can probably get by with only a finishing washer as you can do an intentional short at the start to begin.

int speakerOut=9;
int LEDPin=13;
int handle=2;
boolean Timing=false;

unsigned long TimeDiff=0, EOG, SOG;

void setup() {
  pinMode(handle, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);

}

void loop() {
  int sensorVal = digitalRead(2);
    
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
    analogWrite(speakerOut, 255);
  }
  else {
    digitalWrite(13, HIGH);
    analogWrite(speakerOut, 0);
    delay(250);
    analogWrite(speakerOut, 255);
    if ((!Timing) && (TimeDiff=0)) {
      SOG = millis();
      Timing=true;
      }
     else if (TimeDiff>0){
       Timing=false;
       EOG = millis();
     }
  }
}

Just need to add the finishing washer and some code and it will be there.

Haven’t fully tested the logic but think its basically correct.

Thanks :slight_smile:

Edit, I didn't put in the wire. Give me a sec.

Better.

/*

  Make the handle ground

*/

const byte speakerOut = 11;
const byte LEDPin = 13;
const byte theWire = 2;
const byte StartPad = 3;
const byte FinishPad = 4;
boolean Timing = false;
bool result = 0;
bool GameOver = false;
float TimeDiff = 0, EOG, SOG;

void setup()
{
  Serial.begin(115200);
  pinMode(StartPad, INPUT_PULLUP);
  pinMode(FinishPad, INPUT_PULLUP);
  pinMode(theWire, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);
  
  noTone(speakerOut);
}

void loop()
{
  while ((digitalRead(theWire) == HIGH) && (GameOver == false) )
  {
    byte SPAD = digitalRead(StartPad);
    byte FPAD = digitalRead(FinishPad);

    digitalWrite(13, !(SPAD ^ FPAD)); // LED will be ON if neither the StartPad or FinishPad are touched with the handle

    if (SPAD == LOW) // handle is touching the StartPad
    {
      TimeDiff = 0;
      Timing = false;
    }
    else //SPAD is not being touched by the handle
    {
      if (!Timing)
      {
        SOG = millis(); // record current millis
        Timing = true; // lockout this IF statement
      }
    }

    if (FPAD == LOW) // See if FinishPad is touched with handle
    {
      EOG = millis(); // get the new current millis
      Timing = false; // reset start time
      result = 1;
      break;
    }
  } // end of while loop
  
  GameOver = true;
  
  if (result == 1)
  {
    Serial.println(F("You Win"));
    Serial.println((EOG - SOG) / 1000); // print the difference in seconds
    delay(1000);
  }
  else
  {
    Serial.println(F("You touched the wire, You Lose"));
    delay(1000);
    tone(speakerOut, 500);
  }

  if (digitalRead(StartPad) == LOW) // returning the handle to the StartPad, resets the game.
  {
    GameOver = false;
    result = 0;
    noTone(speakerOut);
  }
}

Hi thanks to all for the input. Unfortunately I didn’t see the last reply as for some reason I am not getting any e-mails notifications even though I am subscribed (but that’s a different issue; yes checked my spam too).

I have finally finished the first stage. I will add the timer later and tidy up the code too but for now I thought I would just show it off :slight_smile: It’s been fun and there are heaps of options for expanding on the project.

int speakerOut=9;

int LEDPins[]={13,12,11};
int count = 0;
int LED = 0;
int vol;

const int volIncr = 63;

boolean playing = true;

int wire=2;
int finish=3;

void setup() {
  
  pinMode(wire, INPUT_PULLUP);
  pinMode(finish, INPUT_PULLUP);

  pinMode(LEDPins[0], OUTPUT);
  pinMode(LEDPins[1], OUTPUT);
  pinMode(LEDPins[2], OUTPUT);
}

void loop() {
  if (playing) {
    int sensorVal = digitalRead(wire);
    int isComplete = digitalRead(finish);
   playing = (!(isComplete == LOW));  
    if (sensorVal == LOW) {
      digitalWrite(LEDPins[count], HIGH);
      if (count<3) vol=255-volIncr*(count+1);
        else
      vol = 0;
      analogWrite(speakerOut, vol);
      delay(250);
     if (count<3) analogWrite(speakerOut, 255);
     count=count+1;
    }
  }
  else
  {
    for (count=2;count>=0;count--) {
      digitalWrite(LEDPins[count], HIGH);
      delay(100);
      digitalWrite(LEDPins[count], LOW);
      delay(100);
    }

  }
}

For now I simply use the reset button to start again.

Stuck the whole thing together with a glue gun so very simply to assemble.

Hey abasel , im currently working on a similar project , im wondering did you find out a way for start and finish of the game ? Im planning on adding a timer and counter to this and need a start and finish mechanism.

You might be lucky. abasel last posted on the 8th of November 2015 - about three months ago. Is he still monitoring these forums?

It might be better to send him a personal message.