Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file...
Transcript of Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file...
![Page 1: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/1.jpg)
Arduino An Introduction to Engineers
Hong Zhang, PhD
Mechanical Engineering
Rowan University
![Page 2: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/2.jpg)
What is Arduino
• Arduino is an open-source electronics prototyping
platform based on flexible, easy-to-use hardware
and software.
![Page 3: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/3.jpg)
Peripherals
USB Cord
Battery Wires
Breadboard
Wire Stripper
![Page 4: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/4.jpg)
Amazon Kit
![Page 5: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/5.jpg)
Sparkfun Kit
![Page 6: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/6.jpg)
Soldering
Solder
Stand
Perfboard Long nose plier
Solder Remover Iron
![Page 7: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/7.jpg)
Variations
LilyPad
Mini Esplora
Due
![Page 8: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/8.jpg)
Shields
Touchpad Joystick
Cellphone LCD TinkerKit
![Page 9: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/9.jpg)
![Page 10: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/10.jpg)
Clones
Motoruino
Maple Boarduino
Roboduino Freeduino
Sanguino
![Page 11: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/11.jpg)
USB Jack
Transmission status LED
Pin 13 LED
Power Jack
Reset
Microcontroller ATMEGA328
Power Pins Analog Input Pins
Digital I/O Pins
ICSP Header
Power LED
Voltage Regulator
USB Chip
Crystal
![Page 12: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/12.jpg)
![Page 13: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/13.jpg)
Verify the grammar
Upload to the Arduino board
New file
Open a file
Save the current file
Open up the serial monitor window
Code
Message Serial port
![Page 14: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/14.jpg)
Use Arduino
Generate Idea
Make Plans
Write Code
Upload & Test
Design Circuit
Review, rebuild, modification
Project Goal
![Page 17: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/17.jpg)
• Don’t put Arduino board on metal surface!
• Unplug power first before making any change on
circuit.
• If you smell something, see smoke or flame, touch
something burning, then something must be wrong!
Disconnect the power first.
1 Safety
![Page 18: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/18.jpg)
Get Started
• http://arduino.cc/en/Main/Software
![Page 19: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/19.jpg)
Learn From Examples • Online:
http://arduino.cc/en/Tutorial/HomePage
• Or from local Examples
![Page 20: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/20.jpg)
Blink Example /*
Blink
Turns on an LED on for one second, then off for one second,
repeatedly.
This example code is in the public domain.
*/
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
![Page 21: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/21.jpg)
pinMode(pin#, OUTPUT) • The specified pin can provide a substantial amount
of current to other circuits.
• Current is up to 40 mA (milliamps) to other
devices/circuits.
• Enough for an LED (don't forget the series resistor)
• Not enough to run most relays, solenoids, or motors.
![Page 22: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/22.jpg)
pinMode(pin#, INPUT) • Default if not specified
• Avoid pin 13 since it’s assigned to an onboard LED.
Pin 13 LED
![Page 23: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/23.jpg)
digitalWrite()
• digitalWrite(Pin#, HIGH); o Set the pin to high voltage (either 3.3V or 5V)
• digitalWrite(Pin#, LOW); o Set the pin to low voltage (0V)
![Page 24: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/24.jpg)
LED
http://www.societyofrobots.com/electronics_led_tutorial.shtml
![Page 25: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/25.jpg)
External LED
![Page 26: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/26.jpg)
Read Resistor
![Page 27: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/27.jpg)
![Page 28: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/28.jpg)
Blink 1 LED
1. Put 1 LED on the breadboard
2. Connect the LED to pin 10 of the Arduino
3. Blink the LEDs 10 & 13 together
4. Change the duration and intervals of blinking
5. Generate Morse code “· · · — — — · · ·”
![Page 29: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/29.jpg)
Using Constants const int ontime = 2000; // LED ON time
const int offtime = 2000; // LED OFF time
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(ontime); // wait
digitalWrite(13, LOW); // set the LED off
delay(offtime); // wait
}
![Page 30: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/30.jpg)
Using Variables int ontime, offtime; // LED ON and OFF time
void setup() {
pinMode(13, OUTPUT);
ontime = 1000;
offtime = 1000;
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(ontime); // wait
digitalWrite(13, LOW); // set the LED off
delay(offtime); // wait
}
![Page 31: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/31.jpg)
220Ω 220Ω
220Ω
+ +
+ _
_
_
![Page 32: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/32.jpg)
Blink 3 LEDs 1. Put 1 Red LED, 1 Green LED, and 1 yellow LED on
the breadboard
2. Connect the LEDs to pins 8, 7, 6
3. Blink the LEDs together
4. Change the duration and intervals of blinking
![Page 33: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/33.jpg)
Flow Control
for (int i=6; i<9; i++) {
for (int j=6; j<9; j++) {
digitalWrite(j, LOW);
}
digitalWrite(i, HIGH);
delay(flashtime);
}
![Page 34: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/34.jpg)
Blink the LEDs Sequentially
1. Follow the order RGYRGYRGY…
2. Combine order with duration and interval.
3. Synchronize with a song of choice.
![Page 35: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/35.jpg)
Read Digital Pin High
![Page 36: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/36.jpg)
const int buttonPin = 6; // the number of the input pin
const int ledPin = 10; // the number of the LED pin
int buttonState = 0; // variable for reading the input status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
digitalWrite(ledPin, buttonState);
}
![Page 37: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/37.jpg)
Operation
• 1 – 0 = 1;
• 1 – 1 = 0;
• !1 = 0;
• !0 = 1;
• 1 – LOW = HIGH;
• 1 – HIGH = LOW;
• !HIGH = LOW;
• !LOW = HIGH;
HIGH = 1
LOW = 0
![Page 38: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/38.jpg)
Read Digital Pin Low
![Page 39: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/39.jpg)
buttonState = !buttonState;
Or
buttonState = 1-buttonState;
![Page 40: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/40.jpg)
Switch
![Page 41: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/41.jpg)
Push To Turn ON
![Page 42: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/42.jpg)
Push To Turn OFF
![Page 43: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/43.jpg)
if … else…
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
![Page 44: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/44.jpg)
Comparison Operations
>
<
==
!=
>=
<=
![Page 45: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/45.jpg)
Optical Switch
![Page 46: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/46.jpg)
Infrared Light of Optical Sensor
![Page 47: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/47.jpg)
Optical Sensor
82Ω 47kΩ
A
K E
C
GND
+5V
Output White wire
![Page 48: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/48.jpg)
82Ω 47kΩ
![Page 49: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/49.jpg)
ON With Reflection
![Page 50: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/50.jpg)
OFF With Reflection
![Page 51: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/51.jpg)
Encoder
T (s)
Voltage
5V or HIGH
![Page 52: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/52.jpg)
Non-Reflective
T (s)
Voltage
0V or LOW
![Page 53: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/53.jpg)
Moving Reflections
T (s)
Voltage
0V or LOW
5V or HIGH
![Page 54: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/54.jpg)
T (s)
Voltage
5V or HIGH
![Page 55: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/55.jpg)
T (s)
Voltage
0V or LOW
![Page 56: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/56.jpg)
T (s)
Voltage
0V or LOW
5V or HIGH
ω
![Page 57: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/57.jpg)
T (s)
Voltage
0V or LOW
5V or HIGH
ω
Φ
Δt Δt
![Page 58: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/58.jpg)
Angular Velocity
RPM =w *60/2p
![Page 59: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/59.jpg)
Φ
• Determined from the daisy wheel design
Φ
Φ=2π/8
![Page 60: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/60.jpg)
Other Encoder Wheels
High resolution
Multi-digit
![Page 61: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/61.jpg)
Got Time?
unsigned long ms, us;
ms = millis(); //obtain the time in milli second since the
program started
us = micros(); // obtain the time in micro second
since the program started
![Page 62: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/62.jpg)
Δt
• In the Setup()
time_last = 0; // Set the base time
• In the Loop()
time_current = millis(); //Get the current time
// After a trigger such as a rising or falling edge
Delta_t = time_current - time_last;
time_last = time_current; //New base time
![Page 63: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/63.jpg)
Obtain a Trigger
• In Setup()
Optical_last = HIGH; //initial base status
• In Loop()
Optical_current = digitalRead(OpticalPin); // get current status of the pin
If (Optical_current != Optical_last) { //Is it a change?
Optical_last = Optical_current; //new base status
}
![Page 64: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/64.jpg)
Serial Monitor
• Display the values of variables on a monitor
• Serial.begin(9600); // Start the serial monitor to get
feedback at 9600bit per second.
![Page 65: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/65.jpg)
Set the serial monitor at the same baud rate
![Page 66: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/66.jpg)
Speed Compares (bit/s)
• Serial: 2400, 9600, 19200, etc
• USB:1.5M, 12M, 3G, 10G
• Wi-Fi: 7.2M, 72.2M, 150M, 866.7M,
• Eithernet: 10M, 1G, 100G
• Harddrive (SATA): 1.5G, 3G, 6G
• Memory: 102G
• Note 1mB (MegaByte)=8mb
![Page 67: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/67.jpg)
Display Values
Serial.print(”text"); // Display text in verbatian
Serial.print(variable); // Display the value of a variable
Serial.println(”text"); // Display text in verbatian then
change line
Serial.println(variable); // Display the value of a
variable then change line
![Page 68: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/68.jpg)
Display Time In Serial Monitor
unsigned long ms, us;
int interval;
void setup() {
Serial.begin(9600); // Start the serial monitor to get feedback
// Make sure the baud rate of the serial monitor is set as 9600.
interval = 2000; // Initialize the interval between display
}
void loop() {
ms = millis(); //obtain the time in milli second since the program started
us = micros(); // obtain the time in micro second since the program started
Serial.print("Time: ms= "); // For easy reading on serial monitor
Serial.print(ms); // Display the time on the serial monitor
Serial.print(" , us= "); // Note that print() will just display variables
Serial.println(us); // println() will change line
delay(interval); // wait for next display
}
![Page 69: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/69.jpg)
Encoder const int OpticalPin = 7; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
unsigned long time_last, time_current; // variable to record time
int Optical_last, Optical_current; // variable for reading the optical sensor status
float phi = PI/8; // measured angle of each windlow on the wheel
float omega, rpm;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(OpticalPin, INPUT);
Serial.begin(9600); // Start the serial monitor to get feedback
time_last = 0; // initialize the time
Optical_last = HIGH;
}
void loop(){
// read the state of the Optical sensor status:
Optical_current = digitalRead(OpticalPin);
digitalWrite(ledPin, Optical_current); //LED indicator of the speed
if (Optical_current != Optical_last) { // for faster operation, we can use Optical_current & Optical_last
time_current = millis(); //read the current time
omega = 1000* phi / (time_current - time_last); // calculate the angular velocity
rpm = omega * 30 / PI; // convert angular velocity to RPM
time_last = time_current;
Optical_last = Optical_current;
Serial.print("omega= "); // Display the result to serial monitor.
Serial.print(omega);
Serial.print("rad/sec, or ");
Serial.println(rpm);
}
}
![Page 70: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/70.jpg)
Analog Read Int potPin = A0; // select the input pin for the
potentiometer. Total 6 pins from A0 to A5
Analog Input Pins
![Page 71: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/71.jpg)
Analog Read potValue = analogRead(potPin); // read the value
from the potPin and store it in the variable
potValue.
An integer range from 0 (0V) to 1023 (5V). The resolution is 4.9mV per unit.
![Page 72: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/72.jpg)
![Page 73: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/73.jpg)
Use a Pot to Control an LED int potPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int potValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600); // Start the serial monitor to get feedback
}
void loop() {
potValue = analogRead(potPin); // read the value from the potentiometer
Serial.print("Potentiometer value is ");
Serial.println(potValue);
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(potValue); // stop the program for <potValue> milliseconds
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(potValue); // stop the program for for <potValue> milliseconds
}
![Page 74: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/74.jpg)
![Page 75: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/75.jpg)
Servo
#include <Servo.h> // First need to include a servo
header file.
Servo myservo; // Create a servo object myservo
myservo.attach(7); // attaches the servo on pin 7 to
the servo object
![Page 76: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/76.jpg)
myservo.write(val);
// Set the servo position according to the variable val.
The range of val is from 0 to 179.
To ensure the range of val, we can map it from any
other values, e.g., the reading from a pot:
val = map(val, 0, 1023, 0, 179);
![Page 77: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/77.jpg)
![Page 78: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/78.jpg)
Use a Pot to Control a Servo
#include <Servo.h> // Include the servo header file
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
![Page 79: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message](https://reader033.fdocuments.net/reader033/viewer/2022042114/5e90e71d86a81132e878603a/html5/thumbnails/79.jpg)