This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:arduino [2015/12/10 14:20] – kaupo.raid | en:arduino [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Arduino Examples ====== | ||
| + | /* | ||
| + | Palun täida koolituse lõpus ka | ||
| + | [[https:// | ||
| + | |||
| + | {{: | ||
| + | |||
| + | |||
| + | English: | ||
| + | {{: | ||
| + | */ | ||
| + | |||
| + | |||
| + | ====== References ====== | ||
| + | |||
| + | [[http:// | ||
| + | [[http:// | ||
| + | |||
| + | ====== Example 1 Digital I/O ====== | ||
| + | |||
| + | ===== Example 1.1 Push down button to light up LED ===== | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | // Constants | ||
| + | const int nupp = A0; // Viik kuhu on ühendatud nupp | ||
| + | const int LED = 13; // Viik kuhu on ühendatud nupp roheline LED | ||
| + | // Global variables | ||
| + | int NupuOlek = 0; // Button state | ||
| + | |||
| + | void setup() { | ||
| + | pinMode(LED, | ||
| + | pinMode(nupp, | ||
| + | digitalWrite(nupp, | ||
| + | } | ||
| + | |||
| + | void loop(){ | ||
| + | NupuOlek = digitalRead(nupp); | ||
| + | if (NupuOlek == HIGH) { // if button state is high (+5 V) | ||
| + | digitalWrite(LED, | ||
| + | } | ||
| + | else { // if button state is not high (GND) | ||
| + | digitalWrite(LED, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example 1.2 On button push LED turns of for 1 second ===== | ||
| + | <code c> | ||
| + | // First part of code copy from example #1.1 | ||
| + | void loop(){ | ||
| + | if (digitalRead(nupp) == LOW) { // if button state is high (+5V) | ||
| + | digitalWrite(LED, | ||
| + | delay(1000); | ||
| + | } | ||
| + | digitalWrite(LED, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example 1.3 LED turns on on button release ===== | ||
| + | <code c> | ||
| + | // First part of code copy from example #1.1 | ||
| + | void loop(){ | ||
| + | if (digitalRead(nupp) == LOW) { // if button pressed | ||
| + | while (digitalRead(nupp) == LOW){} // wait for button release | ||
| + | digitalWrite(LED, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Task #1.1 ===== | ||
| + | |||
| + | Modify example program so that LED blinks three times. | ||
| + | / | ||
| + | ===== Task #1.2 ===== | ||
| + | |||
| + | Modify example program so that on button press LED starts to blink with 1 second intervals. On second button press led stops blinking. | ||
| + | / | ||
| + | 0 %).*/ | ||
| + | |||
| + | ====== Example #2 Analog inputs ====== | ||
| + | |||
| + | ===== Example #2.1 When exceeding potentiometer threshold value the LED turns on ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | |||
| + | // Set threshold value at which the LED turns on | ||
| + | const int nivoo = 512; | ||
| + | |||
| + | int pote_sisend = A0; // set potentiometer input pin | ||
| + | int led = 13; // set LED pin | ||
| + | int pote = 0; // integer variable to hold potentiometer value | ||
| + | |||
| + | void setup() { | ||
| + | pinMode(led, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | pote = analogRead(pote_sisend); | ||
| + | // if value is greater than threshold then turn on LED | ||
| + | if (pote> | ||
| + | else digitalWrite(led, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example #2.2 LED blinking speed depends on potentiometer value ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | |||
| + | int pote_sisend = A0; // set potentiometer pin | ||
| + | int led = 13; // set LED pin | ||
| + | int pote = 0; // integer variable to hold potentiometer value | ||
| + | |||
| + | void setup() { | ||
| + | pinMode(led, | ||
| + | } | ||
| + | void loop() { | ||
| + | pote = analogRead(pote_sisend); | ||
| + | digitalWrite(led, | ||
| + | delay(pote); | ||
| + | digitalWrite(led, | ||
| + | delay(pote); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Task #2 ===== | ||
| + | |||
| + | Modify example program so that its possible to change LED brifgtness with potentiometer. Use delay functions. | ||
| + | / | ||
| + | |||
| + | ====== Example #3 LCD ====== | ||
| + | |||
| + | Connect LCD shield to Arduino board. Before attaching remove all connections from the board. | ||
| + | |||
| + | ===== Example #3.1 LCD using ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | |||
| + | #include < | ||
| + | |||
| + | // Initialize LCD | ||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | |||
| + | void setup() { | ||
| + | lcd.begin(16, | ||
| + | lcd.print(" | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | | ||
| + | lcd.setCursor(0, | ||
| + | | ||
| + | lcd.print(millis()/ | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example #3.2 LCD and analog inputs ===== | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | #include < | ||
| + | |||
| + | // Initialize LCD | ||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | int pote_sisend = A1; // set potentiometer pin | ||
| + | |||
| + | void setup() { | ||
| + | lcd.begin(16, | ||
| + | lcd.print(" | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(analogRead(pote_sisend)); | ||
| + | lcd.print(" | ||
| + | delay (100); | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Task #3.1 ===== | ||
| + | |||
| + | Make a program that on button press changes LCD row. Potentiometer value has to be printed only on the new row. | ||
| + | /*Koostada programm, mis võimaldab nupuvajutusega vahetada LCD rida, kuhu trükitakse potentsiomeetri väärtus.*/ | ||
| + | |||
| + | |||
| + | |||
| + | ====== Example 4 Sensors and LCD ====== | ||
| + | |||
| + | ===== Example #4.1 Temperature sensor with math library ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | |||
| + | /* | ||
| + | Nimetus: Näide #4.1 Temperatuuri andur koos matemaatika teegiga | ||
| + | |||
| + | |||
| + | Kirjeldus: | ||
| + | Kasutab Steinhart-Hart termistori valemit: | ||
| + | temperatuur kelvinites = 1 / {A + B[ln(R)] + C[ln(R)]^3} | ||
| + | kus A = 0.001129148, | ||
| + | Autor: Milan Malesevic and Zoran Stupic | ||
| + | Modifikatsioon: | ||
| + | Kuupäev: 15.01.2013/ | ||
| + | Versioon: 1.1 | ||
| + | */ | ||
| + | |||
| + | // Kaasame vajalikud teegid | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Initsialiseerime LCD koos vastavate viikude ühendamisega | ||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | int andur = A1; // set thermistor input pin | ||
| + | |||
| + | void setup() { | ||
| + | lcd.begin(16, | ||
| + | lcd.print(" | ||
| + | delay (1000); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | void Termistor(int RawADC) { | ||
| + | | ||
| + | long Takistus; | ||
| + | // Valem: Takistus = (1024 * JaguriTakisti/ | ||
| + | Takistus=((10240000/ | ||
| + | |||
| + | //Esimene rida | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | lcd.print(RawADC); | ||
| + | |||
| + | lcd.setCursor(8, | ||
| + | lcd.print(" | ||
| + | lcd.print(((RawADC*5.0)/ | ||
| + | |||
| + | |||
| + | //Teine rida | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | lcd.print(Takistus); | ||
| + | |||
| + | Temp = log(((10240000/ | ||
| + | Temp = 1/ | ||
| + | Temp = Temp - 273.15; // Konverteeri Kelvinid Celciustesse | ||
| + | | ||
| + | lcd.setCursor(8, | ||
| + | lcd.print(" | ||
| + | lcd.print(Temp); | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Näide #4.2 Kaugusandurid ===== | ||
| + | |||
| + | <code c> | ||
| + | // Reading analog and digital sensors | ||
| + | void setup() | ||
| + | { | ||
| + | // For visualization we can use serial monitor | ||
| + | Serial.begin(9600); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | // Read the input on analog pin 2 and 3 | ||
| + | int AnalogSensorValue = analogRead(A2); | ||
| + | int DigitalSensorValue = digitalRead(A3); | ||
| + | | ||
| + | Serial.println(AnalogSensorValue); | ||
| + | Serial.println(DigitalSensorValue); | ||
| + | | ||
| + | delay(500); // Delay in between readings for readability | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Harjutus #4.1 ===== | ||
| + | Modifitseerida programmi nii, et potentsiomeetriga määratakse temperatuuri nivoo, mis salvestatakse nupuga programmi mällu ja selle nivoo hilisemal ületamisel käivitatakse täiturmehhanism (milleks on LED). | ||
| + | **Extra:** | ||
| + | Kombineerida LED-i heleduse programm ja eelnev temperatuuri juhtimise programm, kus LED-i heledus sõltub sellest, kui palju nivootemperatuuri ületati (1 kraad: heledus 25 %, 2 kraadi: heledus 50 %, 3 kraadi: heledus 75 % ja 5 kraadi: heledus 10 | ||
| + | |||
| + | ===== Harjutus #4.2 ===== | ||
| + | Modify distance sensor example so that it would print values on the LCD in centimeters (+-5cm accuracy). If no measureing tool is available then use the sensor datasheet graph. | ||
| + | |||
| + | ====== Näide #5 Mootor ====== | ||
| + | |||
| + | Disconnect the LCD shield before using motors. Before disconnecting remove all connections from shield and Arduino board. | ||
| + | |||
| + | ===== Näide #5.1 Servomootor ===== | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | Servo right_motor, | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | right_motor.attach(11); | ||
| + | left_motor.attach(12); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | // Control servo with value 0-180. 90 means that servo stands still | ||
| + | right_motor.write(0); | ||
| + | left_motor.write(180); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Näide #5.2 Potentsiomeetriga juhitav servomootor ===== | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | Servo right_motor, | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | while (digitalRead(10)==1){}; | ||
| + | right_motor.attach(11); | ||
| + | left_motor.attach(12); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | int AnalogSensorValue = analogRead(A0); | ||
| + | // Compare it with reference | ||
| + | if (AnalogSensorValue> | ||
| + | | ||
| + | | ||
| + | } | ||
| + | right_motor.write(0); | ||
| + | left_motor.write(180); | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Harjutus #5.1 ===== | ||
| + | Luua programm, mis võimaldab servomootori positsiooni muuta vastavalt analoog kaugusanduri väärtusele (kasutada map funktsiooni). | ||
| + | |||
| + | ===== Harjutus #5.2 ===== | ||
| + | Luua programm, mis servomootori baasil ehitatud radariga ja analoog kaugusanduriga leiaks ruumist lähima punkti (objekti otsimine tühjast ruumist). | ||
| + | |||
| + | ====== Näide 6 Kommunikatsioon ====== | ||
| + | |||
| + | ===== Näide #6.1 Jadaliides ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Nimetus: Näide #6.1 Jadaliides | ||
| + | */ | ||
| + | #include < | ||
| + | int NTC_sisend = A2; // määrame temperatuurianduri sisendviigu | ||
| + | void setup() { | ||
| + | | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | double Termistor(int RawADC) { | ||
| + | | ||
| + | long Takistus; | ||
| + | // Valem: Takistus = (1024 * JaguriTakisti/ | ||
| + | Takistus=((10240000/ | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(RawADC); | ||
| + | Serial.print("/ | ||
| + | Serial.print(", | ||
| + | Serial.print(((RawADC*5.0)/ | ||
| + | Serial.print(" | ||
| + | Serial.print(", | ||
| + | Serial.print(Takistus); | ||
| + | Serial.print(" | ||
| + | |||
| + | Temp = log(((10240000/ | ||
| + | Temp = 1/ | ||
| + | Temp = Temp - 273.15; // Konverteeri Kelvinid Celciustesse | ||
| + | Serial.print(", | ||
| + | Serial.print(Temp); | ||
| + | Serial.print(" | ||
| + | return Temp; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Harjutus #6 ===== | ||
| + | |||
| + | Luua programm, mis jadaliidesest saadetud teksti kirjutaks LCD ekraanile. Märk „+“ vahetab rida ning märk „-“ kustutab kogu ekraani. | ||
| + | |||
| + | |||
| + | ====== Praktiline projekt #1 ====== | ||
| + | |||
| + | Luua programm, mis temperatuurinivoo ületamisel muudab õhuklapi asendit (servo mootor). Temperatuurinivoo määratakse potentsiomeetriga. Õhuklapi asend sõltub temperatuuri erinevusest. Iga temperatuuri kraad, mis ületab nivood lisab klapi nurga 25%. Lüliti katkestab igal ajahetkel süsteemi töö (hädastop) ja viib klapi algasendisse tagasi, jättes punase LED-i vilkuma. Edasijõudnud võivad kasutada katkestust. | ||
| + | ====== Praktiline projekt #2 ====== | ||
| + | |||
| + | Ventilaatori juhtimine XRF kaudu. | ||
| + | Luua programm, mis suudab XRF raadiomooduliga lugeda kaugmoodulilt temperatuuri ning kui temperatur ületab kohalikul kontrolleril seatud nivoo käivitatakse kaugmoodulil asetsev ventilaator. Suhtkuseks kaugmooduliga tuleb kasutada allolevat protokolli: \\ | ||
| + | * xTEMP – tagastab kümnekordse temperatuuri Celsiuse kraadides (24,5C puhul saadetakse 245) | ||
| + | * vastus: xNNN | ||
| + | * xFANn – FAN1 käivitab ventilaatori, | ||
| + | * vastus xFANn | ||
| + | |||
| + | Protokollis tähistab „x“ kohaliku seadme numbrit, mille annab juhendaja igale tiimile erineva. | ||