Top Banner
71

ARDUINO TRAINING - Day 1.ppt

Dec 01, 2015

Download

Documents

Cassio Paz
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ARDUINO TRAINING - Day 1.ppt
Page 2: ARDUINO TRAINING - Day 1.ppt

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
Page 4: ARDUINO TRAINING - Day 1.ppt

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

Computer Program – is known as software program. It is a structured collection of instructions to the computer.

Page 6: ARDUINO TRAINING - Day 1.ppt

Computer Programmer – is a professional who writes programs.

Page 7: ARDUINO TRAINING - Day 1.ppt

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
Page 9: ARDUINO TRAINING - Day 1.ppt

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

A part of the environment where we can write the codes

Page 11: ARDUINO TRAINING - Day 1.ppt

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

It is a computer program that is used to test and debug other programs

Page 13: ARDUINO TRAINING - Day 1.ppt
Page 14: ARDUINO TRAINING - Day 1.ppt

Preprocessor directive main(){VariablesStatements

}

Head

Body

Page 15: ARDUINO TRAINING - Day 1.ppt

Preprocessor CommandsFunctionsVariablesStatements & ExpressionsComments

Page 16: ARDUINO TRAINING - Day 1.ppt

These commands tells the compiler to do preprocessing before doing actual compilation.

Page 17: ARDUINO TRAINING - Day 1.ppt

#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

are main building blocks of any C Program

Page 19: ARDUINO TRAINING - Day 1.ppt

#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

are used to hold numbers, strings and complex data for manipulation

Page 21: ARDUINO TRAINING - Day 1.ppt

#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

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

#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

Doing it the right way...

Page 25: ARDUINO TRAINING - Day 1.ppt

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

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

Declaration and Data Types

Page 28: ARDUINO TRAINING - Day 1.ppt

Naming VariablesDeclaring VariablesUsing VariablesThe Assignment Statement

Page 29: ARDUINO TRAINING - Day 1.ppt

Variables – use represent some unknown value

Example : Perimeter = 2L + 2W;

Page 30: ARDUINO TRAINING - Day 1.ppt

Variables in programming can be represented containing multiple charactersExample: full_name = first_name + last_name;

Page 31: ARDUINO TRAINING - Day 1.ppt

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

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

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

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

C is case sensitive◦Example:

areaAreaAREAArEa

All are seen as different variables by the compiler.

Page 36: ARDUINO TRAINING - Day 1.ppt

Syntax : <data_type> variable_name;

Example:int num_of_meatballs;

float area;

Page 37: ARDUINO TRAINING - Day 1.ppt

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

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

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
Page 41: ARDUINO TRAINING - Day 1.ppt

Examples:

int length = 7 ;float diameter = 5.9 ;char initial = ‘A’ ;double cash = 1058635.5285

Page 42: ARDUINO TRAINING - Day 1.ppt

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

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

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

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

Use spaces around all arithmetic and assignment operators.◦Examples:feet = 6 * fathoms ;a = b + c;

Page 47: ARDUINO TRAINING - Day 1.ppt

#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
Page 49: ARDUINO TRAINING - Day 1.ppt

Assignment statement is represented by an equal sign (=)

Examples:diameter = 5.9 ;area = length * width ;

Page 50: ARDUINO TRAINING - Day 1.ppt

#include <stdio.h> int main( ) { int inches, feet, fathoms ;

fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ;

.

.

.

Page 51: ARDUINO TRAINING - Day 1.ppt
Page 52: ARDUINO TRAINING - Day 1.ppt

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

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

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

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

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

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

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

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

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

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
Page 63: ARDUINO TRAINING - Day 1.ppt

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

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

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

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

Write a program that output all even numbers between 1 to 15.

Page 68: ARDUINO TRAINING - Day 1.ppt

Write a program that reads in 10 positive integers and then calculate its sum.

Page 69: ARDUINO TRAINING - Day 1.ppt

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

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

Modify the sample problem so that it will ask the user for the number to multiply and the number of terms.