introduction to sensor technology week five adam taylor [email protected]
TRANSCRIPT
Error Checking
• Simple Comparison• Max Min values• Calibration• Averaging• Median Filtering• Debouncing• Kalman Filtering
Sensors
• Location- GPS, WiFi, Cell Based, Ping Based• Item ID- RFID• Non-Sensor Sensors- Calendars, Logging Data,
Preferences …
Multi Task Programming
• Might want to do more than 1 thing at a time• Might not know when things will happen• Different priorities for jobs• Responsiveness etc.
Threads
• On most computers this is achieved through threads and processes
• A desktop computer has many cores, so it is able to do several things at once.
• Arduino can not so it needs to be clever (or you do)
Time
• Using delay(n) is ok for waiting but some times you want to do other things while you wait
• millis() returns the time in mS since the program started as an unsigned long
• If you save the time you do something then wait until it + some time you have a delay that you can do other things during
• Unsigned longs are 32 bits (4 bytes) so…
Time
• Max 2^32 -1 mS can be counted =4294967295• 4294967295/1000 = seconds• 4294967/60/60=hours• 1193.04638889/24=days• 49.71 days is max time you can do
Interrupt Driven Programming
• In more complicated programs for when things can happen at any time (i.e. user input)
• Arduino has ~2 interrupt lines, if these happen then a function is called
• http://arduino.cc/en/Reference/attachInterrupt
• Interrupts can happen because of users or things like timers
Interrupt Driven Programming
• attachInterrupt(interrupt, myFunction, mode)• Interrupt can be 0/1 for pin 2/3 on uno• Mode can be LOW, CHANGE, RISING, FALLING• myFunction can be any function but Delay(n)
won’t work and they should be a quick as possible
• int pin = 13;volatile int state = LOW;//a variable that can change from outside program
void setup(){ pinMode(pin, OUTPUT); attachInterrupt(0, blink, CHANGE);//if pin 2 changes call blink}
void loop(){ digitalWrite(pin, state);//set the LED}
void blink(){ state = !state;}
Serial Event
• Serial events are a lot like interrupts but caused by typing
• When they happen a special function is called• serialEvent() is its name• Takes less set up than ‘normal’ interrupts
Serial Event• void serialEvent() {
while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { stringComplete = true; } }}
Shields
Shields
• Shields allow Arduino’s to do things that the basic hardware wouldn’t allow
• Connect to WiFi, Ethernet, Xbees, GSM• Cameras, Voice recognition, Motor Control
Project Deadlines
• Demo: April 15th from 11:00• Report: ~1pg due April 20th at 23:59• If you change the project make sure to check
with me first.• Submit by email to
[email protected] with your report and code attached