Ultrasonic ping-pong game

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).

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.

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.

Somewhat shorter: (uncompiled, untested)

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);
}

Awesome! Lisax.left_brain, you enjoy beating Lisax.right_brain? ;)

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 :D

Thats a great idea.

Well done. :)