FOSSCOMM 2013 6ο Συνέδριο Κοινοτήτων Ανοιχτού Λογισμικού Σάββατο 20 Απριλίου 2013 Ομάδα Σχολής Ικάρων Εργαστήριο Arduino
Arduino Workshop
LAB 1 : Παιχνίδι με έναν αισθητήρα φωτός Τι θα χρειαστούμε: 1 LED (light emitting diodes) 1 Φωτοαντίσταση (light sensor) Μερικά καλώδια 1 breadboard 1 arduino board
LAB 1 : Παιχνίδι με έναν αισθητήρα φωτός Σενάριο: Μέσω μιας φωτοαντίστασης θα παίξουμε με το led. Το παιχνίδι θα έχει ως εξής : αρχικά θα επηρεάσουμε την συχνότητα με την οποία ανάβει το led ανάλογα της φωτεινότητας που δέχεται ο αισθητήρας.στην συνέχεια αντί να πειράζουμε την συχνότητα θα επηρεάσουμε την φωτεινότητα του led. Ανάλογα του τι είναι πιο χρήσιμο θα προγραμματίσουμε το arduino μας ώστε είτε να φωτίζει περισσότερο αν δέχεται λιγότερο φως, είτε το αντίθετο.
Κύκλωμα 1ου LAB
Arduino Κώδικας για το LAB 1(A)(file lab1_a_.ino) // LAB 1(a): Blink LED at a rate specified by the // value of the analogue input const int LED = 13; // the pin for the LED int val = 0; // variable used to store the value // coming from the sensor void setup() pinmode(led, OUTPUT); // LED is as an OUTPUT // Note: Analogue pins are // automatically set as inputs void loop() val = analogread(0); // read the value from // the sensor digitalwrite(led, HIGH); // turn the LED on delay(val); // stop the program for // some time digitalwrite(led, LOW); // turn the LED off delay(val); // stop the program for // some time
Arduino κώδικας για το LAB 1(B)(file lab1_b_.ino) // LAB 1(b) : Turn LED on at a value specified by sensor const int LED = 13; // the pin for the LED int val = 0; // variable used to store the value // coming from the sensor void setup() pinmode(led, OUTPUT); // LED is as an OUTPUT // Note: Analogue pins are // automatically set as inputs void loop() val = analogread(0); // read the value from the sensor analogwrite(led,255- val/4); // turn the LED on by value specified
LAB 2 : Έλεγχος Led μέσω σειριακής οθόνης Τι θα χρειαστούμε: 2 LED Μερικά καλώδια 1 breadboard 1 arduino board
LAB 2 : Έλεγχος Led μέσω σειριακής οθόνης Σενάριο : Αυτό που θα κάνουμε είναι να ελέγχουμε την κατάσταση (On and Off) 2 LED. Ανάλογα με το τι τιμή θα στέλνουμε μέσω της σειριακής οθόνης(π.χ 3 ή 4) θα ανοιγοκλείνουν τα 2 αυτά LED.
Κύκλωμα 2ου LAB
int d; int a=3; int b=4; int a1=0; int b1=0; void setup() Serial.begin(9600); Serial.println("Serial communication started... "); pinmode(a,output); pinmode(b,output); void loop() if(serial.available()) d=serial.read(); if(d==51) Serial.write(d); Serial.println(" led activated."); if(a1==1) digitalwrite(a,high); a1=0; else digitalwrite(a,low); a1=1; delay(500); else if(d==52) Serial.write(d); Serial.println(" led activated."); if(b1==1) digitalwrite(b,high); b1=0; else digitalwrite(b,low); b1=1; delay(500); Κώδικας του LAB 2 (file lab2.ino)
LAB 3 : Ρυθμίζοντας την φωτεινώτητα 2 LED Τι θα χρειαστούμε: 2 LED Μερικά καλώδια 2 αντιστάσεις 1 breadboard 1 arduino board
LAB 3 : Ρυθμίζοντας την φωτεινότητα 2 LED Σενάριο: Θέλουμε να ελέγξουμε τη φωτεινότητα 2 LED μέσω του κώδικα που θα γράψουμε. Τα 2 LEDάκια θα αρχίζουν από την κατάσταση (off) και στη συνέχεια η φωτεινότητά τους θα αυξάνεται μέχρι να φτάσει στο μέγιστο δυνατό και στη συνέχεια πάλι θα μειώνεται μέχρι να σβήσουν τελείως. Η παραπάνω διαδικασία θα επαναλαμβάνεται ασταμάτητα.
Κώδικας του LAB 3 (file lab3.ino) /* * LedBrightness sketch * controls the brightness of LEDs on analog output ports */ const int firstled = 3; // specify the pin for each of the LEDs const int secondled = 5; int brightness = 0; int increment = 1; void setup() // pins driven by analogwrite do not need to be declared as outputs void loop() if(brightness > 254) I increment = -1; // count down after reaching 255 else if(brightness < 1) increment = 1; //count up after dropping back down to 0 brightness = brightness + increment; // increment (or decrement sign is minus) // καθορίζουμε τη φωτεινότητα των LEDs analogwrite(firstled, brightness); analogwrite(secondled, brightness); delay(10); // 10ms for each step change means 2.55 secs to fade up or down
Κύκλωμα 3ου LAB
LAB 4: Χρησιμοποιώντας το Arduino ώς ρολόι Σενάριο: Θέλουμε να χρησιμοποιήσουμε το Arduino ως ρολόι στο οποίο δίνουμε μια αρχική ώρα για συγχρονισμό. Στη συνέχεια, χρησιμοποιούμε 2 διακόπτες. Όταν κρατάμε πατημένο τον πρώτο, ο χρόνος τρέχει προς τα μπρος. Όταν κρατάμε πατημένο τον δεύτερο, ο χρόνος τρέχει προς τα πίσω.
Κώδικας LAB 4 (file lab4.ino) /* AdjustClockTime sketch buttons on pins 2 and 3 adjust the time */ #include <Time.h> const int btnforward = 2; // button to move time forward const int btnback = 3; // button to move time back unsigned long prevtime; // when the clock was last displayed void setup() digitalwrite(btnforward, HIGH); // enable internal pullup resistors digitalwrite(btnback, HIGH); settime(12,0,0,1,1,11); // start with the time set to noon Jan 1 2011 Serial.begin(9600); Serial.println("ready"); void loop() prevtime = now(); // note the time while( prevtime == now() ) // stay in this loop till the second changes // check if the set button pressed while waiting for second to roll over if(checksettime()) prevtime = now(); // time changed so reset start time digitalclockdisplay(); // functions checks to see if the time should be adjusted // returns true if time was changed boolean checksettime() int step; // the number of seconds to move (backwards if negative) boolean istimeadjusted = false; // set to true if the time is adjusted step = 1; // ready to step forwards while(digitalread(btnforward)== LOW) adjusttime(step); istimeadjusted = true; // to tell the user that the time has changed step = step + 1; // next step will be bigger digitalclockdisplay(); // update clock delay(100); step = -1; // negative numbers step backwards while(digitalread(btnback)== LOW) adjusttime(step); istimeadjusted = true; // to tell the user that the time has changed step = step - 1; // next step will be a bigger negative number digitalclockdisplay(); // update clock delay(100); return istimeadjusted; // tell the user if the time was adjusted void digitalclockdisplay() // digital clock display of the time Serial.print(hour()); printdigits(minute()); printdigits(second()); Serial.print(" "); Serial.print(day()); Serial.print(" "); Serial.print(month()); Serial.print(" "); Serial.print(year()); Serial.println(); void printdigits(int digits) // utility function for clock display: prints preceding colon and leading 0 Serial.print(":"); if(digits < 10) Serial.print('0'); Serial.print(digits);
Κύκλωμα 4ου LAB
LAB 5 : Δημιουργία ειδοποίησης για να εκτελείται περιοδικά μία συνάρτηση Τι θα χρειαστούμε: 1 arduino board Time library Led ή Buzzer (ή μεγαφωνάκι, ή...)
LAB 5 : Δημιουργία υπενθυμίσεων και επαναλαμβανόμενων διεργασιών Σενάριο : Θέλουμε να πραγματοποιήσουμε κάποια ενέργεια σε συγκεκριμένες μέρες και στιγμές.χρησιμοποιούμε την βοηθητική βιβλιοθήκη TimeAlarms η οποία εμπεριέχεται στην Time. Η βιβλιοθήκη αυτή μας βοηθά να δημιουργήσουμε ειδοποιήσεις (Alarms). Μπορούμε να δημιουργήσουμε ειδοποιήσεις ή ενέργειες οι οποίες εκτελούνται 1 φορά ή επαναλαμβάνονται ανα τακτά χρονικά διαστήματα τα οποία εμείς καθορίζουμε.
Κύκλωμα 5ου LAB (file lab5.ino) Δεν απαιτούνται πρόσθετα εξαρτήματα, διότι χρησιμοποιούμε εξαρτήματα που υπάρχουν πάνω στην πλακέτα.
Arduino κώδικας LAB 5 (file lab5.ino) #include <Time.h> #include <TimeAlarms.h> const int LED = 13; // LED connected to // digital pin 13 int count; void setup() pinmode(led, OUTPUT);// sets the digital // pin as output Serial.begin(9600);//set communication speed to 9600 bps Serial.println("TimeAlarms Example"); Serial.println("Alarms are triggered daily at 8:30 am and 17:45 pm"); Serial.println("One timer is triggered every 15 seconds"); Serial.println("Another timer is set to trigger only once after 10 seconds"); Serial.println(); settime(8,29,40,3,27,13); // set time to 8:29:40am Mar 27 2013 Alarm.alarmRepeat(8,30,0, MorningAlarm);// 8:30am every day Alarm.timerRepeat(15, RepeatTask);// timer for every 15 seconds Alarm.timerOnce(10, OnceOnlyTask);// called once after 10 seconds void MorningAlarm() Serial.println("Alarm: - turn lights off"); for (count = 0; count < 4; count++) delay(500); // waits for half a second digitalwrite(led, HIGH); // turns the LED on delay(500); // waits for half a second digitalwrite(led, LOW); // turns the LED off void loop() digitalclockdisplay(); Alarm.delay(1000); // wait one second between clock display void digitalclockdisplay() // digital clock display of the time Serial.print(hour()); printdigits(minute()); printdigits(second()); Serial.println(); // utility function for digital clock display: prints preceding // colon and leading 0. void printdigits(int digits) Serial.print(":"); if(digits < 10) Serial.print('0'); Serial.print(digits); void RepeatTask() Serial.println("15 second timer"); void OnceOnlyTask() Serial.println("This timer only triggers once");
Τέλος εργαστηρίου Ευχαριστούμε που μας παρακολουθήσατε