Oof… Midterm project…
For my midterm project, I ultimately ended up creating some sort of game. This game requires two players: one to press a button and the other to push the ball. The goal of the game is to press the button when the ball passes right under the distance sensor. If done correctly, the green LED will light up. If the button is pressed at the wrong time, the red LED will flash.
For this project, I used two LED lights, a distance sensor, and a momentary switch. The goal was created out of wood and attached to a piece of cardboard. The schematic for this is attached below.
The code for this assignment is also listed below:
// defines pins numbers const int trigPin = 9; const int echoPin = 10; const int GreenLED = 2; const int RedLED = 3; const int button = 4; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode(GreenLED, OUTPUT); pinMode(RedLED, OUTPUT); pinMode(button, INPUT); Serial.begin(9600); // Starts the serial communication } void loop() { int buttonState = digitalRead(button); Serial.println(buttonState);//testing for button state // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); //conditions for the game if (buttonState == 1){ //if press button when ball is there, flashes green light if(distance < 11){ digitalWrite(GreenLED,HIGH); delay(100); digitalWrite(GreenLED,LOW); }//if press button when ball is not there, flashes red light else if(distance >=11){ digitalWrite(RedLED,HIGH); delay(100); digitalWrite(RedLED,LOW); } } }
I used the code from https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ for the distance sensor.
This project revolves around gathering two types of input. The distance sensor checks if the ball is beneath it while the button is pressed to match the moment the ball is underneath the sensor. Conceptually, it is quite simple and most of the project revolved around building the game. A video of the project is shown below (I didn’t have anyone to test the game so it’s just a small demonstration of it working):
I faced many problems while making my midterm project. At first, I wanted to create something that resembled football (or soccer). I attached the little touchpad sensors into the goal. However, I realized that there would not be enough force from the ball to even trigger the sensor: I would never obtain a value from the sensor. I could have used an even heavier ball, but I was limited by the strength of the servo motor. There was no way that a servo motor could hit a ball with a strong enough force to trigger the sensor. After that, I basically had to scrap that idea. My first idea wouldn’t work, so I thought of using something that resembled goal-line technology in football. It basically checks whether or not the ball passes the line to determine if a goal is a goal. However, I quickly stumbled upon another issue: I could not get more than one distance sensors to run. It would always just show the distance for the other distance sensor as zero. I was slightly discouraged after this, but I thought of still using a distance sensor and a button. I ultimately came up with what I have now. For this project, I wasted lots of time soldering and building things that I never really did use, which is quite unfortunate. However, I think the project turned out alright. The sawing and drilling part was pretty fun.
For this project, I learned quite a few things. Firstly, managing the wires is something I need to keep in mind. Things will become a mess if I don’t keep track of where each wire goes. Additionally, I should have the entire game stationed on like a piece of wood and cardboard to allow for easier transportation. Looking at other people’s projects, I feel like I should think outside of the box and try some different things. I thought how Will used three potentiometers to make an audio lock was pretty darn amazing. Looking back, there’s definitely a lot skills I learned and can be used for my final project.
Great job with this game. It was fun and the interaction was reliable and satisfying. I like that you explained what your initial idea was, the problems you ran into with that, the changes you came up with, and the new problems you ran into, and how you finally arrived at the working game. Great job coming up with creative solutions to problems! Your schematic is clear and correct (although the LEDs are missing the little arrows; without the arrows they are regular diodes (that’s what the letter ‘D’ stands for in LED)).
Your code could do with better comments; for instance in the line
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
the comment doesn’t add anything to what the code already says; however, you could have mentioned that the trigger is what initiates the ultrasonic pulse coming out of the sensor, or something like that. In the code on the blog there are no blank lines in the code. I don’t know if this is what your program looks like or whether WordPress ignored them. If you did write it this way, I encourage you to add some blank lines to separate different logical tasks.
As far as construction, I like the heavy piece of wood for the button. If players were playing this game seriously, the button would get lots of abuse, and the solid base would stand up to that. In your post you anticipated my comments about having everything on one base and making the rest of the game a little stronger. Overall though great job!