Pages: [1]   Go Down
Author Topic: Ultrasonic ping-pong game  (Read 1709 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, just want to share with you a little project I finished few days ago. Its classic Atari pong game but controled by two ultrasonic range sensors (SRF05).

http://youtu.be/6JomPCmY5RQ

Source code for arduino
int ultraSoundpin1 = 7;
int ultraSoundpin2 = 5;
unsigned long ultrasoundDuration;

void setup() {
  Serial.begin(9600);
}

void loop() {

  // switch pin to output
  pinMode(ultraSoundpin1, OUTPUT);

  // send a low, wait 2 microseconds, send a high then wait 10 microseconds
  digitalWrite(ultraSoundpin1, LOW);
  delayMicroseconds(2);
  digitalWrite(ultraSoundpin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraSoundpin1, LOW);

   // switch pin to input
  pinMode(ultraSoundpin1, INPUT);

  // wait for a pulse to come in as high
  ultrasoundDuration = pulseIn(ultraSoundpin1, HIGH);

  // output
  Serial.print(ultrasoundDuration);
  Serial.print(";");

 
    // switch pin to output
  pinMode(ultraSoundpin2, OUTPUT);

  // send a low, wait 2 microseconds, send a high then wait 10 microseconds
  digitalWrite(ultraSoundpin2, LOW);
  delayMicroseconds(2);
  digitalWrite(ultraSoundpin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraSoundpin2, LOW);

   // switch pin to input
  pinMode(ultraSoundpin2, INPUT);

  // wait for a pulse to come in as high
  ultrasoundDuration = pulseIn(ultraSoundpin2, HIGH);

  // output
  Serial.print(ultrasoundDuration);
  Serial.print(":");

  delay(50);
}



Here is source code for computer application (made in Delphi) are here

Delphi and arduino source codes together with all files which are necessary to compile it are here
To compile delphi app you will need com port component. You can download it here.

« Last Edit: November 17, 2011, 11:16:07 am by Lisax » Logged

SouthEast UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No replies yet! so I'll post one. That is neat, and with the Delphi app as well. I will try the Pascal  code in Lazarus/fpc when I get a chance.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Somewhat shorter: (uncompiled, untested)
Code:
int ultraSoundpin [] = {7, 5};

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.print(ping (ultraSoundpin [0]));
  Serial.print(";");
  Serial.print(ping (ultraSoundpin [1]));
  Serial.print(":");
  delay(50);
}

unsigned long ping (int ultraSoundpin)
{
  pinMode(ultraSoundpin, OUTPUT);

  // send a low, wait 2 microseconds, send a high then wait 10 microseconds
  digitalWrite(ultraSoundpin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultraSoundpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraSoundpin, LOW);

   // switch pin to input
  pinMode(ultraSoundpin, INPUT);

  // wait for a pulse to come in as high
  return pulseIn(ultraSoundpin, HIGH);
}
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome!
Lisax.left_brain, you enjoy beating Lisax.right_brain? smiley-wink
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wiz: Yeah, try it, its really fun to play. All works great, there is just slight problem with sensors, they return a bit unstable values so paddles are shaky. I solved this problem in my delphi app with making average from last few values, that came from arduino (number of values from which should be the average made is in constant called fluidity). Or you can try another type of sensor.

Here is graph of values (target is static)


AWOL: Thank you, it looks way more elegant now

liudr: Yes, my right part of brain was really happy when it won smiley-grin
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats a great idea.

Well done. smiley
Logged

--
My New Arduino Book: http://www.arduinobook.com

Pages: [1]   Go Up
Jump to: