This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:arduino:examples [2017/07/25 05:08] – kaupo.raid | en:arduino:examples [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | TTU Summer School programming | + | ========= |
| - | ====== | + | ====== |
| {{: | {{: | ||
| - | ===== Näide | + | ===== Example |
| - | [[https:// | + | Theory: |
| <code c> | <code c> | ||
| /* | /* | ||
| - | Nimetus: Näide | + | Example |
| - | Kirjeldus: Nuppu all hoides LED põleb | + | |
| */ | */ | ||
| - | /* Konstandid | + | /* Constants |
| - | // Viik kuhu on ühendatud nupp | + | // Set button pin |
| - | const int nupp = A0; | + | const int button |
| - | // Viik kuhu on ühendatud | + | // Set LED pin |
| const int led = 13; | const int led = 13; | ||
| - | /* Globaalsed muutujad | + | /* Global variables |
| - | // Nupu muutuja oleku salvestamine ja selle algväärtustamine | + | // Holds the current state of button |
| - | int nupuOlek | + | int buttonState |
| void setup() | void setup() | ||
| { | { | ||
| - | // LED viigu väljundi seadistamine | + | // Set LED pin to output |
| pinMode(led, | pinMode(led, | ||
| - | // Nupu viigu sisendi seadistamine ja sisemise "pull-up" takisti aktiveermine | + | // Set button pin to input with internal |
| - | pinMode(nupp, INPUT_PULLUP); | + | pinMode(button, INPUT_PULLUP); |
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // Nupu muutuja hetkeväärtuse salvestamine | + | // Get the curren state of button pin |
| - | | + | |
| - | // Kui nupp on alla vajutatud, siis seadistame | + | // If button is pressed then turn on LED |
| - | if (nupuOlek | + | if (buttonState |
| { | { | ||
| digitalWrite(led, | digitalWrite(led, | ||
| } | } | ||
| - | // Muul juhul seadistame | + | // All other cases turn LED off |
| else | else | ||
| - | { digitalWrite(led, | + | { |
| + | digitalWrite(led, | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | ===== Näide | + | ===== Example |
| <code c> | <code c> | ||
| /* | /* | ||
| - | Nimetus: Näide | + | Example |
| - | Kirjeldus: Nupule vajutades süttib LED 1 sekundiks | + | |
| */ | */ | ||
| - | // Algus identne näitega | + | // Setup is same with example |
| - | const int nupp = A0; | + | const int button |
| const int led = 13; | const int led = 13; | ||
| - | int nupuOlek | + | int buttonState |
| void setup() | void setup() | ||
| { | { | ||
| pinMode(led, | pinMode(led, | ||
| - | pinMode(nupp, INPUT_PULLUP); | + | pinMode(button, INPUT_PULLUP); |
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // Nupu muutuja hetkeväärtuse salvestamine | + | // Get the curren state of button pin |
| - | | + | |
| | | ||
| - | /* Kui nupp on alla vajutatud, siis seadistame LED viigu kõrgeks ehk LED süttib, | + | // When button is pressed then set LED on and halt program for 1 second |
| - | | + | if (buttonState |
| - | if (nupuOlek | + | |
| { | { | ||
| digitalWrite(led, | digitalWrite(led, | ||
| delay(1000); | delay(1000); | ||
| - | } | + | } |
| + | // Turn LED off | ||
| digitalWrite(led, | digitalWrite(led, | ||
| } | } | ||
| </ | </ | ||
| - | ===== Näide | + | ===== Example |
| <code c> | <code c> | ||
| /* | /* | ||
| - | Nimetus: Näide | + | Example |
| - | Kirjeldus: Nupule vajutades muudab LED oma olekut püsivalt | + | |
| */ | */ | ||
| - | // Algus identne näitega | + | // Setup is same with example |
| - | const int nupp = A0; | + | const int button |
| const int led = 13; | const int led = 13; | ||
| - | int nupuOlek | + | int buttonState |
| void setup() | void setup() | ||
| { | { | ||
| pinMode(led, | pinMode(led, | ||
| - | pinMode(nupp, INPUT_PULLUP); | + | pinMode(button, INPUT_PULLUP); |
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // Programm ei tee midagi kuni vajutatakse nuppu | + | // When button is pressed and hold the program stays inside while(1) loop |
| - | if (digitalRead(nupp) == LOW) | + | if (digitalRead(button) == LOW) |
| { | { | ||
| - | // | + | // |
| delay(50); | delay(50); | ||
| - | while (digitalRead(nupp) == LOW) | + | while (digitalRead(button) == LOW) |
| - | | + | |
| } | } | ||
| - | | + | // Invert LED current state |
| - | | + | |
| digitalWrite(led, | digitalWrite(led, | ||
| | | ||
| - | // | + | // |
| delay(50); | delay(50); | ||
| } | } | ||
| Line 128: | Line 125: | ||
| - | ===== Harjutused | + | ===== Exercises |
| - | === Harjutus | + | === Exercise |
| - | Modifitseerida näiteprogrammi nii, et nupule vajutades vilgub | + | Modify example so that on button press LED blinks 3 times. |
| - | === Harjutus | + | === Exercise #1.2 === |
| - | Modifitseerida näiteprogrammi nii, et nupule vajutades hakkab | + | Modify example so that on button press LED starts to blink with constant interval. Second press ends the blinking. |
| - | === Harjutus | + | |
| - | Modifitseerida näiteprogrammi nii, et nupule vajutades LED vilgub nii mitu korda, kui on nuppu programmi töötamise jooksul kokku vajutatud. Pärast igat vajutust suureneb vilkumiste arv ühe võrra. | + | |
| + | |||
| + | ====== Project 2 Controlling LED with potentiometer | ||
| + | |||
| + | {{:et: | ||
| + | |||
| + | ===== Example #2.1 Potentiometer controls state of LED ===== | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #2.1 | ||
| + | */ | ||
| + | |||
| + | // Set potentiometer pin | ||
| + | const int pot = A1; | ||
| + | |||
| + | // Set LED pin | ||
| + | const int led = 13; | ||
| + | |||
| + | // Holds potentiometer digital value | ||
| + | int potState | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Set LED pin to output | ||
| + | pinMode(led, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | // Get digital value of potentiometer | ||
| + | potState = digitalRead(pot); | ||
| + | |||
| + | // When potentiometer digital value is logical " | ||
| + | if(potState > 0) | ||
| + | { | ||
| + | digitalWrite(led, | ||
| + | } | ||
| + | // All other cases set LED off | ||
| + | else | ||
| + | { | ||
| + | digitalWrite(led, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Example | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #2.2 | ||
| + | */ | ||
| + | |||
| + | // Setup is same with example #2.1 | ||
| + | const int pot = A1; | ||
| + | const int led = 13; | ||
| + | |||
| + | int potState = 0; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | pinMode(led, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | // Get analog value of potentiometer | ||
| + | potState = analogRead(pot); | ||
| + | |||
| + | // Turn LED on | ||
| + | digitalWrite(led, | ||
| + | |||
| + | // Halt the program for potentiometer analog value number of milliseconds | ||
| + | delay(potState); | ||
| + | |||
| + | // Turn LED off | ||
| + | digitalWrite(led, | ||
| + | |||
| + | // Halt the program for potentiometer analog value number of milliseconds | ||
| + | delay(potState); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example #2.3 Controlling LED brightness with potentiometer ===== | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #2.3 | ||
| + | */ | ||
| + | // Setup is same with example #2.1 | ||
| + | |||
| + | const int pot = A1; | ||
| + | const int led = 13; | ||
| + | int potState | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | pinMode(led, OUTPUT); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | // Get analog value of potentiometer | ||
| + | potState = analogRead(pot); | ||
| + | |||
| + | // When potentiometer value is greater than 0 then set LED on halt program for short period | ||
| + | if (potState > 0) | ||
| + | { | ||
| + | digitalWrite(led, | ||
| + | delayMicroseconds(potState); | ||
| + | } | ||
| + | |||
| + | // Turn LED off | ||
| + | digitalWrite(led, | ||
| + | |||
| + | // Halt program for short period | ||
| + | delayMicroseconds(1023 - potState); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Exercises ===== | ||
| + | === Exercise #2.2 === | ||
| + | Control LED on and off time with potentiometer. | ||
| + | |||
| + | ====== Project 3 Alphabetical LCD ====== | ||
| + | |||
| + | |||
| + | ===== Example #3.1 Writing on LCD screen ===== | ||
| + | |||
| + | Theory: https:// | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #3.1 | ||
| + | */ | ||
| + | // Include LCD library | ||
| + | #include < | ||
| + | |||
| + | // Create LCD object and set hardware connection pins | ||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Define LCD size | ||
| + | lcd.begin(16, | ||
| + | |||
| + | // Print out welcome message | ||
| + | lcd.print(" | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | // Change cursor position to second line | ||
| + | lcd.setCursor(0, | ||
| + | |||
| + | // Print out program working time in seconds | ||
| + | lcd.print(millis()/ | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Example | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #3.2 | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | |||
| + | // Array to generate new character, | ||
| + | // " | ||
| + | byte customChar[8] = | ||
| + | { | ||
| + | 0b11111, | ||
| + | 0b00000, | ||
| + | 0b01010, | ||
| + | 0b00000, | ||
| + | 0b10001, | ||
| + | 0b01110, | ||
| + | 0b00000, | ||
| + | 0b11111 | ||
| + | }; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Load new character to LCD memory position " | ||
| + | lcd.createChar(0, | ||
| + | |||
| + | // Define LCD size | ||
| + | lcd.begin(16, | ||
| + | |||
| + | // Display new custom character on LCD | ||
| + | lcd.write((uint8_t)0); | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | // Do nothing | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example #3.3 Reading LCD shield buttons ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Nimetus: Example #3.3 | ||
| + | Kirjeldus: Reading LCD shield buttons | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | |||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | |||
| + | // Define button states constants | ||
| + | const int RIGHT = 0; | ||
| + | const int UP = 1; | ||
| + | const int DOWN = 2; | ||
| + | const int LEFT = 3; | ||
| + | const int SELECT = 4; | ||
| + | const int NONE = 5; | ||
| + | |||
| + | /* Global variables */ | ||
| + | // Holds the value of pressed button | ||
| + | int pressedButton; | ||
| + | // Holds the value of analog input | ||
| + | int buttonValue; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Define LCD size | ||
| + | lcd.begin(16, 2); | ||
| + | |||
| + | // Print explaining text | ||
| + | lcd.print(" | ||
| + | |||
| + | // Print out analog input value | ||
| + | lcd.print(analogRead(A0)); | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | // Calls out fucntion and saves returned value to variable | ||
| + | pressedButton = checkButtons(); | ||
| + | |||
| + | // Check if there is a pressed button | ||
| + | if(pressedButton < NONE) | ||
| + | { | ||
| + | lcd.clear(); | ||
| + | |||
| + | lcd.print(" | ||
| + | |||
| + | lcd.print(buttonValue); | ||
| + | |||
| + | delay(500); // Halts program for 500ms to get stable text on LCD | ||
| + | } | ||
| + | } | ||
| + | // Function read analog input value and compares it with values of button states | ||
| + | // Tagastab arvu vahemikus 0 kuni 5 vastavalt defineeritud konstandile | ||
| + | int checkButtons() | ||
| + | { | ||
| + | // Get analog input value | ||
| + | | ||
| + | |||
| + | if (buttonValue < 50) | ||
| + | if (buttonValue < 195) return UP; | ||
| + | if (buttonValue < 380) return DOWN; | ||
| + | if (buttonValue < 555) return LEFT; | ||
| + | if (buttonValue < 790) return SELECT; | ||
| + | // If no button state matches then return NONE | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Exercises ===== | ||
| + | |||
| + | === Exercise #3.1 === | ||
| + | Modify example so that text is center aligned. | ||
| + | |||
| + | === Exercise #3.2 === | ||
| + | Make a small animation with custom characters. | ||
| + | Custom character code generator [[https:// | ||
| + | |||
| + | |||
| + | |||
| + | ====== Project 4 Reading sensors and displaying results on LCD | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== Example #4.2 Distance and proximity sensors reading ===== | ||
| + | Libraries: [[https:// | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #4.2 | ||
| + | */ | ||
| + | |||
| + | // Include libraries | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Set pins for sensors | ||
| + | const int US_TRIGGER = A2; | ||
| + | const int US_ECHO = A3; | ||
| + | const int IR = A4; | ||
| + | |||
| + | /* Global variables and constants */ | ||
| + | const int maxDistance = 200; | ||
| + | int distance, proximity; | ||
| + | |||
| + | // Create LCD object and define connection pins | ||
| + | LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | ||
| + | |||
| + | // Create ultrasonic object and define connection pins | ||
| + | NewPing sonar(US_TRIGGER, | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | //Define LCD size | ||
| + | lcd.begin(16, | ||
| + | |||
| + | // Print out explaining text | ||
| + | lcd.print(" | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | // Get distance in centimeters | ||
| + | distance = sonar.ping_cm(); | ||
| + | |||
| + | // Get proximity of object | ||
| + | proximity = digitalRead(IR); | ||
| + | |||
| + | // Set cursor to first line 10th character | ||
| + | lcd.setCursor(9, | ||
| + | |||
| + | // EPrint out distance | ||
| + | lcd.print(distance); | ||
| + | |||
| + | // Overwrite previous value numbers with spaces | ||
| + | lcd.print(" | ||
| + | |||
| + | // Set cursor on second line 10th character | ||
| + | lcd.setCursor(9, | ||
| + | |||
| + | // when infrared sensor does not see anything print out " | ||
| + | if(proximity == 1) lcd.print(" | ||
| + | else lcd.print(" | ||
| + | |||
| + | // Halt program to get stable view of LCD | ||
| + | delay(500); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Exercises ===== | ||
| + | === Exercise #4.1 === | ||
| + | Find out which sensor sample time is faster. | ||
| + | |||
| + | |||
| + | ====== Project 5 DC motor drive and speed control ====== | ||
| + | |||
| + | {{:et: | ||
| + | |||
| + | ===== Example #5.1 DC motor ad H-bridge ===== | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Example #5.1 | ||
| + | */ | ||
| + | // Set pins for driver inputs | ||
| + | const int left_A = 10; // driver pin A-1A | ||
| + | const int left_B = 12; // driver pin A-1B | ||
| + | const int right_A = 11; // driver pin B-1A | ||
| + | const int right_B = 13; // driver pin B-2A | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Set driver control pins as output | ||
| + | pinMode(left_A,OUTPUT); | ||
| + | pinMode(left_B, | ||
| + | pinMode(right_A, | ||
| + | pinMode(right_B, | ||
| + | |||
| + | motors(0, | ||
| + | delay(2000); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | |||
| + | motors(1, | ||
| + | delay(2000); | ||
| + | |||
| + | motors(-1, | ||
| + | delay(500); | ||
| + | } | ||
| + | |||
| + | // This function controls the driver input pins to get correct motion | ||
| + | void motors(int left, int right) | ||
| + | { | ||
| + | // Left Motor | ||
| + | if(left == 1) | ||
| + | { // Left motor CW | ||
| + | digitalWrite(left_A, | ||
| + | digitalWrite(left_B, | ||
| + | } | ||
| + | else if(left == -1) | ||
| + | { // Left motor CCW | ||
| + | digitalWrite(left_A, | ||
| + | digitalWrite(left_B, | ||
| + | } | ||
| + | else | ||
| + | { // Left motor STOP | ||
| + | digitalWrite(left_A, | ||
| + | digitalWrite(left_B, | ||
| + | } | ||
| + | |||
| + | // Right motor | ||
| + | if(right == 1) | ||
| + | { // Right motor CW | ||
| + | digitalWrite(right_A, | ||
| + | digitalWrite(right_B, | ||
| + | } | ||
| + | else if(right == -1) | ||
| + | { // Right motor CCW | ||
| + | digitalWrite(right_A, | ||
| + | digitalWrite(right_B, | ||
| + | } | ||
| + | else | ||
| + | { // Right motor STOP | ||
| + | digitalWrite(right_A, | ||
| + | digitalWrite(right_B, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Example #5.2 DC motor speed controlling ===== | ||
| + | |||
| + | <code c> | ||
| + | /* Nimetus: | ||
| + | Example #5.2 | ||
| + | */ | ||
| + | // Set pins for driver inputs | ||
| + | const int left_A = 10; // driver pin A-1A | ||
| + | const int left_B = 12; // driver pin A-1B | ||
| + | const int right_A = 11; // driver pin B-1A | ||
| + | const int right_B = 13; // driver pin B-2B | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Set driver control pins as output | ||
| + | pinMode(left_A, | ||
| + | pinMode(left_B, | ||
| + | pinMode(right_A, | ||
| + | pinMode(right_B, | ||
| + | |||
| + | motors(0, | ||
| + | delay(2000); | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | motors(100, | ||
| + | delay(2000); | ||
| + | motors(100, | ||
| + | delay(2000); | ||
| + | motors(-255, | ||
| + | delay(2000); | ||
| + | motors(-255, | ||
| + | delay(2000); | ||
| + | motors(150, | ||
| + | delay(2000); | ||
| + | } | ||
| + | |||
| + | // This function sets the motors speed and directions | ||
| + | void motors(int left, int right) | ||
| + | { | ||
| + | // Left motor | ||
| + | if(left > 0 && left <= 255) | ||
| + | { // Left motor CW | ||
| + | analogWrite(left_A, | ||
| + | digitalWrite(left_B, | ||
| + | } | ||
| + | else if(left < 0 && left >= -255) | ||
| + | { // Left motor CCW | ||
| + | analogWrite(left_A, | ||
| + | digitalWrite(left_B, | ||
| + | } | ||
| + | else | ||
| + | { // Left motor STOP | ||
| + | digitalWrite(left_A, | ||
| + | digitalWrite(left_B, | ||
| + | } | ||
| + | |||
| + | // Right motor | ||
| + | if(right > 0 && right <= 255) | ||
| + | { // Right motor CW | ||
| + | analogWrite(right_A, | ||
| + | digitalWrite(right_B, | ||
| + | } | ||
| + | else if(right < 0 && right >= -255) | ||
| + | { // Right motor CCW | ||
| + | analogWrite(right_A, | ||
| + | digitalWrite(right_B, | ||
| + | } | ||
| + | else | ||
| + | { // Right motor STOP | ||
| + | digitalWrite(right_A, | ||
| + | digitalWrite(right_B, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Exercises ===== | ||
| + | |||
| + | ==== Exercise #5.1 ==== | ||
| + | Modify exampe so that robot drives number eight shape. | ||
| + | |||
| + | ==== Harjutus #5.2 ==== | ||
| + | Make robot parallel park itself. | ||