0% found this document useful (0 votes)
15 views4 pages

Ultrasonic Arduino Game - Lott Davidson

Uploaded by

faxity120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Ultrasonic Arduino Game - Lott Davidson

Uploaded by

faxity120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Spencer Lott & Hank Davidson

1. Red Light Green Cup

2. The game is played with 2 or more people and you rotate turns. Anj LED indicates when
a player starts their turn by turning on. The player must find the correct distance to place
their hand away from the sensor and when they find it a light turns on Indicating they
may now shoot the ping pong ball. A servo motor is activated indicating which cup the
player must go for and each cup has different point values. The game can go for 3-10
rounds. The player at the end with the most points wins the coin.
3. The range for the player to find with the ultrasonic sensor is random between 1-20 and is
mapped to the 180 degrees of the servo motor that determines which cup you can shoot
for.

4.
5.
#include <Servo.h>

Servo myservo;

int trigPin = 8; // Trigger


int echoPin = 9; // Echo
long duration, cm, inches;

int level = 1;
long targetDist ;
int pos = 0;
bool called = false;
int timer = 0;
bool timesup = false;
long degree;
void setup() {
// put your setup code here, to run once:
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
myservo.attach(10);
digitalWrite(5, LOW);
myservo.write(0);
Serial.begin(9600);
timer = 0;
timerstuff();

long rand = random(1, 20);

degree = map(rand, 1, 20, 1, 180);


}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343


inches = (duration / 2) / 74; // Divide by 74 or multiply by 0.0135

//Serial.print(inches);
//Serial.print("in, ");
//Serial.print(cm);
//Serial.print("cm");
//Serial.println();
if (level == 1 && !called) {

if (inches < targetDist) {


digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
} else if (inches > targetDist) {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
} else if (inches == targetDist) {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(2, LOW);
servostuff();
}
}
Serial.println(targetDist);
Serial.println(inches);

delay(100);
}

void servostuff() {
called = true;
// in steps of 1 degree

myservo.write(degree)

Serial.println("Called");
}

void timerstuff() {

for (timer = 0; timer < 10; timer++) {


delay(1000);
}
if (timer = 5) {
timesup = true;
digitalWrite(5, HIGH);
}
}

You might also like