Arduino Programming Practical
Arduino Programming Practical
In this weeks blog, I will be talking about my experience with the second CPDD practical of the semester. For this practical, we were tasked with programming an Arduino board to complete the assigned task. All groups were given the same task of making the wings of a cardboard pegasus flap continuously using the 180-servo motor provided. Other than the 180-servo motor and the cardboard pegasus, we were also provided with 30cm of metal wire, a hot glue gun, some plain cardboard as well as everything that is included in the Maker UNO Arduino kit that was provided to us a few weeks prior.
Construction Phase
Before we could get into the Arduino programming portion of the practical, we first had to decide on how to get the wings of the pegasus to flap using the 180-servo motor. We had initially thought of tying the tip of the servo to the intersection of the 2 wings within the body of the pegasus and then "stuffing" the servo into the pegasus body in order to hide it. We wanted to hide the servo as we thought of commercially sold toy designs that include electrical components. In those designs, the electrical components are not visible and are usually hidden within the toy itself, hence we wanted to replicate that as this cardboard pegasus is ultimately a decorative toy. However, when we implemented the code to the servo using this design, we discovered a fatal flaw, the inside of the pegasus body is too narrow to allow for full rotation of the servo. This resulted in the flapping motion being incomplete as the servo motion was being restricted. This meant that we had to place the servo motor outside the body which raised the next question of how we were going to conceal it. After some brain storming, we decided to create an elevated platform for the pegasus the stand on, in which we would cut a hole for the servo to sit in. The hole would then be covered by a little cardboard cap to fully conceal the servo, this will not only conceal the servo motor to make it more aesthetically pleasing, but it will also prevent the servo from moving around which would ensure that the flapping of the wings are consistent. We also glued the legs of the pegasus to the base to increase the stability of the toy. Below are pictures of the sketches and final design.
Programming Phase
After we had settled on the design, we had to implement the codes into the Arduino board. In the programming phase, things went rather smoothly as the code used is almost the same as the default servo code used in the individual pre-practical. The code can be found within the Arduino IDE software under File>Example>Servo>Sweep. The only difference is that we had reduced the delay of the servo from 15 milliseconds to 2 milliseconds. This was to increase the speed and fluidity of the flapping motion of the wings. The rest of the code is left in default configuration where the servo will start at 0, go to 180 and then back to 0 in an infinite loop.
Challenge
Apart from the practical activity itself, our group was also given a challenge by one of the lecturers which was to implement an ON/OFF button into the toy as well as to have a melody play when the wings are flapping. This took us by surprise as we did not expect to be given "homework". The programming for this one was a lot harder than the one used in the activity as we had to implement 3 different commands rather than just one. Before we started programming, we came up with a flowchart to illustrate our thought process on how we were going to complete the challenge. Below is the thought process flowchart.
The code int melody[] is an array where integer information is stored in a melody array. Get the song melody from online and convert it into the form written in the pitches.h library.
The code int noteDurations[] defines the note duration into an integer.
The code for(int thisNote = 0; thisnote <24; thisNote++){} indicates a loop.
- int thisNote refers to the declaration of a temporary variable.
- thisNote < 24 refers to thisNote is less than 24
- thisNote++ refers to thisNote +1
Note: Since the melody has 24 notes, this means note will run 24 times and then will break out of this loop. Thus, the full song melody will be played.
Within the loop,
The code int noteDuration = 1000 / noteDurations[thisNote] means that this code will calculate the note duration using the formula of taking one second and dividing it by the note type.
The code tone(8, melody [thisNote], noteDuration) means that it will play the note and duration on PIN8.
The code int pauseBetweenNotes = noteDuration * 0.5; delay(pauseBetweenNotes) means that the code will create a short break in between each note to make each note distinct and not mash them up all together to create and inaudible sound.
The last code, noTone(8) means that it will stop playing any notes on
PIN8.
Now that we are done with the first 2 segments, it is time to move on to the final segment which is the servo segment. This segment is quite similar to the code used in the practical itself but some adjustments will have to be made in order to incorporate the 2 other segments. Just like in the practical, the code used is a modified version of the default code that can be found within Arduino IDE under File>Example>Servo>Sweep. Once loaded, modify the code so that it looks like this.
Figure 8: Code for Servo
The code #include <Servo.h> means that it includes Servo’s Library
The code
Servo myservo; means that the line refers to create a servo object named
“myservo”
The code
int pos=0 is creating an integer variable called pos, and of starting value, 0
*pos stand for Position
In the void
setup, myservo.attach(9) means that we are declaring that the servo motor is
attached to PIN9.
The code for (pos = 0; pos <= 180; pos +=1) means
- this code sets pos=0
- if pos is less than or equals to 180, it will increase itself by 1
The code myservo.write(pos) means that it will write the value of pos and be sent to the servo motor.
The code delay(2) means that there will be a wait / delay for 2 milliseconds.
After everything has been coded correctly, the 180-servo motor will be able to move from 0° to 180°, which will help flap the Pegasus wings.
Lastly, we will be adding all the codes together.
To
combine the codes together, simply place all the codes which are under
“void_setup” together, all the codes under “void_loop” together, and lastly all
the remaining codes which are not under any categories to be placed together at the top
of all the codes. With this done, your combined codes should look like this.
Figure 9: Combined Code Pt.1
Figure 10: Combined Code Pt.2
Figure 11: Combined Code Pt.3
Since we had put a loop() for the melody and the servo code is looped under the melody, based on what we had programmed, after 1 note of the melody is played, the servo will then move from 0° to 180°, and back to 0°. After the servo has done this action, the melody will play another loop. As we have a total of 24 melody notes, it will stop after 24 loops. Having the delay for the servo set to 2milliseconds will make the servo move faster and thus the melody will be played faster, which will have the effect of the melody playing continuously while the wings are flapping.
The link to the code used for Pegasus wings with melody toy challenge and preview of it in action:
Individual Work
For the individual work, we have 4 tasks that we have to complete by using TinkerCad, 2 of which are input devices (Potentiometer & LDR) while the other 2 are output devices (3 Fading LEDs & ON/OFF button DC motor).
For the first task, 1a, I had Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE. Using TinkerCad, replicate the connections made in the picture below:
Note: The exact layout does not have to be strictly followed and the positions used can be altered to your liking as long as your wiring matches with what you have chosen.
Once your board has been set-up, you can now move on to the coding. The code I used is shown below. Do note that the PIN numbers used will depend on how you wired your potentiometer and LED to the Arduino board.
Feel free to play around with the embedded simulation below or use the exact TinkerCad circuit I used via the link provided:
Comments
Post a Comment