Term 2 (Arduino Circuit)
Activity 4 (Digital pins with code)
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000); //Millisecond
digitalWrite(13, LOW);
delay(1000);
digitalWrite(12, HIGH);
delay(1000); //Millisecond
digitalWrite(12, LOW);
delay(1000);
digitalWrite(7, HIGH);
delay(1000); //Millisecond
digitalWrite(7, LOW);
delay(1000);
}
Breadboard
Glossary
- A Breadboard is simply a board for prototyping or building
circuits on.
Breadboard
- two major types of breadboards; solder and solderless
boards.
A metal alloy used to create strong permanent bonds; such
Solder
as copper joining in circuit boards and copper pipe joints.
What is a breadboard?
• A breadboard is a rectangular plastic board with a bunch of tiny holes in it.
• These holes let you easily insert electronic components
to prototype (meaning to build and test an early version of) an electronic
circuit, like this one with a battery, switch, resistor, and an LED (light-emitting
diode).
Breadboard Activity (No code)
Activity 5
// C++ code
//
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop()
{ // Red LED
digitalWrite(LED_BUILTIN, HIGH); // LED ON
delay(100); // Wait for 100 millisecond(s)
digitalWrite(LED_BUILTIN, LOW); // LED OFF
delay(100); // Wait for 100 millisecond(s)
// Yellow LED
digitalWrite(12, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(12, LOW);
delay(500); // Wait for 500 millisecond(s)
// Green LED
digitalWrite(11, HIGH);
delay(100); // Wait for 100 millisecond(s)
digitalWrite(11, LOW);
delay(100); // Wait for 100 millisecond(s)
// Blue LED
digitalWrite(7, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(7, LOW);
delay(1000); // Wait for 1000 millisecond(s)
// Blue LED
digitalWrite(6, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6, LOW);
delay(1000); // Wait for 1000 millisecond(s)
// Blue LED
digitalWrite(5, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(5, LOW);
delay(500); // Wait for 500 millisecond(s)
}
Activity 6A (Push Button and Slide Switch) – No code
Activity 6B (Push Button and Slide Switch) with digital pins (with
code)
// C++ code
//
void setup()
{
pinMode(10, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{ //red LED
digitalWrite(10, HIGH); // LED On
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(10, LOW); // LED Off
delay(1000); // Wait for 1000 millisecond(s)
// yellow LED
digitalWrite(4, HIGH); // LED On
}
Variable
What is a variable in computer programming?
A variable in programming is a named storage location that holds data or a
value. Variables are essential for storing and manipulating data in programs.
C++ data types examples
integer: int age = 25; (Whole number)
float: float temp = 36.6; (Decimal number)
character: char grade = 'A'; (Single letter or symbol)
String: string greeting = "Hello"; (Word, phrase, sentence,)
boolean: bool isReady = true; (True or False)
How to declare variable in Arduino?
To declare a variable in Arduino, you can use the following format:
• Variable_Datatype: The type of variable, such as int or float
• Variable_Name: The name of the variable
• Semicolon: A semicolon (;) ends the variable declaration
Activity 7 (Variable)
// C++ code
//
int redledpin = 10;
int blueledpin = 8;
int yellowledpin =6;
int whiteledpin = 2;
void setup()
{
pinMode(redledpin, OUTPUT);
pinMode(blueledpin, OUTPUT);
pinMode(yellowledpin, OUTPUT);
pinMode(whiteledpin, OUTPUT);
}
void loop()
{
digitalWrite(redledpin, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(redledpin, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(blueledpin, HIGH); //blue LED ON
digitalWrite(yellowledpin, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(yellowledpin, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(whiteledpin, HIGH); //white LED ON
}
"Serial Communication"
What is Serial.begin(9600)?
https://www.youtube.com/watch?v=8GX5brSZ_1E&t=5s
Serial.begin() - Sets the data rate in bits per second (baud) for serial data transmission.
Serial.print() - Prints data to the serial port as human-readable ASCII text.
Serial.println() - Prints data to the serial port as human-readable ASCII text and a newline.
Serial.print()
• Serial.print(78) gives "78"
• Serial.print(1.23456) gives "1.23"
• Serial.print('N') gives "N"
• Serial.print("Hello world.") gives "Hello world."
• Serial.print("\t"); // prints a tab
• Serial.print("\n"); // prints a new line
• Serial.println(); // carriage return after the last label
• Serial.println(78, BIN); gives "1001110"
Program 1 (Code)
// C++ code
//
void setup()
{
Serial.begin(9600);
Serial.print("Hello World!");
}
void loop()
{
}
Program 2 (Code)
// C++ code
//
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hello World!");
delay (500);
}
Program 3 (code)
// C++ code
//
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hello World!");
delay (500);
Serial.println(5);
delay(200);
}
Program Code (Sample)
// C++ code
//
void setup()
{
Serial.begin(9600);
Serial.println(78,BIN);
Serial.println(78,OCT);
Serial.println(78,HEX);
Serial.println(1.2345,0);
Serial.println(1.2345,2);
}
void loop()
{
}
Activity 8B (Calculation)
Program 1 (Code)
// C++ code
//
void setup()
{
Serial.begin(9600);
int Num1 = 3;
int Num2 = 6;
int Total = Num1 + Num2;
Serial.print(Total);
}
void loop()
{
}
Program 2 (Code)
// C++ code
//
void setup()
{
Serial.begin(9600);
int Length = 100;
int Width = 200;
int Area = Length * Width;
Serial.print("Area is ");
Serial.print(Area);
}
void loop()
{
}
Data types
int → integer → whole number eg: 1, 10, 23
float → decimal → eg: 1.23, 4.56
char → single character → eg; A, G, %, #…
string → sequence of letters → eg: Hello!, Name, How are you?,…
Boolean → eg: True or False
Arithmetic Operator
Addition → +
Subtraction → -
Multiplication → * (asterisk)
Division → /
Activity 9A (Calculation)
// C++ code
//
void setup()
{
int num1 = 5;
int num2 = 10;
int addition = num1 + num2;
int subtract = num1 - num2;
int multiplication = num1*num2;
int division = num1/num2;
Serial.begin(9600);
Serial.print("The addition of 2 numbers = ");
Serial.println(addition);
Serial.print("The subtraction of 2 numbers = ");
Serial.println(subtract);
Serial.print("The multiplication of 2 numbers = ");
Serial.println(multiplication);
Serial.print("The division of 2 numbers = ");
Serial.println(division);
}
void loop()
{}
Activity 9B (Float Variable)
Datatype → float - a number that has a decimal point
Calculate the area of Circle - Circle area formula: A = πr²
// C++ code
//
void setup()
{
Serial.begin(9600);
float pi = 3.14;
float radius = 5.2;
float CircleArea = pi * radius * radius;
Serial.print("Circle Area = ");
Serial.print (CircleArea);
}
void loop()
{
}
Activity 9C (Float Variable)
Calculate the area of Triangle
Triangle: The area of a triangle is calculated using the formula A=1/2×b×h
// C++ code
//
void setup()
{
Serial.begin(9600);
float b = 5.2;
float h = 3.1;
float TriangleArea = 0.5 * b * h;
Serial.print("Triangle Area = ");
Serial.print (TriangleArea);
}
void loop()
{ }