Skip to main content

ARDUINO Programming

Arduino Programming


Hello!! Welcome back to another blog ☺!!

This week, I have learnt about Arduino Programming 😎. Before the lesson, I was personally very excited to be learning programming as coders in movies looked really cool. However, after the lesson I realize that coding is really tedious and difficult😵. Even though it is difficult, this does not mean we should back down. With the helps of my classmates, I had learnt some really important basics of coding/programming, and in blog, I will show you what I had done and explain so that everyone can understand! The sense of accomplishment after each successful code is so rewarding!


What I have learnt from the RISE Package🧠:


1) Digital Signal (LED - 2 Values)
  • 0 - Off
  • 1 - On
2) Analog Signal (LED)
  • Can be any value
  • Adjust any level of brightness (dim to bright) using different values
3) There are 2 types of voltage
  • 3.3Volts or 5Volts
  • Located on the left side of the Arduino Board
4) Digital Input / Output (D0 - D13) pins.
  • These pins on the Arduino can be configured as either inputs or outputs.
  • 6 Pins (D3, D5, D6, D9, D10, D11) are marked with (~) are PWM
  • PWM stands for Pulse Width Modulation and it is a technique used in controlling brightness of LED, speed control of DC motor, controlling a servo motor or where you have to get analog output with digital means.
  • Located on the right side of the Arduino Board
5) Analog Input Pins (A0 - A5)
  • While the main function of the analog pins for most Arduino users is to read analog sensors, the analog pins also have all the functionality of general - purpose input / output (GPIO) pins (the same as digital pins 0-13).
6) 

  • GND - Ground
  • Buzzer is connected to Pin 8 (Turn ON)
  • To use Buzzer for other Pin (Turn OFF)
7) Mathematical Symbols & Interpretations

Basic Interpretation and Symbols


Advance Interpretation and Symbols


Variable Interpretation and Symbols

8) code: void setup (){...}
  • This is where we setup our board's pin identity. For example, we can set PIN13 as in input or an output, or simply make it blink on the board's built-in LED light.
9) code: void loop (){...}
  • This is the part where we want the code to run on an infinite loop.
10) code: //
  • To add comments for our code.
  • This can help remind us the reason why this line or chunk of code exist.
  • Comments will not affect our code.
11) PIN13 is the pin where the Arduino UNO's on board LED is attached to.
  • PIN13 can be represented at LED_BUILTIN
12) code: delay(1000); means that it will wait for 1 second.

13) PIN2 is the default pin for the programmable button.

14) pull-up means the pushbutton's logic is inverted. It goes HIGH when it is open, and LOW when it is pressed.
  • To configure PIN2 and an input and enable the internal pull-up resistor,
  • pinMode (2, INPUT_PULLUP)
15) code: if...else...
  • IF a condition is met, do something...ELSE perform another action...
16) There is a Piezo Buzzer which is tagged under PIN8.

17) code: #include "pitches.h" refers to the inclusion of all the pitches in a library so that the code can refer to the library if the code is being called.

18) code: int melody[] is an array where integer information are stored in melody array.

19) code: int noteDurations[] is similar to melody, and it stores information about the note duration.

20) function: for(condition), do{something}

21) code: for (int i=0; i<5; i++)
  • int i = 0 means that integer “i” equals to zero
  • i < 5 means that integer “i” is less than the value 5
  • i++ means that integer “i” value will +1 o this action will take place 5 times until integer “i” becomes the value 5 from the increment action of i++.
Therefore, the LED for PIN13 will blink for 5 times before it turns off by itself.

22) code: int noteDuration = 1000 / noteDurations[thisNote] means that this code will calculate the note duration, take one second and divide it by the note type.

23) tone(8, melody[thisNote], noteDuration); means that it will play the note and duration on PIN8

24) int pauseBetweenNotes = noteDuration*1.30; delay(pauseBetweenNotes) means that the code will create a short break in between sound to identify the difference.

25) noTone(8) means that it will stop playing any tones on PIN8.

26) Motor
  • 180 Servo Motor
    • When programmed, it moves to an angle according to the input we have given. It has a range from 0 - 180 degrees only.
  • Continuous Servo Motor
    • It moves a full 360 degrees , but when programmed, instead of it moving to a certain angle, the value we input affects the speed.27) code: #include <Servo.h> refers that it includes Servo's Library
27) code: Servo myservo refers to create a servo object named 'myservo'

29) code: int pos=0 creates an integer variable called pos, and of starting 0

30) Colors of DUPOINT wires
  • Bright colors (RED, YELLOW, WHITE) for 5V
  • Dull colors (BLACK,GREY, BLUE) for GND
  • Fun colors (ORANGE, GREEN, PURPLE) for DATA
31) Dupoint to Servo
  • Bright colors to red wire of servo
  • Dull colors to brown wire of servo
  • Fun colors to orange wire of servo

🤯 Now, let us try some fun activities!


Individual Work

Challenge #1a: Interface a Potential Analog Input to make UNO board and measure its signal in serial monitor Arduino IDE

Since I am not that good with programming, lets use a step-by-step approach.

Firstly, let us start by selecting all the components we need!
Here are the components which we would need: 
Arduino board, Breadboard, Potentiometer, Wires, LED light, Resistor and a Multimeter.

Secondly, lets start building these components together. The steps are summarized:
  • Connection of Arduino Board to the Breadboard
  • Connection of Arduino Board to Multimeter
  • Connection of Arduino Board to LED
  • Connection of Arduino Board to Potentiometer.
This is the end result!


Thirdly, to allow our built Arduino set-up to work, we will need the help of codes. Lets use what we had learned earlier to help us!

Using TinkerCad, even a beginner like myself is able to code using the simplified coding system; Blocks. This coding system has really helped me enhanced my understanding and knowledge of coding.
  1. Under variable, lets create a new variable called "SensorVal" which stands for sensor value for easier reference when writing out codes.
  2. Using our new variable, we use the block "set SensorVal to" and add the block "read analog pinA0" which is located under input.
    RECAP: We are using Analog so that we can adjust the level
  3. Next, use the block "set built-in LED to" and select HIGH.
    RECAP: HIGH refers to being turned on.
  4. After that, use the block "wait ... seconds" and add in our variable "SensorVal", and change seconds to milliseconds.
  5. Lastly, by repeat step 2 for turning the LED off.
    HINT: Set LED to LOW.

We can now change our code to "Blocks and Text" so that we can familiarize ourselves with the manual types codes. This way, we can understand how to manually type codes in future.

This is the end result.



Here is the end product!



Challenge #1b: Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.

Firstly, lets us start by selecting all the components we need again.
Here are the components required:
Arduino Board, Breadboard, LED, LDR (Photoresistor), Wires and a Multimeter.

Secondly let us start building these components together. The steps are summarized:
  • Connection of Arduino Board to Breadboard
  • Connection of LED and LDR to Breadboard
  • Connection of Arduino Board to multimeter
This is the end result!


Thirdly, to allow our built Arduino Set-up to work, we will need to create our code. Again using TinkerCad blocks coding mechanism, lets start writing the codes.
  1. Under variable, let us create a new variable again called "SensorVal" which stands for Sensor Value.
  2. Drag out the block "Set SensorVal to ..." and include "read analog PinA0".
  3. Then, drag out the block "print to serial monitor without newline" and include "SensorVal".
  4. Lastly, use the block "Set built-in LED to high"
We can now change our code to "Blocks and Text" so that we can familiarize ourselves with the manual types codes. This way, we can understand how to manually type codes in future.

This is the end Result

Serial Monitor:

Here is the End Product!



Challenge #2a: Interface 3 LEDS (Red, Yellow, Green) to maker UNO board and program it to perform fade.

Firstly, let us start by selecting all the required components.
Here are the components required:
Arduino Board, Breadboard, 3 LEDs, Resistor and Wires

Secondly, let us build our Adruino Set-up. The steps are summarized:
  • Connection of Arduino Board to Breadboard
  • Connection of LEDs and Resistor to Breadboard
This is the end result!


Thirdly, its time to code for our Arduino Set-up again.
  1. Under variable, let us create our own variable called "Brightness"
  2. Drag the block "count up by..." under Control.
    Edit the block so that it says "Count up by 5 for brightness from 0 to 255 do"
  3. Drag 3 blocks of "Set pin # to ..." and include our variable brightness into it.
    The pin# depends on the pin we have attached our LED to our Arduino Board. For my case, it is the pin 9,10 and 11.
  4. Drag out the block "wait ... seconds" and change seconds to milliseconds and include "30"
  5. Duplicate the whole block and do the same process for brightness going down from 255 to 0.
We can now change our code to "Blocks and Text" so that we can familiarize ourselves with the manual types codes. This way, we can understand how to manually type codes in future.

This is the end result


Here is the End Product!



Challenge #2b: Interface the DC motor to maker UNO board and program it to on and off using push button on the board

Firstly, let us list down the components which will be required.
The components required are:
Arduino Board, Breadboard, DC motor, Pushbutton, Resistor and Wires.

Secondly, let us build our Arduino Setup using the components. The steps are summarized:
  • Connection of Arduino Board to Breadboard
  • Connection of DC motor to breadboard and Arduino Board
  • Connection of Pushbutton to breadboard
  • Connection of resistor to Pushbutton and Breadboard
This is the end result!


Thirdly, let us start programming our code with the Blocks.
  1. Under variable, lets create a new variable called "ButtonState".
  2. Drag the block "set ... to ..." and include read digital pin 6 and our new variable ButtonState.
    Digital pin# depends on which pin we have connected our Pushbutton to Arduino Board.
  3. Under control, drag out the If...Else... block.
  4. For IF add in the math conditions and include ButtonState and change to "=" and HIGH.
  5. For THEN, add in "set pin# to HIGH" and select 9.
    This pin# refers to the pin we have connected our DC motor to the Arduino Board.
    RECAP: HIGH refers to turning on the DC motor.
  6. For ELSE, add in "set pin# to LOW" and select 9.
    The pin# refers to the pin we have connected our DC motor to the Arduino Board.
    RECAP: LOW refers to turning off the DC motor.
  7. Therefore, the "If...Else..." button can be understood as: If pushbutton is pressed, DC motor will spin. If  pushbutton is not pressed, DC motor will NOT spin.
We can now change our code to "Blocks and Text" so that we can familiarize ourselves with the manual types codes. This way, we can understand how to manually type codes in future.

This is the end result!

This is the End Product!


Practical
Our practical requires my team to enable a flapping mechanism for our laser cut carboard Pegasus. However, our teacher has given my team an extra challenge, which is to include melody inside the codes and create a start and stop button. 

To enable the flapping of wings, we have to locate the part where it enable the wings to flap. It was found to be at the bottom of the Pegasus, where the 2 wings had intersected. Having one end of the wire connecting to the point of intersection, and another point connecting to the servo, this allows the wings to be flapped when servo is spinning. Now, all we have to do is to program the code for the servo!




This are our handwritten codes:

This is how our Pegasus works:


This is how we have programmed our Pegasus:



Having done this practical enables my team to expand our knowledge on programming. It was definitely a tedious and difficult practical, but doing it as a team definitely has helped us to understand how programming works. 

The sense of achievement after successfully accomplishing our practical and the challenge was very rewardable.♔


Reflection
In this practical, although it was personally extremely difficult, with the help of my team, I was able to understand the basics, and we were able to accomplish all our tasks successfully. 

My group were tasked to make a Pegasus with it's wings flapping with the help of a servo. However, our teacher has gave us an extra challenge which is to include melody, start and a stop button. My group was not very familiar with coding thus we had face troubles, such as having the melody played in the background while the wings of the Pegasus is flapping. After a few days of calling together and researching, my group was able to come up with a solution which is to increase the flapping speed, and have each note played after 1 flap of the wings. Having a fast flapping speed allows it to have the illusion of having the melody played continuously in the background. With this solution, we have successfully accomplish our challenge.

Individually, I had to accomplish 4 challenges using TinkerCad. Luckily, I had found TinkerCad much easier to use, coding wise. It has a block coding which really helped me to program my Arduino set-up much easier. After completing my Block codes, I had switched over to Text codes and allow myself to familiarize myself on how to manually type the code and to understand what each line of code refers to.

The group challenge and individual challenges allow me to understand the use of Arduino board and programming. As the quote says "Practice Makes Perfect", being exposed to many challenged with different components used and different codes used, I was able to experiment with the different components and codes, allowing myself to understand what is going on. 

Although I still feel that Programming is still confusing and difficult, I will definitely still research more on myself so that I am able to program anything I want in future, which can also help my team and I in our CP5070 project of making a CO system, and our Final Year Project.

Comments

Popular posts from this blog

HOME

Hello!! I am  Wayne  from DCHE/FT/2B/01.  I am the leader for my group F4!😁  My hobbies includes  Football and listening to K-POP.