In this weeks Arduino installation, Kilian and Martin created a little game called PushTheLukas - Air Edition. The goal of the game is to balance a ping pong ball on top of an air stream within a tube by pushing a small blue ball (see youtube video: http://www.youtube.com/watch?v=DJJg9OHGuKk). Every time the ping pong ball stays inside a target area, the player is being awarded credits. The higher the player scores, the larger becomes a random momentum added to the players input which makes balancing the ball more and more tricky.
Materials
paper board
toilet paper pipes
plastic forks
three resistors
piezo speaker
transistor
diode
wires
Arduino code
int DEBUG = 0;
// data
enum notes {c, d, e, f, g, a, b, C, COIN};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 300};
int potPin = 0, sensPin = 1, piezoPin = 7; // select the input pin for the potentiometer
int motorPin = 9; // select the pin for the Motor
int val = 0; // variable to store the value coming from the sensor
int sensVal = 0;
int lightThresh = 100;
int timeOut = 3000;
int counter = 0;
int output = 0;
int i = 0;
signed int randomy = 0;
void setup() {
pinMode(piezoPin, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(3));
}
void loop() {
//val = analogRead(potPin) * 0.6084 + 400; // read the value from the sensor, between 0 - 1024
val = reMap(analogRead(potPin));
sensVal = analogRead(sensPin);
analogWrite(motorPin, val/4); // analogWrite can be between 0-255
if(sensVal <= lightThresh){
if(counter == timeOut){
counter = 0;
addPoints();
playTone(COIN);
}
else counter++;
}
if(DEBUG){
Serial.print("Pressure: "); Serial.print(val, DEC);
Serial.print(" || Light sensor: "); Serial.println(sensVal, DEC);
}
}
void playTone(int note){
for(i = 0; i < 200; i++){
digitalWrite(piezoPin,HIGH);
delayMicroseconds(tones[note]-i);
digitalWrite(piezoPin, LOW);
delayMicroseconds(tones[note]-i);
}
}
int reMap(int input){
if(input == 0) return 0;
output = (input - randomy) * 1.8;
output = (output > 1023) ? 1023 : output;
output = (output < 0) ? 0 : output;
return output;
}
void addPoints(){
static unsigned int score = 0;
score += 1;
randomy = random(score*20);
randomy -= score*10;
Serial.print(score, DEC);
Serial.print(";");
//Serial.println(randomy, DEC);
delay(100);
}
Score Board