You are on page 1of 15

C++ IN ARDUINO

WHAT IS C++?

• C++ was developed by Bjarne Stroustrup at Bell Laboratories in 1979


• C++ is a High Level Programming Language
• It is an attempt to add object-oriented features
USES OF C++

• System Software
• Game Development
• Embedded Systems
• Scientific computing
• High performance applications
VARIABLES IN C++

• Are containers for storing data values.


• Ex.
int x
float speed
double velocity
RULES IN NAMING A VARIABLE

• Name your variables based on terms of the subject area.


• Create variable names by deleting spaces that separate the words.
• Do not begin variable names with an underscore.
• Do not use variable names that consist of a single character.
• Name variables that describe binary states (true or false)
DATA TYPES IN C++

• Integer
• Boolean
• Character
• Float
• String
• Byte
• Double
• Long
INPUT AND OUTPUT
• pinMode()
- configures the specified pin to behave either as an input or an output.
• Syntax:
pinMode(pin,mode)
Ex:
void setup() {
pinMode(inPin, INPUT);
}
void loop () {
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
INPUT AND OUTPUT

• digitalRead()
- reads the value from a specified digital pin, either HIGH or LOW.
• Syntax:
digitalRead(pin)
Ex:
int inPin = 7;
void setup() {
pinMode(inPin, INPUT);
}
INPUT AND OUTPUT
• digitalWrite()
- write a HIGH or LOW value of digital pin.
• Syntax:
digitalWrite(pin, value)
Ex:
void setup() {
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
INPUT AND OUTPUT
• analogRead()
- reads the value from a specified analog pin.
• Syntax:
analogRead(pin)
Ex:
int analogPin = A3;
• Int val = 0;
void setup() {
Serial.begin(9600);
}

void loop() {
val = analogRead(analogPin);
Serial.println(val);
}
INPUT AND OUTPUT
• analogWrite()
- writes an analog value to a pin.
• Syntax:
analogRead(pin)
Ex:
int analogPin = A3;
• Int val = 0;
void setup() {
Serial.begin(9600);
}

void loop() {
val = analogRead(analogPin);
Serial.println(val);
}
CONTROL STRUCTURE
• If statement
- checks the condition and executes the following statements or set of statements if the condition is ‘true’.

Syntax:
if (condition) {
//statement(s);
}

Ex:
if (x>120) digitalWrite(LEDpin, HIGH);

if (x>120)
digitalWrite(LEDpin, HIGH);

if (x>120) { digitalWrite(LEDpin, HIGH) };

if (x>120) {
digitalWrite(LEDpin, HIGH);
}
CONTROL STRUCTURE
• Switch case
- specify different code that should be executed in various conditions.

Syntax:
switch (var) {
case label1:
//statement(s);
break;
case label2:
//statement(s);
break;
default:
//statement(s);
break;
}
• Ex:
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
}
CONTROL STRUCTURE
• For loop
- used to repeat block of statement enclosed in curly braces.
Syntax:
for (initialization; condition; increment) {
//statement(s);
}
Ex:
int PWMPin = 10;

void setup(){
//no setup needed
}
void loop() {
for (int I =0 ; I <=255; i++) {
analogWrite(PWMpin, I );
delay(10);
}
}

You might also like