0% found this document useful (0 votes)
47 views

C Programming Language

This document provides an overview of C programming for Arduino. It discusses C program structure using functions like setup() and loop(), basic data types, math and logic operations, digital and analog input/output, control structures like if/else and for loops, and serial communication. The goal is to teach the essential elements of C needed to program Arduino boards and microcontrollers.

Uploaded by

mohamed ramadan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

C Programming Language

This document provides an overview of C programming for Arduino. It discusses C program structure using functions like setup() and loop(), basic data types, math and logic operations, digital and analog input/output, control structures like if/else and for loops, and serial communication. The goal is to teach the essential elements of C needed to program Arduino boards and microcontrollers.

Uploaded by

mohamed ramadan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

C Programming

Language
for Arduino
Arduino Programming

Programmer

Assembly C++
(Readable Code) (Readable Code)
Assembler compiler

C++
Machine Language Library
(Binary Code)
C Programming Language
for Arduino

Program Math Logic Input/ Bit Program


Structure Instruction Instruction Output Instruction Control
& Basics Logic Gate Instruction Instruction
Variable +-*/ Shift
Definition Functions Compare
Program Structure
put your Variable and
Constant
void setup() {

Put your program

; at end of Command
// at start of Comment
}
Program Structure
put your Variable and
Constant
void setup() {
put your setup code
here, to run once

Again:
put your main code
here, to run repeatedly
goto Again
}
Program Structure
put your Variable and
Constant
void setup() {
put your setup code
here, to run once
}
void loop() {
put your main code
here, to run repeatedly

}
Program Structure
Example
void setup() {
pinMode (12,output);
}

void loop() {
digitalWrite (12, HIGH);
Delay(1000);
digitalWrite (12, LOW);
Delay(1000);
}
Constants
HIGH | LOW // 1,0
INPUT | OUTPUT | INPUT_PULLUP // used to configure input
output pin
LED_BUILTIN // Led conected to Pin 13
true | false // 1,0
Floating Point Constants //Floating constant No. 0.000365
Integer Constants //Integer constant No. 0-255
Data Types
array //table[]={1,3,6,4,3,0,9,7,5} table[6]=9
bool
boolean
byte
char // char myChar = 'A'; char myChar = 65;
unsigned char // from 0 to 255
double
float // floating No. -3.4028235E+38 to 3.4028235E+38 32bit
int // integer data - 32768 to 32767 16bit
long // integer data -2,147,483,648 to 2,147,483,647 32bit
unsigned int //0….65535 16bit
unsigned long //0…. 4,294,967,295 32bit
short
size_t // if t={ 3,9 ,6,2} size t=4
string //
String() //

void
word
Data Type
Integer x=230 +230

unsigned int x=230 230

Float x=230.25 2.30 E+2

Char y=(‘A’) 65 ASCII of A

Int Array[]={5,3,8,2} 5

Memory
Math
Arithmetic Operators
% (remainder) Trigonometry
* (multiplication) cos()
+ (addition) sin()
- (subtraction) tan()
/ (division)
= (assignment operator)

abs()
map()
y = map(x, 1, 100, 1000, 1); // convert x (from 1 to 100) to y (from 1000 to 1)
max()
sensVal = max(sensVal, 20); // assigns sensVal to the larger of sensVal or 20
min()
sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100
constrain()
sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150

pow()
z = pow(x, y); //z=xy
sq()
z = sq(x); //z=x2
sqrt()
z = sqrt (x); //z=√x
Compound Operators
%= (compound remainder)
&= (compound bitwise and)
*= (compound multiplication)
++ (increment)
+= (compound addition)
-- (decrement)
-= (compound subtraction)
/= (compound division)
^= (compound bitwise xor)
|= (compound bitwise or)

A-=5 // A=A-5
A%=5 // A=A%5
Logic
Comparison Operators
!= (not equal to)
< (less than) Condition
<= (less than or equal to) ! (logical not)
== (equal to) && (logical and)
> (greater than) || (logical or)
>= (greater than or equal to)

Logic Gate
& (bitwise and)
^ (bitwise xor)
| (bitwise or)
~ (bitwise not)

Shift
<< (bitshift left)
>> (bitshift right)
Digital Input / Output
digitalWrite (12, HIGH); // HIGH is the voltage level

val = digitalRead(7); // read the input pin 7

Analog Input / Output

val = analogRead(analogPin); // read the input pin

analogWrite(13,1023); // write 1023 on Pin


Digital Output
digitalWrite (13, HIGH);

delay(500)

digitalWrite (13, LOW);

delay(500)
Digital Output
digitalWrite (13, HIGH);

delay(500)

digitalWrite (13, LOW);

delay(500)
Digital Input
val = digitalRead(7);
if val==0 {

digitalWrite (13, HIGH);

delay(500)

digitalWrite (13, LOW);

delay(500)

}
Analog Input
From Potentiometer/Sensors
Example
void setup() {
}

void loop() {
val = analogRead(A0)
5v to A0 val=1023
} 0v to A0 val=0
PWM Example
void setup() {
pinMode (13,output);
}

void loop() {
analogWrite(13,0); // write 0 on Pin 13
delay(100)
analogWrite(13,0); // write 1 on Pin 13
delay(100)

analogWrite(13,1023); // write 1023 on Pin 13


delay(100)
}
Pin 13 on Oscilloscope
Bits and Bytes
bit()
bit(n) //z=2n

bitClear()
bitClear(x, n) //Put zero in bit no. n of variable x

bitRead()
bitRead(x, n) //Read bit no. n of variable x

bitSet()
bitSet(x, n) //Put one in bit no. n of variable x

bitWrite()
bitWrite(x, n, b) //Put (0 or 1) in bit no. n of variable x

highByte()
highByte(x ) //Only the high Byte of x

lowByte()
lowByte(x) //Only the low Byte of x
Control Instruction
break
continue
do...while
else
for
goto
if
return
switch...case
while
delay
Delay
delay(1000); // waits for 1000 miliseconds
delayMicroseconds(50); // pauses for 50 microseconds
for
for (int x = initial ; condition ; step)
{

Example Code
In the following code, for loop to output values on PWM pin

for (int x = 0; x < 255; x++) {

analogWrite(PWMpin, x);

delay(50);
}
Break
Example Code
In the following code, the control exits the for loop when the sensor value
exceeds the threshold.

int threshold = 40;


for (int x = 0; x < 255; x++) {
analogWrite(PWMpin, x);
sens = analogRead(sensorPin);
if (sens > threshold)
{ // bail out on sensor detect
x = 0; break;
}
delay(50);
}
Do …While
do { // statement block } while (condition);

Example Code

int x = 0;
do {
delay(50); // wait for sensors to stabilize
x = readSensors(); // check the sensors
}
while (x < 100);
IF
if (condition) {
//statement(s)
}

Example Code

if (x > 120) {

digitalWrite(12, HIGH);

}
IF…..ELSE
if (condition1) {
// do Thing A
}
else if (condition2) {
// do Thing B
} else {
// do Thing C
}

Example Code
Below is an extract code for temperature sensor system

if (temperature >= 70) {


digitalWrite(11, HIGH);
}

else if (temperature >= 60) {


digitalWrite(12, HIGH);
}

else {
digitalWrite(13, HIGH);
}
Switch… Case
switch (var) {
case label1: // statements
break;
case label2: // statements
break;
default: // statements break;
}

switch (x) {
case 1: //do something when var equals 1
digitalWrite(12, HIGH);
break;

case 2: //do something when var equals 2


digitalWrite(13, HIGH);
break;

default: // if nothing else matches, do the default


digitalWrite(11, HIGH);
break;
}
goto
if (analogRead(0) > 250) {
goto bailout;

// more statements ...


}

// more statements ...

bailout:
// more statements
Serial Communication
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

to receive
if (Serial.available() > 0) { // if the incoming byte available:
Moh = Serial.read(); // say what you got:

To send
Serial.print(‚Hello world "); // send " Hello world "to serial port
Example
int a = 5;
int b = 10;
int c = 20;
void setup() {

Serial.begin(9600);
Serial.print("a + b = "); // add
Serial.println(a + b);
Serial.print("a * c = "); // multiply
Serial.println(a * c);
Serial.print("c / b = "); // divide
Serial.println(c / b);
Serial.print(‚c- b = "); // Subtract
Serial.println(c-b);

void loop() {
}
Serial Communication
Example Code
int a= 0; // for incoming serial data

void setup() {
Serial.begin(9600); // sets data rate to 9600 bps
}

void loop() { // is serial port available


if (Serial.available() > 0) { // read the incoming byte:
a= Serial.read(); // say what you got:
Serial.print("I received: ‚a);
}

}
For more details Press

Here

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy