This is not the first time I’m doing something with the Arduino, but it is the first time I made it considerably large. To pass for the course Technology for Interaction we had to do an assignment, either in a group or alone. (I’ve decided to do it alone).
These were the requirements of the assignment:
- At least one moving part or sound
- At least one communication part (LEDs or LED matrix)
- Responds to user input (movement, sound or light)
- Sends data to the internet to a remote server (wireless = better than wired)
- Receives data via the internet from a remote server (wireless = better than wired)
- The code is understandable and follows a logical structure
- Logical variable names and function names are used
- Hardware module follows a logical structure
- Your contribution is always a combination of hardware and software
The hardware
This is basically connecting everything as specified in tutorials / documentations. Only the speaker is really connected differently (using 2 digital pins instead of 1). This is because I tried to see if I could somehow load sound files to play on the speaker, even though I don’t have a place to store the data on. That’s when I found the avr sound library, they are using 2 pins instead of 1. I still didn’t connect it the same way as specified in their documentation, but because I don’t have a transistor I managed to still make it work decently this way.
The D1 Wemos Mini is connected using the hardware serial, at first I had it connected using software serial, but the software serial had a much larger error rate (when sending message “hello” often “hdllo” was received for example). This wasn’t a problem for the hardware serial, so to not have to deal with bad serial communication (a lot) I decided to go with the hardware serial monitor.
The software
The program consists of 4 different code bases:
The Arduino:
- Listens for commands from the D1 Mini to control the Servo, Led matrixes and Speaker
- Keeps updating actions corresponding to commands given, eg. if “wave” is send the servo will keep waving from left to right and right to left until another servo command is given.
- Sends changes from the POT-meters and buttons back to the D1 Mini
- Is NOT playing pong: merely listens to “put these pixels on on matrix 1” and stuff like that, it is not aware of playing ‘pong’.
D1 mini:
- Is connected to the internet through a WiFi hotspot on my smartphone.
- Connects to a websocket server
- Listens from Serial communication from the Arduino and sends these commends to the server
- Listens for events from the server and sends these to the Arduino
Node server:
- Uses socket.io to create a websocket server
- listens for client connections (both arduino and website), awaits an “intro message”
- Arduino will send arduino intro message
- Client will not send intro automatically the way my source code is uploaded, I did this because I received a ghost-player while testing and did not want this to happen while I’m being graded, so instead you require to send the intro command in the debugger console. sock.emit(“intro”, “Leonie’s secret ;)”). After receiving the correct intro message it will either make listeners for the Arduino or the Client.
- Sends Arduino’s commands to the Client, and Client commands to the Arduino.
Web client:
- Has a very simple poing game, and some debugger buttons/slider to control and test matrixes and servo.
- Yellow is “Tails” and Blue is “Sonic”.
- When Tails is 3 points ahead of Sonic, it will send the “wave” command to the Servo; making Tails happy. It will also send a low-pitch sound to the speaker every time tails scores (this is basically Sonic’s sad sound, it will play everytime Tails scores and is 3 points ahead). As soon as Sonic scores Tails will stop waving; need to focus!
- When Sonic is 3 points ahead of Tails, it will send the servo a command to go to position 175(degrees) making tails turn his back on you; he is sad. Sonic will send a high-pitch noise to the speaker every time he scores as long as he is 3 points ahead.
- Every time tails scores matrix 1 will be updated adding one to his score, every time sonic scores matrix 2 will be updated adding one to his score.
- Game retrieves position numbers from analog1 and analog2 pot meters, it will position the players paddle according to these numbers.
- When the blue (sonics) button is pressed, the ball gains +1 in speed, for the yellow button (tails) it will slow down (-1) in speed with a minimum speed of 1.
- When one player scored 11, the game will stop updating for a while, after a bit it will either show “sonic” or “tails” on the led matrixes (depending on who won). Then after another small delay it will send the speaker “gotime” command, the speaker will play a sound from a sonic sound effect file, then after another delay the game will start again with 0-0, it will reposition tails (servo) at the center.
Source code: https://git.littledev.nl/leonie/ArduinoWeb