Electronics and Programming

About Arduino

When setting up any Arduino Nano, there are a few important things to note. When the Arduino is plugged into your laptop the laptop is automatically sending it 5V of power. If you would like the code to run independently of a laptop, ensure that your Arduino is powered by a 5V power supply connected to Vin. The Arduino board should also be grounded. Meaning the ground wire of your power supply is connected to one of the GND pins on the board. When this is set up correcting the board should have a red light that turns on. 

From here, there are many different configurations and functions for an Arduino Nano. Notably, there are both digital and analog pins on the board. Indicated by either a A or D in front of the port number. Digital pins receive binary signals. In these pins there either is or is not a signal. This is great for relaying information about things like buttons where they are either pressed or not pressed, and there is no in between. The analog ports allow for a range of data. These are great for motor control as you can set a variety of angles, not just up or down. It is also great for things like light sensors that may have a range of values to communicate the amount of light seen. 

Arduino Nano Ports

When figuring out how to set up the electrical board, one key tool was the diagram to the left. It clearly outlines the purpose of each port and in green/ purple shows what would need to be typed into the code to access that port. 

Breadboard Diagram

Breadboard Basics

A bread board is a board used to connect electrical components. It is often used in programming as it is extremely adaptable and easy to set up. A diagram of how the pins are all connects is displayed on the right. 

Each breadboard also contains two sets of power rails to connect components to ground and to a given voltage. In the center of the board there are two columns. The pins in the rows of each column are connected. This means that if I plug a wire into one of the pins in a given row and then plug another pin into the same row, electricity will flow from one wire to another. However, if I plug the second wire into a different row, or even the same row in a different column the wires will not be connected. 

In the case of the Arduino, it connects on the bread board so that each of the pins are in a different row and column. In this way the pins are still independent of each other. If for example a student wants to connect a wire to Vin, they can now plug a wire into the same row as the Vin pin and it will be connected. 

Setting up our Electrical Board 

In our design, we used one bread board half of the bread board was used for the Arduino Nano for the musical lights and the other half was used for the motors and buttons for the pinball machine. Each is powered by their own 5V power supply. 

(Based on the number of ports used, it would be possible to combine these two into one board. However with the current code for each use two different baud rates. Since each code needs to communicate in different frequencies it was not possible to combine them. If it was possible, we would have used an interrupt in the code each time a button was pressed.)

The set up for this is shown below. 

Image of the Electrical board on the display
Electrical Diagram

Programming

In order to code in Arduino you must download the Arduino IDE. From here you can open a new program and begin to code. Plug your Arduino into your laptop. Once your code is ready to test, click the compile button (Check at the top of the screen). Once it says compiling is completed, you can click the upload button (arrow at the top of the screen). If this does not successfully upload, refer to the links below which provide step by step instructors for setting up your system. 

In Arduino there are two loops given in the default code. One is called setup and the other is called loop. The code inside the parentheses for set up will run once. This is typically used to initialize any components on the board, set the frequency of communication, and perform any other starting tasks. The loop runs continuously. This is where the main functions of the Arduino will be written. Refer to the links below to find out more about the built in commands in Arduino

New Code file in Arduino
FlippersSinglePlayerPinball.ino
Single Player
FlippersFeb1.ino
Double Player

Pinball Code 

To the left is the links to the code that we used to program both the single player and double player machines. For the single player machine there are two buttons and two servo motors. When the button is pressed, the corresponding motor goes from a 0 degree angle to an 80 degree angle then returns to the 0 degree angle. Delays are used to give the servo time to reach the ideal position. The code is commented with more details about its functionality. The dual player machine functions the same way, but with twice as many components. 

Troubleshooting

If you are having trouble running the code because you do not have the option to select a port to communicate with the Arduino through. You likely need to download the Arduino Drivers. Please google "Arduino Driver CH340 download" There are many links that will appear with step by step instructions on how to download it. Once it has been successful installed, check to see if you still are having this error. 

If you are running the code and the flippers or button is not reacting properly. Run the Test_Button File and the Test_Motor File below. If these run, check the code. If these do not run properly, check the wiring. Also double check that the ports that the servo and button are plugged into align with the constants defined in the top of the code. 

FlippersSinglePlayerPinball.ino
Code to Test Button
Test_Servo.ino
Code to Test a Motor

Other Resources to Learn to Code in Arduino

Setting Up Programming in Arduino Nano: https://linuxhint.com/program-arduino-nano/

Arduino Driver CH340: https://sparks.gogo.co.nz/ch340.html

Built-In Arduino functions: https://www.arduino.cc/reference/en/

Coding in Arduino: https://www.youtube.com/watch?v=BLrHTHUjPuw

Getting Started with Arduino Nano: https://www.youtube.com/watch?v=R102xfcx75I

Learn More About...