Lesson From Tinkercad Blocks to C - Internet of Things - ثاني ثانوي

228 Link to digital (essan Lesson 2 From Tinkercad Blocks to C++ www.len.edusa Migrating from Visual Blocks Programming to C++ Programming In this lesson, we are going to learn how to move from programming an Arduino with Tinkercad blocks to programming it with C++. While Tinkercad blocks are useful for rapid prototyping, using native C++ is necessary for utilizing the full capabilities of the Arduino microcontroller. We are going to learn the basic functions and statements to start programming an Arduino microcontroller with C++. TIN KEN CAD G Figure 6,10 Tinkercad blocks to C++ code Tinkercad provides a simulation environment for Arduino modeling, which does not require a physical Arduino to be connected to a computer. Variable Assignments and Operations Command groups used: Oulpui Control Mall Notation Virmblies Declaring and changing variables in Tinkercad blocks is done through the Variables and Math command groups. The following table illustrates examples of the available commands. Declaring a variable named x. Tinkercad block Ch Input X Assigning a value to a variable. Tinke 5 - כקוץ to 3 CH int x = 0; x=3;

Lesson 2 From Tinkercad Blocks to C++

Changing a variable by a specific value. Tinkercad bincs change by 5 Performing a mathematical operation between variables x and y. Tinkercad block Sal y 4++ x += 5; C++ Assigning a third variable z to a mathematical operation between variables x and y. Take ad block sal 2▾ to Performing a mathematical comparison between variables x and y. Tinkercad bin Performing a logical comparison between variables x and y. Tinkercad block Performing a logical operation between two statements. Tinad blox P 2005-1945 z = x/y; G++ x < y 4++ x != y Ска x != y 8 x < y 229

Lesson 2 From Tinkercad Blocks to C++

230 Conditional Statements, Loops and Output Messages Building conditional statements, loops and output messages in Tinkercad blocks is done through the Control and Output command groups. The following table illustrates examples of the available commands. Print a message to the Serial Monitor. Command groups used: Outpul Inpul Notation Tikwad block C-- pup to serii monia hello world wit HEWline Wait for 5 seconds. Tinkercad block 5 Execute the blocks in the if codeblock if the logical condition is true. Tinkercard Shoe change y by 5 10 Execute the blocks in the if codeblock if the logical condition is true. Tinkercad blocks وزارة التعليم 2173-1465 Contri Math Vanables Serial.println("hello world"); Gitt delay(5000); C++ if (x < 10) { y = 5; } Ett if (x >= 10 86 x < 20) { y += 10; } else { y += 20;

Lesson 2 From Tinkercad Blocks to C++

Execute the blocks in the for codeblock if the logical condition is true. Tinkercad block 5 bu change by 1 } Repeat a while loop under the following condition. Tinkercad blocks whi change ** by 1 10 for (counter = 0; counter <5; counter) { y = 1; CH while (x <10) x += 1; 1 Arduino Digital and Analog Pin I/O Interacting with the Arduino board's digital and analog pins in Tinkercad blocks is done through the Input, Output, and Math command groups. Each time that a pin is used, whether it is analog or digital, Tinkercad blocks recognizes if it is used for digital or analog I/O. To use a pin, you need to specify in the setup() Arduino function whether it is used in INPUT or OUTPUT mode. For analog output, the pins 3, 5, 6, 9, 10, 11 are used with Pulse Width Modulation (PWM). The following table illustrates examples of the available commands. Command groups used: Outp Notation Getting the value of the digital pin 4 and storing it in the variable.x. Tinkercad block C+ read digital pin 4 ▾ pinMode(4, INPUT); x = digitalRead(4); وزارة التعليم 165 - כקו Contral Math Varoles 231

Lesson 2 From Tinkercad Blocks to C++

Setting the value of the digital pin 4 to HIGH. Tinkercad block sel pin 4 to HIGH C++ pinMode(4, OUTPUT); digitalWrite(4, HIGH); Getting the value of the analog pin A3 and storing it in the variable y. Tinkercad block sel y to read analog pin A3 pinMode(A3, INPUT); y = analogRead(A3); Setting the value of the pin 10 to the analog value 15 using PWM Tinkercad block set pin 10 to 15 Get pinMode(10, OUTPUT); analogWrite(10, 15); 232 Examples of Migration from Tinkercad Blocks to C++ We will create simple examples in Tinkercad to showcase the migration of programming the Arduino board with Tinkercad blocks, to programming it with C++. Blinking LEDs Example We will build a simple program that creates two loops that blink an LED light 5 and 10 times at different rhythms. Components needed: 1 Arduino Uno R3 •LED. Programming in C++ in Tinkercad can start by converting the editing type from Blocks to Text. Components that you will use in this project 00 Arduino Uno R3 LED Figure 8.1 Project components

Lesson 2 From Tinkercad Blocks to C++

Connecting the LED: > Connect the Cathode of the LED to the GND of the Arduino and change the wire color to black. > Connect the Anode of the LED to the Digital Pin 11 of the Arduino and change the wire color to green. Programming the Arduino The program will blink the LED light every 1 second 5 times, and then it will blink the LED light every 200 milliseconds 10 times. Tinkercad blocks 0 1 2 DIGITAL PWM- OO UNO ARDUINO Figuix 12 Connecting the LED وزارة التعليم 2127-1985 10 02 02 int counter; int counter2; void setup() f } pinMode(11, OUTPUT); void loop() { for (counter = 0; counter <5; ++counter) { digitalWrite(11, HIGH); delay(1000); // Wait for 1000 millisecond(s) digitalWrite(11, LOW); delay(1000); // Wait for 1000 millisecond(s) } for (counter2 = 0; counter2 <10; ++counter2) { digitalWrite(11, HIGH); delay(200); // Wait for 200 millisecond(s) digitalWrite(11, LOW); delay(200); // Wait for 200 millisecond(s) 233

Lesson 2 From Tinkercad Blocks to C++

234 PIR Alarm Example We will expand the previous project with a PIR alarm that will send a signal to light the LED 3. times in rapid succession. • Components needed: •1 Arduino Uno R3 .1 LED • 1 PIR Sensor Components that you will use in this project LED Creating the circuit: > Connect the Cathode of the LED to the GND of the Arduino and change the wire color to black. > Connect the Anode of the LED to the Digital Pin 11 of the Arduino and change the wire color to green. > Connect the Signal of the PIR Sensor to the Digital Pin 10 of the Arduino and change the wire color to orange. > Connect the Power of the PIR Sensor to the SV of the Arduino and change the wire color to red. > Connect the Ground of the PIR Sensor to the GND of the Arduino and change the wire color to black. وزارة التعليم Arduino Uno R3 PIR Sensors Figure 613 Project components 2 1 5 Figure & 14 Connecting the arcult DIGITAL IPWM-) OO UNO ARDUINO 4 POWER ANALOG IN

Lesson 2 From Tinkercad Blocks to C++

Tinkercad blocks 8 03 int counter; void setup() { } pinMode(10, INPUT); pinMode(11, OUTPUT); void loop() f if (digitalRead(10) == HIGH) { for (counter = 0; counter <5; ++counter) { digitalWrite(11, HIGH); delay(300); // Wait for 300 millisecond(s) digitalWrite(11, LOW); delay(300); // Wait for 300 millisecond(s) رارة التعليم J12-1985 Programming the Arduino The program will check if the PIR sensor has detected an object in its Field of View. If it has detected an object, it will send a signal to blink the LED light 5 times in rapid succession. Choose the programming mode of Text in the code editor to see the produced C++ text. 238

Lesson 2 From Tinkercad Blocks to C++

DC Motor Example We will build a simple circuit to control a DC motor depending on the temperature of the environment. You will need the following components: • 1 Arduino Uno R3 • 1 DC Motor ⚫ 1 Temperature Sensor (TMP36) Creating the circuit: > Connect the Power pin of the Temperature Sensor to the 5V of the Arduino and change the wire color to red. > Connect the Vout pin of the Temperature Sensor to the Analog pin A0 of the Arduino and change the wire color to green. > Connect the GND pin of the Temperature Sensor to the GND of the Arduino and change the wire color to black. > Connect Terminal 1 of the DC motor to the GND of the Arduino and change the wire color to black > Connect Terminal 2 of the Servo motor to the Digital pin 11 of the Arduino and change the wire color to red. Components that you will use in this project perture Arduino Uno Rensor DC Motor Figure 515 Project components TMP وزارة التعليم 236 IMP: 1 3 2 5 4 Figure & 10. Connecting the arcult DIGITAL PRM- - UNO AIDUINO

Lesson 2 From Tinkercad Blocks to C++

Tinkercad blocks Pu 123-1985 2 templare 27 Programming the Arduino The program will create a variable named temperature which will be connected to the Analog pin A0 of the Arduino and record the temperature of the environment. When the temperature variable reaches the value 27 (degrees Celsius) in the Tinkercad simulation, it will activate the DC motor for 2 seconds. Choose the programming mode of Text in the code editor to see the produced C++ taxt, int temperature = 0; void setup() { } pinMode(AB, INPUT); Serial.begin(9600); pinMode(11, OUTPUT); void loop() { temperature = analogRead(A0); Serial.println(temperature); if (temperature >= 27) ( digitalWrite(11, HIGH); The Serial object is used for printing in the Serial Monitor. In the setup() function, the begin() function Initializes the Serial Monitor so it can be used later. Then, the user can print values and messages to the Serial Monitor with the function print() or printin(), with the latter also adding a newline at the end. delay(2000); // Wait for 2000 millisecond(s) digitalWrite(11, LOW); 257

Lesson 2 From Tinkercad Blocks to C++

230 Exercises 1 Write a C++ function that takes two float data type arguments, an analog signal, and a multiplier. The function multiplies the signal and returns it. 2 Write a C++ sketch that reads an analog signal input from a pin that is a temperature reading in Fahrenheit. Create a function that converts this value to degrees Celsius and sends it to a pin as an analog output. جبلية الكلية

Lesson 2 From Tinkercad Blocks to C++

3 Find the syntax error and the logical error in the following code snippets: void loop() { for (counter = 0; counter <5; --counter) { digitalWrite(11, HIGH); // Wait for 1000 millisecond(s) delay("1000"); digitalWrite(11, LOW); //Wait for 1000 millisecond(s) delay("1000"); } Syntax Error: void loop() { Lemperature digitalRead(A0); Serial.println(temperature); if (temperature >= 278) { digitalWritell, 1); //Wait for 2000 millisecond(s) delay(2000); digitalWrite(11, 0); } Syntax Error وزارة التعليم Logical Errors Logical Error: 239

Lesson 2 From Tinkercad Blocks to C++

260 4 Write a C++ sketch for the Arduino that uses the function from exercise 1 and reads an analog signal input. It then creates a for loop that uses the function from exercise 1 to amplify the original signal 5 times. Each time the signal is amplified, it is sent to a pin as an analog output. 5 Extend the Blinking LEDs example to accommodate another LED light of a different color that blinks every time the first LED light is off. وزارة التعليم

Lesson 2 From Tinkercad Blocks to C++

6 Extend the PIR alarm example to accommodate another PIR alarm and another LED light of another color Each PIR alarm will be bound to an LED light which will blink depending on whether the PIR alarm detected an object. 7 Adjust the DC motor example to send an analog signal to the DC motor depending on the temperature that is detected by the TMP sensor. وزارة التعليم 241

Lesson 2 From Tinkercad Blocks to C++