Programming With Arduino: Course URL: Uggy/home
Programming With Arduino: Course URL: Uggy/home
ARDUINO
Course URL:
https://sites.google.com/thapar.edu/b
uggy/home
Arduino Programming Basics
Comments
Boolean: boolean
variableName;
•loop
•For
•while
Basic Repetition
void loop ( ) { }
Basic Repetition
void loop ( ) { }
Basic Repetition
void loop ( ) { }
void loop ( ) { }
void loop ( ) { }
void loop ( ) { }
Basic Repetition
while ( count<10 )
{
//while action code goes here
//should include a way to change count
//variable so the computer is not stuck
//inside the while loop forever
}
Basic Repetition
while ( count<10 )
{
//looks basically like a “for” loop
//except the variable is declared before
//and incremented inside the while
//loop
}
Basic Repetition
Or maybe:
while ( digitalRead(buttonPin)==1 )
{
//instead of changing a variable
//you just read a pin so the computer
//exits when you press a button
//or a sensor is tripped
}
Important functions
• Serial.println(value);
– Prints the value to the Serial Monitor on your
computer
• pinMode(pin, mode);
– Configures a digital pin to read (input) or write (output)
a digital value
• digitalRead(pin);
– Reads a digital value (HIGH or LOW) on a pin set for
input
• digitalWrite(pin, value);
– Writes the digital value (HIGH or LOW) to a pin set for
output
Using LEDs
void setup()
{
pinMode(13, OUTPUT); //configure pin 13 as
output
}
// blink an LED once
void blink1()
{
digitalWrite(13,HIGH); // turn the LED on
delay(500); // wait 500 milliseconds
digitalWrite(13,LOW); // turn the LED off
delay(500); // wait 500 milliseconds
}
Switch Case
Input & Output
• Transferring data from the computer to an
Arduino is done using Serial
Transmission
• To setup Serial communication we use the
following
void setup() {
Serial.begin(9600);
}
36
Writing to the Console
void setup() {
Serial.begin(9600);
Serial.println(“Hello World!”);
void loop() {}
37
Reading data from Arduino
void setup()
{
Serial.begin(9600);
}
void serialtest()
{
int i;
for(i=0; i<10; i++)
Serial.println(i);
}
Arduino Code Basics
Arduino programs run on two basic
sections:
void setup() {
}
void loop() {
}
DC MOTORS AND
ACTUATORS
Actuators
• An electric motor is an electrical machine that
converts electrical energy into mechanical energy.
• Actuator: Device that turns energy (typically
electrical) to motion
• Features
– Force
– Speed
– Torque
– Efficiency
DC motor
• Force is produced
due to the electric
current in a wire inside
a magnetic field.
• Proportional to the
current, therefore can
be controlled by
potentiometer
• Hard to control
precisely
DC motor Driver/ H bridge IC/ L293D
H bridge connection with arduino
Buggy Motor Code
then wait to capture the rising edge output by echo port, at the
same time, open the timer to start timing.
PIC18F26K80 DEVELOPMENT BOARD
Ultrasonic Sensor Example (1/2)
void loop() {
int s=0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= (duration*0.034)/2;
distanceInch = (duration*0.0133)/2;
Serial.println(distanceCm);
if(distanceCm<30) //here we set the object range 15cm.
{
Put your code here
}
Code using NewPing library
#include <NewPing.h>
#define TRIGGER_PIN 13
#define ECHO_PIN 12
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50);
Serial.print("Ping: ");
Serial.print(sonar.ping_cm());
Serial.println("cm");
}
ZigBee Technology
The explosion in wireless technology has seen the emergence of many standards,
especially in the industrial, scientific and medical (ISM) radio band. There have been
a multitude of proprietary protocols for control applications, which bottlenecked
interfacing. Need for a widely accepted standard for communication between sensors
in low data rate wireless networks was felt.
Comparision Between Zigbee and Bluetooth