0% found this document useful (0 votes)
7 views61 pages

Arduino Programming Part 1

The document provides an introduction to Arduino programming, covering essential functions such as pinMode, digitalWrite, and digitalRead, along with examples for controlling LEDs and push buttons. It explains the use of pull-up resistors, debouncing techniques for push buttons, and introduces concepts like arrays and modulo operations for LED control. Additionally, it includes practical examples and exercises to reinforce learning, such as creating patterns with LEDs and interfacing with seven-segment displays.

Uploaded by

2022303796
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views61 pages

Arduino Programming Part 1

The document provides an introduction to Arduino programming, covering essential functions such as pinMode, digitalWrite, and digitalRead, along with examples for controlling LEDs and push buttons. It explains the use of pull-up resistors, debouncing techniques for push buttons, and introduces concepts like arrays and modulo operations for LED control. Additionally, it includes practical examples and exercises to reinforce learning, such as creating patterns with LEDs and interfacing with seven-segment displays.

Uploaded by

2022303796
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arduino Programming Part 1

Engr. Enmar T. Tuazon


pinMode(pin, Mode)
• Configures the specified pin to behave either input or output.
• Parameters:
pin: the number of the pin whose mode you wish to set.
Mode: either INPUT or OUTPUT
Example:
int ledPin = 13; // LED connected to digital pin 13
Example
int ledPin = 13; // LED connected to digital pin 13

void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
digitalWrite(pin, value)
•Sets a pin configured as OUTPUT to either a
HIGH or a LOW state at the specified pin.
•The digitalWrite() function is also used to set
pullup resistors when a pin is configured as an
INPUT.
digitalRead(pin)
• Reads the value from a specified pin, it will be either
HIGH or LOW.
Example:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the
voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the
voltage LOW
delay(1000); // wait for a second
}
Push Button
The pushbutton is a component that connects two points in a circuit
when you press it.
When the pushbutton is open (unpressed) there is no connection
between the two legs of the pushbutton, so the pin is connected to 5
volts (through the pull-up resistor) and we read a HIGH. When the
button is closed (pressed), it makes a connection between its two legs,
connecting the pin to ground, so that we read a LOW. (The pin is still
connected to 5 volts, but the resistor in-between them means that the
pin is "closer" to ground.)
Digital Input Pull-Up Resistor
Properties of Pins Configured as
INPUT_PULLUP
• There are 20K pullup resistors built into the Atmega chip that can be
accessed from software. These built-in pullup resistors are accessed
by setting the pinMode() as INPUT_PULLUP. This effectively inverts
the behavior of the INPUT mode, where HIGH means the sensor is off,
and LOW means the sensor is on.
• When connecting a sensor to a pin configured with INPUT_PULLUP,
the other end should be connected to ground. In the case of a simple
switch, this causes the pin to read HIGH when the switch is open, and
LOW when the switch is pressed.
Debounce on a Pushbutton
• Pushbuttons often generate spurious open/close transitions when
pressed, due to mechanical and physical issues: these transitions may
be read as multiple presses in a very short time fooling the program.
This example demonstrates how to debounce an input, which means
checking twice in a short period of time to make sure the pushbutton
is definitely pressed. Without debouncing, pressing the button once
may cause unpredictable results. This sketch uses the millis() function
to keep track of the time passed since the button was pressed.m
Example
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
Example
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is LOW:


if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
Knight Rider
Named this example in memory to a TV-series
from the 80's where the famous David Hasselhoff
had an AI machine driving his Pontiac. The car had
been augmented with plenty of LEDs in all
possible sizes performing flashy effects.
Example
Example
Example
int timer = 100;
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}

}
Example

// loop from the highest pin to the lowest:


for (int thisPin = 7; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
Seatwork
• Kindly write down a code for a condition of:
• 4 leds are used as OUTPUT
• The first 2 LED blinks with a delay of 1000 then turns LOW.
• After the first 2 LED turns low,
• The last 2 LED blinks with a delay also of 1000 then turns LOW.
Interfacing Seven Segment to an Arduino
• Common Cathode

• Common Anode
Using for Loop
void setup() {
// define pin modes
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
void loop() {
// loop to turn leds od seven seg ON
for(int i=2;i<9;i++)
{
digitalWrite(i,HIGH);
delay(600);
} // loop to turn leds od seven seg OFF
for(int i=2;i<9;i++)
{
digitalWrite(i,LOW);
delay(600);
}
delay(1000);
}
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, 0); // start with the "dot" off
}
void loop() {
// write '9'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '8'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '7'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
// write '6'
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '5'
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '4'
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '3'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
delay(1000);
// write '2'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 1);
delay(1000);
// write '1'
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
// write '0'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
delay(4000);
}
Array (typeName variableName [size])
An array is a collection of variables that are accessed with an index
number. Arrays in the C programming language, on which Arduino is
based, can be complicated, but using simple arrays is relatively
straightforward.
Example:
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
2, 3, 4, 5, 6, 7
}; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the
array)

void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
Using Switch Case
Modulo %
Remainder operation calculates the remainder when one integer is
divided by another. It is useful for keeping a variable within a particular
range (e.g. the size of an array). The % (percent) symbol is used to carry
out remainder operation.
Syntax:
remainder = dividend % divisor;
Parameters:
remainder : variable. Allowed data types: int, float, double
dividend : variable or constant. Allowed data types: int
divisor : non zero variable or constant. Allowed data types: int
Example:
• int x = 0;
• x = 7 % 5; // x now contains 2
• x = 9 % 5; // x now contains 4
• x = 5 % 5; // x now contains 0
• x = 4 % 5; // x now contains 4
• x = -4 % 5; // x now contains -4
• x = 4 % -5; // x now contains 4
Led Control using Modulo
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin1 = 3; // the number of the LED pin
const int ledPin2 = 4; // the number of the LED pin
const int ledPin3 = 5; // the number of the LED pin
const int ledPin4 = 6; // the number of the LED pin
const int ledPin5 = 7; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 5;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (buttonPressCount % numberOfLED == 0){
digitalWrite(ledPin1, HIGH);} // turn LED1 on:
else{
digitalWrite(ledPin1, LOW);}
if (buttonPressCount % numberOfLED == 1){
digitalWrite(ledPin2, HIGH); }// turn LED2 on:
else{
digitalWrite(ledPin2, LOW)};
buttonPressCount++;
delay(300);
}
}
Modulo on 8-bit LED
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}
void loop()
{
for(byte i=0;i<256;i++)
{

byte a=i%2;
byte b=i/2 %2;
byte c=i/4 %2;
byte d=i/8 %2;
byte e=i/16 %2;
byte f=i/32 %2;
byte g=i/64 %2;
byte h=i/128 %2;
digitalWrite(2,a);
digitalWrite(3,b);
digitalWrite(4,c);
digitalWrite(5,d);
digitalWrite(6,e);
digitalWrite(7,f);
digitalWrite(8,g);
digitalWrite(9,h);

delay(200);
}
}
Array + Modulo 8bit LED Counter
int ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the pin each LED is
connected to Arduino.
void setup () {
for (int i = 0; i<8;i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop () {
for (int i = 0; i<256;i++) {
iloveEcE(i);
delay(100);
}
}
void iloveEcE(int num) {
for (int i = 0; i<8; i++) {
If(num%2){
digitalWrite(ledPins[i], HIGH);}
else{
digitalWrite(ledPins[i], LOW);}
num/=2;
}
}
Logical Operators
LOGICAL AND (&&)
Description
Logical AND results in true only if both operands are true.
Example Code
This operator can be used inside the condition of an if statement.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // if BOTH the
switches read HIGH
// statements
}
Logical NOT (!)
Description
Logical NOT results in a true if the operand is false and vice versa.
Example Code
This operator can be used inside the condition of an if statement.
if (!x) { // if x is not true
// statements
}
It can be used to invert the boolean value.
x = !y; // the inverted value of y is stored in x
LOGICAL OR (||)
Description
Logical OR results in a true if either of the two operands is true.
Example Code
This operator can be used inside the condition of an if statement.
if (x > 0 || y > 0) { // if either x or y is greater than zero
// statements
}
Not Equal (!=)
Description
Compares the variable on the left with the value or variable on the
right of the operator. Returns true when the two operands are not
equal.
Syntax
x != y; // is false if x is equal to y and it is true if x is not equal to y
Example Code (SSD)
int a = 2; //For displaying segment "a"
int b = 3; //For displaying segment "b"
int c = 4; //For displaying segment "c"
int d = 5; //For displaying segment "d"
int e = 6; //For displaying segment "e"
int f = 8; //For displaying segment "f"
int g = 9; //For displaying segment "g"
void setup() {
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G
}
void displayDigit(int digit)
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);

//Conditions for displaying segment b


if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);

//Conditions for displaying segment c


if(digit !=2)
digitalWrite(c,HIGH);
//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);

//Conditions for displaying segment e


if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);

//Conditions for displaying segment f


if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);
//Conditions for displaying segment g
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);
}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}
void loop() {
for(int i=0;i<10;i++)
{
displayDigit(i);
delay(1000);
turnOff();
}
}

You might also like