Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:arduino:examples [2017/07/25 05:10] kaupo.raiden:arduino:examples [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-TTU Summer School programming+========= TTU Summer School programming =========
  
  
-====== Projekt 1 LED-i juhtimine nupuga  ======+====== Project Controlling LED with button  ======
  
 {{:et:arduino:buttons:projekt1.jpg?350|Arduino ühendusskeem}} {{:et:arduino:buttons:projekt1.jpg?350|Arduino ühendusskeem}}
  
-===== Näide #1.1 Nuppu all hoides LED põleb =====+===== Example #1.1 LED light up when button is pressed =====
  
-[[https://www.arduino.cc/en/Tutorial/DigitalPins|]].+Theory: [[https://www.arduino.cc/en/Tutorial/DigitalPins|]].
  
 <code c> <code c>
 /* /*
-Nimetus: Näide #1.1 +Example #1.1
-Kirjeldus: Nuppu all hoides LED põleb +
 */ */
  
-/* Konstandid */ +/* Constants */ 
-// Viik kuhu on ühendatud nupp +// Set button pin 
-const int nupp = A0; +const int button = A0; 
  
-// Viik kuhu on ühendatud LED+// 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 = 0; +int buttonState = 0; 
  
 void setup()  void setup() 
  
-  // LED viigu väljundi seadistamine +  // Set LED pin to output
   pinMode(led, OUTPUT);    pinMode(led, OUTPUT); 
-  // Nupu viigu sisendi seadistamine ja sisemise "pull-up" takisti aktiveermine +  // Set button pin to input with internal pull-up activated 
-  pinMode(nupp, INPUT_PULLUP); +  pinMode(button, INPUT_PULLUP); 
 } }
  
 void loop() void loop()
 { {
-  // Nupu muutuja hetkeväärtuse salvestamine +  // Get the curren state of button pin 
-  nupuOlek = digitalRead(nupp);  +  buttonState = digitalRead(button);  
-  // Kui nupp on alla vajutatud, siis seadistame LED viigu kõrgeks ehk LED süttib +  // If button is pressed then turn on LED 
-  if (nupuOlek == LOW) +  if (buttonState == LOW) 
   {    { 
     digitalWrite(led, HIGH);     digitalWrite(led, HIGH);
   }    } 
-  // Muul juhul seadistame LED viigu madalaks ehk LED ei põle+  // All other cases turn LED off
   else    else 
-  { digitalWrite(led, LOW);+  {  
 +   digitalWrite(led, LOW);
   }   }
 } }
 </code> </code>
  
-===== Näide #1.2 Nupule vajutades LED süttib sekundiks =====+===== Example #1.2 Short push on the button makes LED light up for second =====
  
 <code c> <code c>
 /* /*
-Nimetus: Näide #1.2 +Example #1.2
-Kirjeldus: Nupule vajutades süttib LED 1 sekundiks+
 */ */
  
-// Algus identne näitega #1.1 (kommenteeritud koodi vaata sealt) +// Setup is same with example #1.1 
-const int nupp = A0; +const int button = A0; 
 const int led = 13;  const int led = 13; 
  
-int nupuOlek = 0; +int buttonState = 0; 
  
 void setup()  void setup() 
  
   pinMode(led, OUTPUT);    pinMode(led, OUTPUT); 
-  pinMode(nupp, INPUT_PULLUP); +  pinMode(button, INPUT_PULLUP); 
 } }
  
 void loop() void loop()
 { {
-  // Nupu muutuja hetkeväärtuse salvestamine +  //  Get the curren state of button pin 
-  nupuOlek = digitalRead(nupp); +  buttonState = digitalRead(button); 
      
-  /* 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 second 
-     programm seisab sekund ja pärast seda seatakse viik tagasi madalaks */ +  if (buttonState == LOW) 
-  if (nupuOlek == LOW) +
   {    { 
     digitalWrite(led, HIGH);     digitalWrite(led, HIGH);
     delay(1000); // 1000 millisekundit ehk 1 sekund     delay(1000); // 1000 millisekundit ehk 1 sekund
-  } +  } 
 +  // Turn LED off
   digitalWrite(led, LOW);   digitalWrite(led, LOW);
 } }
 </code> </code>
  
-===== Näide #1.3 Nupule vajutades muudab LED oma olekut =====+===== Example #1.3 Short push on the button changes LED state =====
  
 <code c> <code c>
 /* /*
-  Nimetus: Näide #1.3 +Example #1.3
-  Kirjeldus: Nupule vajutades muudab LED oma olekut püsivalt+
 */ */
  
-// Algus identne näitega #1.1 (kommenteeritud koodi vaata sealt) +// Setup is same with example #1.1 
-const int nupp = A0; +const int button = A0; 
 const int led = 13;  const int led = 13; 
  
-int nupuOlek = 0; +int buttonState = 0; 
  
 void setup()  void setup() 
  
   pinMode(led, OUTPUT);    pinMode(led, OUTPUT); 
-  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) 
     {     {
-   // Lüliti kontaktide värelemise (debounce) efekti mõju vähendamiseks väike viide+   // Debounce effect eliminator delay
     delay(50);      delay(50); 
-    while (digitalRead(nupp) == LOW) +    while (digitalRead(button) == LOW) 
-     //Tsükkel hoiab programmi kinni kuni nupu viigu olek muutub kõrgeks+     
      }      }
-    /* LED viigu olek inverteeritakse ehk seatakse + // Invert LED current state
-       vastupidiseks kasutades hetkeväärtust */+
     digitalWrite(led, !digitalRead(led));      digitalWrite(led, !digitalRead(led)); 
          
-   // Lüliti kontaktide värelemise (debounce) efekti mõju vähendamiseks väike viide+   // Debounce effect eliminator delay
     delay(50);      delay(50); 
     }     }
Line 128: Line 125:
  
  
-===== Harjutused ===== +===== Exercises ===== 
-=== Harjutus #1.1 === +=== Exercise #1.1 === 
-Modifitseerida näiteprogrammi nii, et nupule vajutades vilgub LED kolm korda+Modify example so that on button press LED blinks 3 times
-=== Harjutus #1.2 === +=== Exercise #1.2 === 
-Modifitseerida näiteprogrammi nii, et nupule vajutades hakkab LED konstantselt vilkuma 1 sekundilise intervalliga ja teine nupule vajutus lõpetab vilkumise. +Modify example so that on button press LED starts to blink with constant intervalSecond press ends the blinking.
-=== Harjutus #1.3 === +
-Modifitseerida näiteprogrammi nii, et nupule vajutades LED vilgub nii mitu korda, kui on nuppu programmi töötamise jooksul kokku vajutatudPärast igat vajutust suureneb vilkumiste arv ühe võrra.+
  
  
  
-~~PB~~ +====== Project Controlling LED with potentiometer   ======
-====== Projekt 2 LED-i juhtimine potentsiomeetriga   ======+
  
 {{:et:arduino:buttons:projekt2.jpg?400|}} {{:et:arduino:buttons:projekt2.jpg?400|}}
  
-===== Näide #2.1 Potentsiomeetri pööramisel üle nivoo süttib LED =====+===== Example #2.1 Potentiometer controls state of LED =====
  
 <code c> <code c>
 /* /*
-Nimetus: Näide #2.1 +Example #2.1
-Kirjeldus: Potentsiomeetri pööramisel üle nivoo süttib LED+
 */ */
  
-// Viik kuhu on ühendatud potentsiomeeter +// Set potentiometer pin 
-const int pote = A1; +const int pot = A1; 
  
-// Viik kuhu on ühendatud LED+// Set LED pin
 const int led = 13;  const int led = 13; 
  
-// Potentsiomeetri muutuja väärtuse salvestamine ja selle algväärtustamine +// Holds potentiometer digital value 
-int poteOlek = 0; +int potState = 0; 
    
 void setup() void setup()
 { {
-  /* Mikrokontrolleri viigud on tavaolekus sisendid ja seega ei pea +  // Set LED pin to output 
-  potentsiomeetri sisendit eraldi seadistama *+  pinMode(led, OUTPUT);
-  pinMode(led, OUTPUT); // Seadistame LED viigu väljundiks+
 } }
  
 void loop()  void loop() 
 { {
-  // Viigu muutuja hetkeväärtuse salvestamine +  // Get digital value of potentiometer 
-  poteOlek = digitalRead(pote); +  potState = digitalRead(pot);
      
-  // Kui sisendviik on kõrge, siis seame LED viigu kõrgeks +  // When potentiometer digital value is logical "true" then turn on LED 
-  if(poteOlek > 0)+  if(potState > 0)
   {   {
     digitalWrite(led, HIGH);     digitalWrite(led, HIGH);
   }   }
-  // Muul juhul seame led viigu madalaks+  // All other cases set LED off
   else   else
   {   {
Line 186: Line 178:
  
  
-===== Näide #2.2 LED-i vilkumise sagedus sõltub potentsiomeetri asendist =====+===== Example #2.2 Controlling LED blinking frequency with potentiometer =====
  
 <code c> <code c>
-/* Nimetus: Näide #2.2 +/*  
-   Kirjeldus: LED-i vilkumise sagedus sõltub potentsiomeetri asendist+Example #2.2
 */ */
-// Algus identne näitega #2.1 (kommenteeritud koodi vaata sealt) + 
-const int pote = A1; +// Setup is same with example #2.1 
 +const int pot = A1; 
 const int led = 13;  const int led = 13; 
  
-int poteOlek = 0; +int potState = 0; 
    
 void setup() void setup()
Line 205: Line 198:
 void loop()  void loop() 
 { {
-  // Potentsiomeetri muutuja hetkeväärtuse salvestamine  +  // Get analog value of potentiometer 
-  poteOlek = analogRead(pote); +  potState = analogRead(pot); 
      
-  // LED viigu oleku kõrgeks seadmine+  // Turn LED on
   digitalWrite(led, HIGH);    digitalWrite(led, HIGH); 
      
-  // Viite tekitamine, mille pikkus on võrdne potentsiomeetri sisendi väärtusega +  // Halt the program for potentiometer analog value number of milliseconds 
-  delay(poteOlek);  +  delay(potState);  
      
-  // LED viigu oleku madalaks seadmine+  // Turn LED off
   digitalWrite(led, LOW);    digitalWrite(led, LOW); 
      
-  // Viite tekitamine, mille pikkus on võrdne potentsiomeetri sisendi väärtusega +  // Halt the program for potentiometer analog value number of milliseconds 
-  delay(poteOlek); +  delay(potState); 
 } }
 </code> </code>
  
-===== Näide #2.3 LED-i ereduse juhtimine potentsiomeetri asendi järgi =====+===== Example #2.3 Controlling LED brightness with potentiometer =====
  
 <code c> <code c>
-/* Nimetus: Näide #2.3 +/* 
-   Kirjeldus: LED-i ereduse juhtimine potentsiomeetri asendi järgi */ +Example #2.3 
-// Algus identne näitega #2.1 (kommenteeritud koodi vaata sealt)+*/ 
 +// Setup is same with example #2.1
  
-const int pote = A1; +const int pot = A1; 
 const int led = 13;  const int led = 13; 
-int poteOlek = 0; +int potState = 0; 
    
 void setup() void setup()
Line 240: Line 234:
 void loop()  void loop() 
 { {
-  // Potentsiomeetri muutuja hetkeväärtuse salvestamine +  // Get analog value of potentiometer 
-  poteOlek = analogRead(pote); +  potState = analogRead(pot); 
      
-/* Kui potentsiomeetri väärtus on suurem kui 0, siis seame LED viigu kõrgeks, +  // When potentiometer value is greater than then set LED on halt program for short period 
-   seejärel programm ootab potentsiomeetri väärtusega võrdse arvu mikrosekundeid */ +  if (potState > 0) 
-  if (poteOlek > 0) +
   {   {
     digitalWrite(led, HIGH);     digitalWrite(led, HIGH);
-    delayMicroseconds(poteOlek); +    delayMicroseconds(potState); 
   }   }
      
-  // Seame LED viigu madalasse olekusse+  // Turn LED off
   digitalWrite(led, LOW);    digitalWrite(led, LOW); 
      
-  // Programm ootab ülejäänud arvu mikrosekundeid perioodist  +  // Halt program for short period 
-  delayMicroseconds(1023 - poteOlek); +  delayMicroseconds(1023 - potState); 
 } }
 </code> </code>
  
-===== Harjutused ===== +===== Exercises ===== 
-=== Harjutus #2.1 === +=== Exercise #2.2 === 
-Modifitseerida näitekoodi nii, et LED läheb põlema viigu kõrgest olekust madalasse olekusse minekulPotentsiomeetri kogu pöördenurk vastab pingevahemikule 0-5 VLeida ligilähedane pinge väärtusmille juures Arduino sisend muutub madalast olekust kõrgesse olekusse ja vastupidiKas esineb hüsterees lülitumiste vahel? Tulemuse saamiseks võib kasutada potentsiomeetri nurka või jadapordi monitori+Control LED on and off time with potentiometer. 
 + 
 +====== Project 3 Alphabetical LCD  ====== 
 + 
 + 
 +===== Example #3.1 Writing on LCD screen ===== 
 + 
 +Theory: https://www.arduino.cc/en/Reference/LiquidCrystal 
 + 
 +<code c> 
 +/* 
 +Example #3.1 
 +*/ 
 +// Include LCD library 
 +#include <LiquidCrystal.h>  
 + 
 +// Create LCD object and set hardware connection pins 
 +LiquidCrystal lcd(89, 4, 5, 6, 7); 
 + 
 +void setup()  
 +
 +  // Define LCD size 
 +  lcd.begin(16, 2);  
 +   
 +  // Print out welcome message 
 +  lcd.print("Hello World!");  
 +
 + 
 +void loop()  
 +
 +  // Change cursor position to second line 
 +  lcd.setCursor(0, 1);  
 +   
 +  // Print out program working time in seconds 
 +  lcd.print(millis()/1000);  
 +
 +</code> 
 + 
 + 
 +===== Example #3.2 Custom characters on LCD ===== 
 + 
 +<code c> 
 +/* 
 +Example #3.2 
 +*/ 
 + 
 +#include <LiquidCrystal.h> 
 + 
 +LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 
 +  
 +  // Array to generate new character, 
 +  // "0b" in front of the number tells the compiler that it's a binary number 
 +byte customChar[8] =  
 +
 +  0b11111, 
 +  0b00000, 
 +  0b01010, 
 +  0b00000, 
 +  0b10001, 
 +  0b01110, 
 +  0b00000, 
 +  0b11111 
 +}; 
 +  
 +void setup() 
 +
 +  // Load new character to LCD memory position "0" 
 +  lcd.createChar(0, customChar);  
 +   
 +  // Define LCD size 
 +  lcd.begin(16, 2);  
 +  
 +  // Display new custom character on LCD 
 +  lcd.write((uint8_t)0); 
 +
 +void loop() 
 +
 +  // Do nothing 
 +
 +</code> 
 + 
 +===== Example #3.3 Reading LCD shield buttons ===== 
 + 
 + 
 +<code c> 
 +/* 
 +Nimetus: Example #3.3 
 +Kirjeldus: Reading LCD shield buttons 
 +*/ 
 + 
 +#include <LiquidCrystal.h> 
 + 
 + 
 +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("ADC: "); 
 +   
 +  // 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(); // Clear LCD 
 +     
 +    lcd.print("ADC: "); // Print out explaining text 
 +     
 +    lcd.print(buttonValue); // Print out analog input value 
 +     
 +    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 
 + buttonValue = analogRead(A0); 
 +  
 + if (buttonValue < 50)   return RIGHT; 
 + 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 
 + return NONE; 
 +
 +</code> 
 + 
 + 
 +===== 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://omerk.github.io/lcdchargen/|]] 
 + 
 + 
 + 
 +====== Project 4 Reading sensors and displaying results on LCD   ====== 
 + 
 +{{:et:arduino:sensorss:projekt4.jpg?800|}} 
 + 
 +===== Example #4.2 Distance and proximity sensors reading ===== 
 +Libraries: [[https://www.arduino.cc/en/guide/libraries#toc4|]] 
 + 
 +<code c> 
 +/* 
 +Example #4.2  
 +*/ 
 +  
 +// Include libraries 
 +#include <LiquidCrystal.h>  
 +#include <NewPing.h>  
 +  
 +// 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, US_ECHO,maxDistance); 
 +  
 +void setup()  
 +
 +  //Define LCD size 
 +  lcd.begin(16, 2);  
 +  
 +  // Print out explaining text 
 +  lcd.print("Dist:"); 
 +  lcd.setCursor(0, 1); 
 +  lcd.print("Object:"); 
 +
 +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, 0); 
 +  
 +  // EPrint out distance 
 +  lcd.print(distance);  
 +  
 +// Overwrite previous value numbers with spaces 
 +  lcd.print("   ");  
 +  
 +  // Set cursor on second line 10th character 
 +  lcd.setCursor(9, 1);  
 +  
 +  // when infrared sensor does not see anything print out "no", otherwise print out "yes"
 +  if(proximity == 1) lcd.print("no ");  
 +  else lcd.print("yes");  
 +  
 +  // Halt program to get stable view of LCD  
 +  delay(500);  
 +
 +</code> 
 + 
 + 
 +===== Exercises ===== 
 +=== Exercise #4.1 === 
 +Find out which sensor sample time is faster. 
 + 
 + 
 +====== Project 5 DC motor drive and speed control ====== 
 + 
 +{{:et:arduino:motors:projekt5.jpg?700|}} 
 + 
 +===== Example #5.1 DC motor ad H-bridge ===== 
 + 
 +<code c> 
 +/* 
 +Example #5.
 +*/ 
 +// 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,OUTPUT); 
 +  pinMode(right_A,OUTPUT); 
 +  pinMode(right_B,OUTPUT); 
 + 
 +  motors(0,0); // stop motors 
 +  delay(2000); // halt program for safety purposes 2 seconds 
 +
 + 
 +void loop()  
 +
 +   
 +  motors(1,1); // robot goes forward 
 +  delay(2000); // halt program for 2 seconds 
 +   
 +  motors(-1,1); // robot turns left 
 +  delay(500);  // halt program for 500 milliseconds 
 +
 + 
 +// 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,HIGH); 
 +    digitalWrite(left_B,LOW); 
 +  } 
 +  else if(left == -1) 
 +  { // Left motor CCW 
 +    digitalWrite(left_A,LOW); 
 +    digitalWrite(left_B,HIGH); 
 +  } 
 +  else 
 +  { // Left motor STOP 
 +    digitalWrite(left_A,LOW); 
 +    digitalWrite(left_B,LOW); 
 +  } 
 + 
 +  // Right motor 
 +  if(right == 1) 
 +  { // Right motor CW 
 +    digitalWrite(right_A,HIGH); 
 +    digitalWrite(right_B,LOW); 
 +  } 
 +  else if(right == -1) 
 +  { // Right motor CCW 
 +    digitalWrite(right_A,LOW); 
 +    digitalWrite(right_B,HIGH); 
 +  } 
 +  else 
 +  { // Right motor STOP 
 +    digitalWrite(right_A,LOW); 
 +    digitalWrite(right_B,LOW); 
 +  } 
 +
 +</code> 
 + 
 +===== 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,OUTPUT); 
 +  pinMode(left_B,OUTPUT); 
 +  pinMode(right_A,OUTPUT); 
 +  pinMode(right_B,OUTPUT); 
 + 
 +  motors(0,0); // Stop motors 
 +  delay(2000); // Delay for safety purposes 
 +
 +void loop() 
 + { 
 +  motors(100,100); // go fwd 
 +  delay(2000); 
 +  motors(100,255); // turn left 
 +  delay(2000);  
 +  motors(-255,-255); // go back 
 +  delay(2000); 
 +  motors(-255,-100); // go backward while turning right 
 +  delay(2000);  
 +  motors(150,-150); // turn on spot 
 +  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,left); 
 +    digitalWrite(left_B,LOW); 
 +  } 
 +  else if(left < 0 && left >= -255) 
 +  { // Left motor CCW 
 +    analogWrite(left_A, 255+left); 
 +    digitalWrite(left_B,HIGH); 
 +  } 
 +  else 
 +  { // Left motor STOP 
 +    digitalWrite(left_A,LOW); 
 +    digitalWrite(left_B,LOW); 
 +  } 
 +  
 +  // Right motor 
 +  if(right > 0 && right <= 255) 
 +  { // Right motor CW 
 +    analogWrite(right_A,right); 
 +    digitalWrite(right_B,LOW); 
 +  } 
 +  else if(right < 0 && right >= -255) 
 +  { // Right motor CCW 
 +    analogWrite(right_A,255+right); 
 +    digitalWrite(right_B,HIGH); 
 +  } 
 +  else 
 +  { // Right motor STOP 
 +    digitalWrite(right_A,LOW); 
 +    digitalWrite(right_B,LOW); 
 +  } 
 +
 +</code> 
 + 
 +===== Exercises ===== 
 + 
 +==== Exercise #5.1 ==== 
 +Modify exampe so that robot drives number eight shape.
  
-=== Harjutus #2.2 === +==== Harjutus #5.2 ==== 
-Modifitseerida näiteprogrammi nii, et potentsiomeetri keskpunktist (väärtus ~512) ühele poole reguleeritakse LED-i põlemas oleku viidet ja teisele poole kustus oleku viidet. Viite pikkuse reguleerimisel peab maksimaalne viite väärtus olema keskpunkti lähedal ja minimaalne lõpp-punktides. Fikseeritud viite pikkus peab olema 512 millisekundit. Seejuures reguleeritav viite pikkus peab jääma vahemikku 0 kuni 511. Tulemusena peaks ühes potentsiomeetri äärmuses LED olema püsivalt põlemas ja teises püsivalt kustus ning keskel võrdselt põlemas ja kustus.+Make robot parallel park itself.
  
-=== Harjutus #2.3 === 
-Modifitseerida näiteprogrammi nii, et LED põleb ainult potentsiomeetri pööramise ajal. LED-i põlemise eredus sõltub potentsiomeetri pööramise kiirusest. 
en/arduino/examples.1500959418.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0