arduino - mechatronics engineering...
TRANSCRIPT
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Course Code: MDP 454, Course Name: Mechatronics (2), Second Semester 2014
• Prof. Dr. Magdy M. Abdelhameed
Arduino
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Microcontroller Platform
Okay… but what’s a Microcontroller?
Tiny, self-contained computers in an IC
Often contain peripherals
Different packages available
Vast array of size and power available
What is Arduino?
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Students, hobbyist, artists & designers
“open source hardware”Open Source Physical Computing Platform
open sourcefree to inspect & modify
communitywiki, forums, tutorials
Why Arduino?
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
InputSensors (buttons, light sensors, ultrasound, …)
Digital Input (Serial, SPI, I2C)
OutputLEDs & Lasers
LCD/OLED/Touch Displays
Speakers
Motors, Servos, Steppers
Control and CommunicationDrive other machinery
Directly or using a communication protocol
What can it do?
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Sensors
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Arduino models differ in design but all share some basicfunctionality
AVR MicrocontrollerAtmel AVR Mega168 or AVR Mega 328
Powered either directly or via USB power
CommunicationsSerial (older models)
USB (most models)
Connections to a USB interface (smaller models)
Pins for various functions
Arduino hardware
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Pins 0-13 (GPIO pins)
•
Either input our output (default to input)•
Can output 40 mA•
•
•
Digital pins
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Switching without Pull-up and pull-down resistors
Digital pins
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Switching with Pull-up and pull-down resistors
Pull-upPull-down
Digital pins
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
What happens if we Digital Read a pin that is not connected?•20K internal pull-up resistor•
Serial pins 0 (RX) and 1 (TX). Connected to FTDI USB-to-TTL•Pin 13 is different (input) (RES + LED)•Analogue pins A0-A5 can be used as digital pins 14-19 (GPIO)•Read up to 5V. Don’t fry your pin!•
Digital pins
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
PWM used to create analogue output on digital pins•
Simulate voltage between 5V and 0V using square waves•(signal switching between on and off)
Pins 3, 5, 6, 9, 10, and 11•
Pulse Width Modulation (PWM)
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
••(signal switching between on and off)
•
Pins A0-A5
10 bit resolution (from 0-5V to int 0-1023)
100 microseconds (0.0001 s) to read an analog input,
maximum reading rate is about 10,000 times a second.
Analogue pins
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
ICSP (In Circuit Serial Programming) directly•program AVR micro-controllers
I2C / TWI (Two-wire interface) communication•Inter IC control•Arduino analog input A4 = I2C SDA (serial data line)•Arduino analog input A5 = I2C SCL (serial clock line)•
External interrupts•Event-handling without polling•Interrupt 0 (on digital pin 2) and 1 (on digital pin 3)•
Analogue reference• (Set analogue reference for ADC)
Other pins
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Arduino
Environment
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
The Arduino Environment
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Board Type
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Serial (COM) port
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
The Environment
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Parts of the code
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• Comments can be anywhere
Comments
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• Comments can be anywhere
• Comments created with // or /*
and */
Comments
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• Comments can be anywhere
• Comments created with // or /*
and */
• Comments do not affect code
Comments
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• Comments can be anywhere
• Comments created with // or /*
and */
• Comments do not affect code
• You may not need comments,
but think about the community!
Comments
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
The equals sign
= is used to assign a value
== is used to compare values
Operators
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
And & Or
&& is “and”
|| is “or”
Operators
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Basic variable types:
–Boolean
–Integer
–Character
–String
Variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: boolean variableName;
Declaring variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: boolean variableName;
–Integer: int variableName;
Declaring variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: boolean variableName;
–Integer: int variableName;
–Character: char variableName;
Declaring variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: boolean variableName;
–Integer: int variableName;
–Character: char variableName;
–String: stringName [ ];
Declaring variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: variableName = true;
–or variableName = false;
Assigning variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: variableName = true;
–or variableName = false;
–Integer: variableName = 32767;
–or variableName = -32768;
Assigning variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
–Boolean: variableName = true;
–or variableName = false;
–Integer: variableName = 32767;
–or variableName = -32768;
–Character: variableName = „A‟;
–or stringName = “SparkFun”;
Assigning variables
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Variable Scope
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void setup ( ) { }
• The setup function comes before
• the loop function and is necessary
• for all Arduino sketches
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void setup ( ) { }
• The setup header will never change,
• everything else that occurs in setup
• happens inside the curly brackets
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• Outputs are declare in setup, this is done by using the pinMode function
• This particular example declares digital pin # 13 as an output, remember to use CAPS
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• Serial communication also begins in setup
• This particular example declares Serial communication at a baud rate of 9600. More on Serial later...
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• You can also create internal pullup resistors in setup, to do so digitalWrite the pin HIGH
• This takes the place of the pullup resistors currently on your circuit 7 buttons
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Setup, Interruptsvoid setup ( ) {
attachInterrupt (interrupt, function, mode) }
–You can designate an interrupt
function to Arduino pins # 2 and 3
–This is a way around the linear
processing of Arduino
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Setup, Interruptsvoid setup ( ) {
attachInterrupt (interrupt, function, mode) }
–Interrupt: the number of the interrupt, 0 or 1, corresponding to Arduino pins # 2
and 3 respectively
–Function: the function to call when the interrupt occurs
–Mode: defines when the interrupt should be triggered
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Setup, Interruptsvoid setup ( ) {
attachInterrupt (interrupt, function, mode) }
•LOW whenever pin state is low
•CHANGE whenever pin changes value
•RISING whenever pin goes from low to high
•FALLING whenever pin goes from low to high
–Don’t forget to CAPITALIZE
Setup
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
if ( this is true ) { do this; }
If statements
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
if ( this is true ) { do this; }
If statements
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
if ( this is true ) { do this; }
If statements
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
if ( this is true ) { do this; }If statements
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
else { do this; }
Else
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
• loop
• For
• while
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void loop ( ) { }Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void loop ( ) { }Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void loop ( ) { }
The “void” in the header is what the function will return (or spit out) when it happens, in this case it
returns nothing so it is void
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void loop ( ) { }
The “loop” in the header is what the function is called, sometimes you make the name up, sometimes (like loop) the function already
has a name
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void loop ( ) { }
The “( )” in the header is where you declare any variables that you are “passing” (or
sending) the function, the loop function is never “passed” any variables
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
void loop ( ) { }Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++) { //for action code goes here//this could be anything
}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++){//for action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++) {//for action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++) {//for action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++) {//for action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++) {//for action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
for (int count = 0; count<10; count++) {//for action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
while ( count<10 ) {//while action code goes here}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
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
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
while ( count<10 ) {//looks basically like a “for” loop //except the variable is declared before//and incremented inside the while //loop}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
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}
Basic Repetition
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Arduino
Programming
Structure
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Declare variables at top
Initialize
setup() – run once at beginning, set pins
Running
loop() – run repeatedly, after setup()
Arduino “Software Layout”
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
int ledPin = 13; – ledconnected to control pin 13
int aSensor = 0; – setupGlobal Variables
sensor 'aSensor' on analog pin 0
int statePin = LOW; – use thisto hold the state of a pin
Arduino “Software Layout”
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
pinMode() – set a pin as inputor output
setup()
serial.Begin() – setup to `talk'to the computer
Arduino “Software Layout”
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
pinMode(ledPin, Output); –set the pin `ledPin' as an output
setup()
serial.Begin(9600); – talk tothe computer at 9600 baud rate
Arduino “Software Layout”
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
digitalWrite() – set a digital pin high/low
digitalRead() – read a digital pin’s state
analogRead() – read an analog pinloop()
analogWrite() – write an “analog” PWM value
delay() – wait an amount of time
millis() – get the current tim
Arduino “Software Layout”
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
14 Digital IO (pins 0 - 13)
6 Analog In (pins 0 - 5)
6 Analog Out (pins 3, 5, 6, 9, 10, 11)
Input / Output
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Digital – only has two values: on/off
Analog – has many (infinite) values
Computers don’t really do analog - usequantization instead
Digital? Analog?
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Many states (Analog) or justtwo (HIGH/LOW - Digital)
Number of states (or “bins”) isresolution
Common computer resolutions:8-bit = 256 states
16-bit = 65,536 states
32-bit = 4,294,967,296 states
Digital? Analog?
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Sensors ActuatorsLDR / IR
SolenoidSwitch /
Stepper MotorPotentiometer /Joystick / Piezo
Accelerometer Other CircuitsUltrasonic
Prototype shieldsIndicators
Special ICsLED / Lamps
Buzzers
I/O to/from What?
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Install latest Arduino IDE from arduino.ccvoid setup(){ Run Arduino IDE
// start serial port at 9600 bps:Write the code on the left into the editor
Serial.begin(9600);Compile / Verify the code by clicking the play}
button
Before uploading your sketch, check the board andvoid loop()the serial port are correct for your Arduino and for
{ your computer
Serial.print("Hello World!\n\r");Menu -> Tools -> Board
// wait 2sec for next reading:Menu -> Tools -> Serial Portdelay(2000);
} Upload the code from the computer to theArduino using the upload button
Hello World!
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Turn Led on and off according to the byte coming from the serial port, if a zero byte then the Led off else the Led on.
LED Blink
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/
int ledPin = 13; // LED connected to digital pin 13
int inByte = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(19200); // initiate serial communication
}
void loop()
{
while (Serial.available()>0) {
inByte = Serial.read();
}
if (inByte>0) {
digitalWrite(ledPin, HIGH); // sets the LED on
} else {
digitalWrite(ledPin, LOW); // sets the LED off
}
}
LED Blink
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/ Initialiseint ledPin = 13; // LED connected to digital pin 13
some of theint inByte = 0;
void setup() variables{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(19200); // initiate serial communication
}
void loop()
{
while (Serial.available()>0) {
inByte = Serial.read();
}
if (inByte>0) {
digitalWrite(ledPin, HIGH); // sets the LED on
} else {
digitalWrite(ledPin, LOW); // sets the LED off
}
}
LED Blink
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/
int ledPin = 13; // LED connected to digital pin 13
int inByte = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(19200); // initiate serial communication
}
void loop()
{ Setup LED pin andwhile (Serial.available()>0) {
inByte = Serial.read(); serial connection}
if (inByte>0) {
digitalWrite(ledPin, HIGH); // sets the LED on
} else {
digitalWrite(ledPin, LOW); // sets the LED off
}
}
LED Blink
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/
int ledPin = 13; // LED connected to digital pin 13
int inByte = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(19200); // initiate serial communication
} Loop - Reading thevoid loop() serial for info, when{
while (Serial.available()>0) {
inByte = Serial.read(); something is received}
if (inByte>0) {turn LED on LED onthe
digitalWrite(ledPin, HIGH); // sets the
} else {
digitalWrite(ledPin, LOW); // sets the LED off
}
}
LED Blink
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Write Arduino code that uses to turn led on (on pin 13) when the button status is high (on pin 7) and led off when the button status is low; Also send this status via serial port with baud rate 19600.
Digital Input/ Output
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (button)
int buttonval = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // set LED as output
pinMode(inPin, INPUT); // set pushbutton as input
Serial.begin(19200); // start serial communication to computer
}
void loop() {
buttonval = digitalRead(inPin); // read the pin and get the button's state
if (buttonval == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
Serial.write('0'); // Button off (0) sent to computer
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.write('1'); // Button on (1) sent to computer
}
}
Digital Input/ Output
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/
Initialiseint ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (button) some of theint buttonval = 0; // variable for reading the pin status
variablesvoid setup() {
pinMode(ledPin, OUTPUT); // set LED as output
pinMode(inPin, INPUT); // set pushbutton as input
Serial.begin(19200); // start serial communication to computer
}
void loop() {
buttonval = digitalRead(inPin); // read the pin and get the button's state
if (buttonval == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
Serial.write('0'); // Button off (0) sent to computer
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.write('1'); // Button on (1) sent to computer
}
}
Digital Input/ Output
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/
int ledPin = 13; // choose the pin for the LED
Setup LED pin,
int inPin = 7; // choose the input pin (button)
int buttonval = 0; // variable for reading the pin status
switch pin and
void setup() {
serial connection
pinMode(ledPin, OUTPUT); // set LED as output
pinMode(inPin, INPUT); // set pushbutton as input
Serial.begin(19200); // start serial communication to computer
}
void loop() {
buttonval = digitalRead(inPin); // read the pin and get the button's state
if (buttonval == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
Serial.write('0'); // Button off (0) sent to computer
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.write('1'); // Button on (1) sent to computer
}
}
Digital Input/ Output
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/
Loop - Reading theint ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (button) button for info, when
int buttonval = 0; // variable for reading the pin status button is press turnvoid setup() {
pinMode(ledPin, OUTPUT); // set LED as output
pinMode(inPin, INPUT); // set pushbutton asthe LED on and input
Serial.begin(19200); // start serial communication to computer
signal the computer
}
void loop() {
Of the button's state
change
buttonval = digitalRead(inPin); // read the pin and get
if (buttonval == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
Serial.write('0'); // Button off (0) sent to computer
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.write('1'); // Button on (1) sent to computer
}
}
Digital Input/ Output
Mechatronics (2)
4th Year- Mechatronics Major
Prof. Dr. Magdy M. Abdelhameed
Thank You For Your Attention!
Questions?