Beruflich Dokumente
Kultur Dokumente
MC Blinky Recipe
Hardware
Precautions
The Arduino MC
Static Chip fragility Lead breakage for components 40mA current limits!! Turn off while building circuits If power LED goes out, turn off immediately
Labeled parts bin Arduino (numbered) USB cable (numbered) Breadboard (numbered) 2 x leds (your choice) resistors
2 x 470
MC Blinky Recipe
Hardware
The Arduino MC USB cable 2 LEDs 2 470 resistors Circuit #1 (test - no control over light) Why the resistor?
GND
Circuit
MC Blinky Recipe
Hardware
The code
The Arduino MC USB cable 2 LEDs 2 470 resistors Circuit #2 (control over 1 light)
Circuit
D12
GND
Heres some working code (we wont worry about what it means yet) Save sketch Compile Set COM port
12
12
Tools>COM
D12 D13
What is a microcontroller?
Easy to use
Processing
Arduino Specs
In/out pins
Power
Reset
Reset button Power jack (9V) USB jack Lights PWR, TX, RX, L ICSP header(6 pins) direct MC programming (no bootloader) bootloader)
The PDE
D12 D13
Digital in
Pushbutton
1, 4 2, 3
1 2
4 3
1, 4 2, 3
1, 4 2, 3
10k
Res
Br K O
5V
The circuit
D2
The code
D12 D13
const int buttonPin = 2; const int ledPin = 13; int buttonState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } 5V void loop(){ buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
D2
IF ELSE
one solution Problems with? Better solutions? What would we do if our code didnt work?
const int buttonPin = 2; const int ledPin1 = 13; const int ledPin2 = 12; int buttonState = 0; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin, INPUT); } void loop(){ buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); delay(1000); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); delay(1000); } else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); } }
Debugging code
How do you know whats going on in your MCs brain? Introducing the Serial monitor a brain window How do we use it?
Examples->DigitalExamples->Digital>Button Add the following lines: Compile Upload Working? Now open Serial Monitor Working? What do these lines do? How is this debugging?
const int buttonPin = 2; const int ledPin = 13; int buttonState = 0; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop(){ buttonState = digitalRead(buttonPin); Serial.print(buttonState= ); Serial.println(buttonState); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
Inputs
Code
Structure
Outputs
Commands
More Inputs!
5V
A A
+ + +
10k
pot
A0
10 k
The circuit
W B
W W
B B
The code Upload, open Serial Monitor Working? Dissect code Challenge: variable rate uni-blinky (0uni(01024ms)
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delay(sensorValue); digitalWrite(ledPin, LOW); delay(sensorValue); }
The QRD1114 reflectance sensor How does it work? How to connect why? To do write code to read values from serial monitor in real time
How about more output? The hardware The circuit The code Does it do what it should? More inputs?
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delayMicroseconds(sensorValue); digitalWrite(ledPin, LOW); delayMicroseconds(sensorValue); }
5V
D13
+
A0
10 k
Under construction Tone() command (add content) Note: output mode not needed with Tone Does pitch go same way? Why?
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { } void loop() { sensorValue = analogRead(sensorPin); tone(ledPin, sensorValue); }
LightLight-controlled blinky
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delay(sensorValue); digitalWrite(ledPin, LOW); delay(sensorValue); }
photoresistor
Circuit
How can we make better? 5V
A0
10 k
void loop() { sensorValue = analogRead(sensorPin); //sensorValue = map(sensorValue, 580, 1000, 20, 500); //Serial.println(sensorValue); Set these Get these digitalWrite(ledPin, HIGH); according to from serial delay(sensorValue); your output monitor digitalWrite(ledPin, LOW); device delay(sensorValue); }
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delayMicroseconds(sensorValue); digitalWrite(ledPin, LOW); delayMicroseconds(sensorValue); }
A0
D13
10 k
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { //Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); //sensorValue = map(sensorValue, 580, 1000, 20, 1000); //Serial.println(sensorValue); digitalWrite(ledPin, HIGH); delayMicroseconds(sensorValue); digitalWrite(ledPin, LOW); delayMicroseconds(sensorValue); }
Use Serial Monitor to get 1st 2 values in map() Then comment out Serial.println command
http://arduino.cc/en/Reference/HomePage http://arduino.cc/en/Reference/HomePage
Inputs
Serial In (from computer) Push button (digitalRead) digitalRead) Pot (analogRead) (analogRead)
Photoresistor (analogRead) analogRead)
Code
Structure
Commands
Outputs
Led lights (digitalWrite) (digitalWrite) Serial Out (to computer) Speaker (digitalWrite) (digitalWrite)
Analog output?
2.0 ms
Vss (0 V) 20 ms
Vdd (5 V)
But you dont need to know any of this thanks to Arduino #include Libraries
2.0 ms
1.0 ms Vdd (5 V)
standard servo www.parallax.com
1.0 ms
Vss (0 V)
Vss (0 V)
10 o clock
2 o clock
12 o clock
20 ms
1.5 ms Vdd (5 V)
standard servo www.parallax.com standard servo www.parallax.com standard servo www.parallax.com
standard servo www.parallax.com
1.5 ms
Vss (0 V)
1.0 ms
2.0 ms
1.5 ms
20 ms
#include <Servo.h> Servo myservo; int pos = 0; void setup() { myservo.attach(9); } void loop() { for(pos = 0; pos < 180; pos += 1) { myservo.write(pos); delay(15); } for(pos = 180; pos>=1; pos-=1 { myservo.write(pos); delay(15); } }
5V D9
GND
Challenge: make a potpotcontrolled servo Dont cheat and use Knob code! The circuit Hints: Dont need for loop
outVal = map(inVal, 0, 1023, 0, 179);
#include <Servo.h> 5V Servo myservo; int potpin = 0; int val; void setup() 10 k A0 { myservo.attach(9); } void loop() { val = analogRead(potpin); val = map(val, 0, 1023, 0, 179); myservo.write(val); }
Working?
An answer
5V D9
GND
Light fading
D9
The Hardware - LED The Circuit The code: Example-> Analog -> ExampleFading Working? What would Dissect code
int ledPin = 9; void setup() { }
GND
void loop() { for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { analogWrite(ledPin, fadeValue); delay(30); } for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { analogWrite(ledPin, fadeValue); delay(30); } }
490Hz
Inputs
Serial In (from computer) Push button (digitalRead) digitalRead) Pot (analogRead) (analogRead)
Photoresistor (analogRead) analogRead)
Code
Structure
Outputs
Commands
Serial Out (to computer) Speaker (digitalWrite) (digitalWrite) Servo (myServo.write) (myServo.write) LED fade (analogWrite) (analogWrite)
pinmode( pinmode(pin#, mode) mode) map(val,d0,d1,r0,r1) #include <file.h> <file.h> Servo objectname
More?
Other sensors/actuators
Input (sensors)
Piezo knock sensor Accelerometer PIR (motion detector) Temperature Pressure Strain Force Microphone GPS US sensor more
LED and LCD displays DC motor Stepper motor Cameras Solonoids more
MC project
Ideas
Can use pre-written code preparts (cite), but overall project should be your own Level of complexity should reflect your skill level Daily progress report (oral) I may add/subtract from your project Assessment (more to come) Due early/mid Nov
Solar, weather, water quality, go-kart motion, goenergy monitoring, compost, animal camera
Example projects
AntiAnti-poo alarm
Puzzle box
Words on wheels
Solar tracker
RoboRobo-roach
Teen cave
Flowcharts
Graphical representation of program flow Clear idea of structure Loops look like loops Easy to ID endless loops Easy to move from chart to code
START
Flowchart symbols
i=i+1
DISPLAY i
IS i<50 ?
YES NO END
Start and end Arrow control passing Processing Input/Output Conditional or decision What does this do? Examples
i=i+1
DISPLAY i
IS i<50 ?
YES NO END
What changed?
void loop() { for (int thisPin = 0; thisPin < 8; thisPin++) { if (thisPin == 4) { break; } for (int blinkVal = 0; blinkVal < 5; blinkVal++) { digitalWrite(thisPin, HIGH); delay(500); digitalWrite(thisPin, LOW); delay(500); } } }
Indentation Style
Indent code within {} Indent = 2 spaces Close bracket in line with open line
Why do it?
Can see code structure Code flow easy to imagine Easy to ID {} Convention
void loop() { for (int thisPin = 0; thisPin < 8; thisPin++) { if (thisPin == 4) { break; } for (int blinkVal = 0; blinkVal < 5; blinkVal++) { digitalWrite(thisPin, HIGH); delay(500); digitalWrite(thisPin, LOW); delay(500); } } }
MC Presentation
Overview of problem, how you thought of it, why MC is a good solution Overview of how you solved, including
Demonstration of your device What you learned, what you would do differently if you could do it again, what you would add if you had more time (and how you might do this)
Score (1-10)
PRETTY BAD 1 2
NEEDS WORK 3 4
SATISFACTORY 5 6
VERY GOOD 7 8
EXCEPTIONAL 9 10
To add
While loop Arrays Math (+-*/) (+Comparison ops (<,>,<=, etc) etc)
Constrain(x,a,b) Constrain(x,a,b) Cant use pins 0+1 while uploading Time: millis() how to use millis() Functions user defined comparison (==) vs assignment (=)
homework
Take home your boards, jumper, components download what you need from http://www.arduino.cc/
Project 1
Project 2
z y
V Not used D3
5V 5V
D2
4 x 470 2 x 10k
(Br K O)
Jumpers (5-10) (5Arduino (numbered) USB cable (numbered) Breadboard (numbered) Power supply (later)