arduino training - day 1.ppt
TRANSCRIPT
![Page 1: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/1.jpg)
![Page 2: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/2.jpg)
Fundamentals of C-Programming Introduction to C Programming Integrated Development Environment
(IDE) Structure of C Programming Elements of Style Variables in C Control Structure Loop Structure
![Page 3: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/3.jpg)
![Page 4: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/4.jpg)
Computer Programming -- is often called coding. It is the process of writing, testing, debugging and maintaining the source code of computer programs.
![Page 5: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/5.jpg)
Computer Program – is known as software program. It is a structured collection of instructions to the computer.
![Page 6: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/6.jpg)
Computer Programmer – is a professional who writes programs.
![Page 7: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/7.jpg)
It is a software application that provides comprehensive facilities to computer programmers for software development.
An IDE normally consists of a source code editor, a compiler and/or interpreter, build automation tools, and usually a debugger.
![Page 8: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/8.jpg)
![Page 9: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/9.jpg)
This window contains the interface 'Projects' which will in the following text be referred to as the project view.
This view show all the projects opened in CodeBlocks at a certain time.
The 'Symbols' tab of the Management window shows symbols and variables.
![Page 10: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/10.jpg)
A part of the environment where we can write the codes
![Page 11: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/11.jpg)
It is a computer program that translates text written in a computer language (the source language) into another computer language (the target language)
![Page 12: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/12.jpg)
It is a computer program that is used to test and debug other programs
![Page 13: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/13.jpg)
![Page 14: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/14.jpg)
Preprocessor directive main(){VariablesStatements
}
Head
Body
![Page 15: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/15.jpg)
Preprocessor CommandsFunctionsVariablesStatements & ExpressionsComments
![Page 16: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/16.jpg)
These commands tells the compiler to do preprocessing before doing actual compilation.
![Page 17: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/17.jpg)
#include <stdio.h>int main(){
double C, F;/*read in value for C*/printf(“Enter Celsius temp: “);scanf(“%lf”,&C);
/* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;}
![Page 18: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/18.jpg)
are main building blocks of any C Program
![Page 19: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/19.jpg)
#include <stdio.h>#include <stdio.h>int main()int main(){ {
double C, F;double C, F;/*read in value for C*//*read in value for C*/printf(“Enter Celsius temp: “);printf(“Enter Celsius temp: “);scanf(“%lf”,&C);scanf(“%lf”,&C);
/* Calculate F */ /* Calculate F */ F = (9/5) * C + 32;F = (9/5) * C + 32; /* Display the value of F */ /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;return 0;}}
![Page 20: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/20.jpg)
are used to hold numbers, strings and complex data for manipulation
![Page 21: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/21.jpg)
#include <stdio.h>int main(){
double C, F;/*read in value for C*/printf(“Enter Celsius temp: “);scanf(“%lf”,&C);
/* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;}
![Page 22: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/22.jpg)
Expressions combine variables and constants to create new values.
Statements are expressions, assignments, function calls, or control flow statements which make up C programs.
![Page 23: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/23.jpg)
#include <stdio.h>int main(){
double C, F;/*read in value for C*/printf(“Enter Celsius temp: “);scanf(“%lf”,&C);
/* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;}
![Page 24: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/24.jpg)
Doing it the right way...
![Page 25: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/25.jpg)
Write one statement per line. Put spaces before and after each arithmetic operator,
just like you put spaces between words when you write.
/* Poor programming practice */
biggest=-l;first=0;count=57;init_key_words();
If (debug)open_log_files();table_size=parse_size+lexsize;
/* Better programming practice*/
biggest=-l;
first=0;
count=57;
init_key_words();
if(debug)
open_log_files();
table_size=parse_size + lex_size;
![Page 26: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/26.jpg)
Change a long, complex statement into several smaller, simpler statements.
/* This is a big mess */gain = (old_value - new_value) /(total_old - total_new) * 100.0;
/* Good practice */delta_value = (old_value - new_value);delta_total = (total_old - total_new);gain = delta_value / delta_total * 100.0;
![Page 27: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/27.jpg)
Declaration and Data Types
![Page 28: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/28.jpg)
Naming VariablesDeclaring VariablesUsing VariablesThe Assignment Statement
![Page 29: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/29.jpg)
Variables – use represent some unknown value
Example : Perimeter = 2L + 2W;
![Page 30: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/30.jpg)
Variables in programming can be represented containing multiple charactersExample: full_name = first_name + last_name;
![Page 31: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/31.jpg)
Variable names in C◦May only consist of letters, digits, and underscores
◦May be as long as you like, but only the first 31 characters are significant
◦May not begin with a number◦May not be a C reserved word (keyword)
![Page 32: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/32.jpg)
auto break case char const continue default do double else enum extern float for goto if
int longregister returnshort signedsizeof staticstruct switchtypedef unionunsigned voidvolatile while
![Page 33: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/33.jpg)
Begin variable names with lowercase letters Use meaningful identifiers Separate “words” within identifiers with
underscores or mixed upper and lower case. ◦ Examples: surfaceArea surface_Area
surface_area Be consistent!
![Page 34: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/34.jpg)
Use all uppercase for symbolic constants (used in #define preprocessor directives).
Examples:
#define PI 3.14159 #define AGE 52
![Page 35: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/35.jpg)
C is case sensitive◦Example:
areaAreaAREAArEa
All are seen as different variables by the compiler.
![Page 36: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/36.jpg)
Syntax : <data_type> variable_name;
Example:int num_of_meatballs;
float area;
![Page 37: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/37.jpg)
Integers (whole numbers)◦int, long int, short int, unsigned int
Floating point (real numbers)◦float, double
Characters◦char
![Page 38: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/38.jpg)
Data Type
Number of Bytes
Minimum Value Maximum Value
char 1 -128 127
int 4 -2147483648 2147483647
float 4 1.17549435e-38 3.40282347e+38
double 8 2.225073858072014e-308
1.7976931348623157e+308
![Page 39: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/39.jpg)
Format Description Example
%c Single character A
%i or %d Integer or decimal Number
1024
%f Floating point number or real
3.145
%lf Double Long range of floating value
%.2f Floating point number with 2 decimal places
3.10
![Page 40: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/40.jpg)
![Page 41: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/41.jpg)
Examples:
int length = 7 ;float diameter = 5.9 ;char initial = ‘A’ ;double cash = 1058635.5285
![Page 42: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/42.jpg)
Good Programming Practice◦a comment is always a good idea◦Example:
int height = 4; // rectangle height
int width = 6 ; // rectangle width
int area ; // rectangle area
![Page 43: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/43.jpg)
Place each variable declaration on its own line with a descriptive comment.
Example : float inches ; // number of inches deep
![Page 44: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/44.jpg)
Place a comment before each logical “chunk” of code describing what it does.
Example :/* Get the depth in fathoms from the user*/printf(“Enter the depth in fathoms: ”);
![Page 45: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/45.jpg)
Do not place a comment on the same line as code (with the exception of variable declarations).
Use blank lines to enhance readability.
![Page 46: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/46.jpg)
Use spaces around all arithmetic and assignment operators.◦Examples:feet = 6 * fathoms ;a = b + c;
![Page 47: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/47.jpg)
#include <stdio.h>int main( ){ float inches ; // number of inches deep float feet ; // number of feet deep float fathoms ; // number of fathoms deep
// Get the depth in fathoms from the user
printf (“Enter the depth in fathoms : ”) ; scanf (“%f”, &fathoms) ;
// Convert the depth to inches
feet = 6 * fathoms ; inches = 12 * feet ;
.
.
.return 0;}
![Page 48: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/48.jpg)
![Page 49: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/49.jpg)
Assignment statement is represented by an equal sign (=)
Examples:diameter = 5.9 ;area = length * width ;
![Page 50: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/50.jpg)
#include <stdio.h> int main( ) { int inches, feet, fathoms ;
fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ;
.
.
.
![Page 51: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/51.jpg)
![Page 52: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/52.jpg)
It is a statement for choosing two alternative action
Basic Syntax:
if (conditional expr)statement_1;
elsestatement_2;
Conditional Expression Execute S_1
Execute S_2
T
F
![Page 53: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/53.jpg)
Write a program that reads in an integer and then display whether the number is positive or negative.
![Page 54: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/54.jpg)
Write a program that reads weight in pounds and output the weight in kilograms. Moreover, if the weight in kilogram exceeds 60 kilos, then display the message “GO ON DIET”, else “YOU ARE IN GOOD SHAPE”
![Page 55: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/55.jpg)
Used to check the value of a certain item that may have more than one outcome
Basic Syntax:
if (conditional expr)statement_1;
else if (conditional expr)statement_2;
else if (conditional expr)statement_3;
…else if (conditional expr)
statement_n;
![Page 56: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/56.jpg)
Write a program that reads in an integer and then display whether the number is positive or negative or neither when the user enters 0.
![Page 57: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/57.jpg)
Write a program that reads the student’s average grade in Programming and then output his grade standing according to the following schemeAVERAGE GRADE GRADE STANDING
Below 75 FAt least 75 but below 85 CAt least 85 but below 95 BAt least 95 A
![Page 58: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/58.jpg)
Write a program to display a menu system for an elementary arithmetic operation and then ask the user to select the operation he or she wants
Elementary Arithmetic Operations1. Addition2. Subtraction3. Multiplication4. DivisionInput your choice (1-4):The user then asked to type two integers and the correct
answer. Your program should also inform the user whether the answer is correct or not.
![Page 59: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/59.jpg)
Allows to execute a series of statement based on the value of an expression
Basic Syntax:
switch (expression) { case constant 1;
statement 1;statement 2;…statement n;break;
case constant 2;statement 1;statement 2;…statement n;break;
case constant n;statement 1;statement 2;…statement n;break;
default:statement;
}
![Page 60: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/60.jpg)
Create a Number Base converter program that allows the user to select from the four options givenNUMBER BASE CONVERTER
1. Decimal to Hexadecimal2. Hexadecimal to Decimal3. Decimal to Octal4. Octal to Decimal
Input your choice (1-4):
![Page 61: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/61.jpg)
Write a program that reads the student’s average grade in Programming and then output his grade standing according to the following schemeAVERAGE GRADE GRADE STANDING
Below 75 FAt least 75 but below 85 CAt least 85 but below 95 BAt least 95 A
![Page 62: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/62.jpg)
![Page 63: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/63.jpg)
It does not use a counter variable to loop a number of times
The general from of while
initialize loop counter;while (test loop counter using a condition){
statement_1;statement_2;increment loop counter;
}
![Page 64: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/64.jpg)
Write a program that reads in an integer and then calculate the sum of the positive integers from 1 to N.
![Page 65: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/65.jpg)
Write a program that reads in an integer and then count the number of positive and negative numbers
![Page 66: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/66.jpg)
The conditional expression is placed at the end of the loop
The general from of do…while
do { statement_1; statement_2; … statement_2;
} while (conditional expression);
![Page 67: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/67.jpg)
Write a program that output all even numbers between 1 to 15.
![Page 68: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/68.jpg)
Write a program that reads in 10 positive integers and then calculate its sum.
![Page 69: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/69.jpg)
It lets you execute a group of statements a certain number of times
The general from of for loop
for( initialization; test counter; increment counter)
{ statement_1; statement_2;… statement_n;
}
![Page 70: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/70.jpg)
Create a program that displays a multiplication table of 5.
Sample OutputSample Output5*1=55*2=105*3=15…5*10=50
![Page 71: ARDUINO TRAINING - Day 1.ppt](https://reader035.vdocuments.us/reader035/viewer/2022062300/55cf9d15550346d033ac2bd2/html5/thumbnails/71.jpg)
Modify the sample problem so that it will ask the user for the number to multiply and the number of terms.