The software on this project was written using Python and C++. The goal for software was to write neat, organized, and well documented code. Due to this, a lot of thought went into which events would be handled in Python and which events would be handled in the Arduino IDE. The decision was made to use the IDE to filter and send sensor data. The rest of the event handling was done in Python in order to allow Python to have access to the state of the game. Below is a more detailed breakdown of the Arduino and Python code.
Variable Declaration
- Declare six integer variables, (hundred, fifty, thirty, twenty, ten, zero) representing analog pins A0 to A5
- Set a threshold value (sensorThreshold) to 500 for sensor readings
- Declare three arrays (sensorVals, prevSensorVals, sensorReadings) to store current sensor values, previous sensor values, and raw sensor readings, respectively
- Declare a boolean array (changeReadings) to track whether there is a change in the sensor readings
- Set the total number of sensors (numSensors) to 6
- Initialize a boolean variable (sendList) to false
setup()
- Initialize serial communication at a baud rate of 9600
readSensors()
- Read analog values from each sensor and store them in the sensorReadings array
processState()
- Iterate through each sensor in sensorReadings
- If a sensor reading is below sensorThreshold, set the corresponding index in sensorVals to 1
- Check if the index of sensorVals is equal to the corresponding index of prevSensorVals
- If the readings are different, update changeReadings to True, otherwise, set that index to False
If a sensor reading is above sensorThreshold, set the corresponding index in sensorVals to 0
- Check changeReadings to see if there was a change in the readings
- Set sendList to True if there was a change in the readings
loop()
- Read sensor values using readSensors()
- Process/filter the sensor state using processState()
If sendList is true, concatenate the sensor values into a String (data) and print it to the serial monitor
The python code handles all the state processing and display code. Following the Model-View-Controller format, the code is split into the following four files: skee_ball.py, parse_messages.py, model.py, and view.py. The only difference between this code organization and the MVC organization is that there is no Controller class. This is because the all the user input events are handled in the Arduino IDE. The only user events that need to be processed are sensor readings. Therefore, there is no need to handle user-input events.
parse_messages.py
This file contains the parse_message function, which takes in the Arduino port, the baud rate, and the Arduino serial object. The data is read, decoded, and returned as a String
model.py
This file contains the Model class, which is responsible for all game state handling. This class handles score updating based on the Arduino data received, updating tries based on when the user scores, and updating whether the game is active. Everything concerning the state of the game is stored in Model.
view.py
This file contains the View class, which is responsible for displaying the game. In this class, all of the images (starting screen, main screen, final screen, numbers) are loaded, the pygame window size is set, and the pygame world is created. Following the MVC format, when initialized, an argument of this class is an instance of Model. This class also contains methods to display all the screens. Finally, this class contains a method that displays the updated score.
skee_ball.py
This file is the file that runs the game. In this file, an instance of Serial is created, and Pygame is initialized. Then, an instance of Model and View are created. After this, the start screen is drawn, then the main screen. The data from the serial monitor is read (parse_message() is called), and the score and number of tries are updated. These updated values are then drawn on the screen. After the game ends, the final score screen and final score are drawn. Then, the game resets.
The main challenge throughout this process was reading the data in Python. After figuring out a way to filter the data, writing the code and sending it to the serial monitor was fairly straightforward. However, when it came to reading and decoding the data, the issue was that there was no information being sent to Python. The Arduino IDE was printing to the serial monitor, but Python was not receiving it correctly. It took about two to three hours to debug, and the issue ended up being simple: instead of reading the data as serial_object.readline(), the data was being read as serial_object.readline().decode(). After fixing this issue, serial communication was successfully established.
We also ran into issues displaying the score on the Raspberry Pi. The Pi was not able to decode the sensor readings. Because the code already worked on the laptop, we decided that debugging was not worth our time, so we ran the code on our laptop.
One goal for the future would be to create a leaderboard. Currently, there is no leaderboard as the information resets every iteration of the while loop. However, implementing a leaderboard would increase the quality of the game and the enjoyment of those playing it. When we let others play the game, we noticed that everyone was very competitive, and it made the game more enjoyable as well as encouraged people to come back and play more rounds.
Another future goal is to allow users to input their names. This adds a more personal touch, and makes the game more fun because then users are able to see their names on the leaderboard. Due to time constraints, we prioritized a polished and reliable scoring and display system over adding additional features.
A few different modules/libraries were imported in writing this code. The first library was Pygame, which made the display possible. We also imported the Pyserial module, which allowed the Python script to read from and decode the data from the serial monitor. All information relating to necessary dependencies can also be found in the project Github, linked here. All of the information necessary to import the modules and successfully run the code can be found in the README.
created with
HTML Website Builder .