26.2.17

Arduino LCD

Käesolev juhend on tõlgitud lehelt https://www.arduino.cc/en/Tutorial/HelloWorld

Kood kirjutab ekraanile teksti ja loeb sekundeid alates sisselülitamise hetkest.



RS pin - valib kuhu LCD mälus andmed kirjutatakse.
R/W pin - valib kirjutamise või lugemise. Kui on lubatud, siis saab kirjutada.
8 andme pinni (D0 -D7). Pinnide seisund (high või low) võimaldab andmeid lugeda või kirjutada.
Ekraani kontrastsuse pin Vo, toide (+5V ja Gnd) ja LED taustavalgus (Bklt+ ja BKlt-).

Skeem

  • LCD RS pin ühenda digital pin 12
  • LCD Enable pin ühenda digital pin 11
  • LCD D4 pin ühenda digital pin 5
  • LCD D5 pin ühenda digital pin 4
  • LCD D6 pin ühenda digital pin 3
  • LCD D7 pin ühenda digital pin 2
Lisaks ühenda 10k potensiomeeter +5V ja GND. 220 oomine takisti ühendatakse taustavalguse tarbeks pinnidele 15 ja 16 LCD ekraanil.
Kood
/*
  LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */


// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

Arduino ja ultraheliandur

Lisan ühendamise skeemi ja kauguse mõõtmise koodi.

Originaali leiab http://arduinobasics.blogspot.com.ee/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html

Ühendamise skeem



Kauguse mõõtmise kood: LED pin 13 on koodist välja märgitud!


/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 6
 
 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/
2012/05/arduino-hc-sr04-ultrasonic-distance.html
 And modified further by ScottC here: http://arduinobasics.blogspot.com.au/
2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
 on 10 Nov 2012.
 */


#define echoPin 7 // Echo Pin
#define trigPin 6 // Trigger Pin
//#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 //pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 //digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 //digitalWrite(LEDPin, LOW); 
 }
 

Arduino ja mootoriplaadi REV 3 kasutamine

Tegin näidiseks jupi koodi koos eestikeelsete kirjeldustega. Mootoril tuleb määrata 3 erineva pinni olekud. Mootoril A pin 3 määrab kiiruse, pin 12 suuna ja pin 9 oleks pidur. Mootor B on minul vasakul. Koodis tuleb tähele panna kuidas määratakse mootori valikul teise mootori pinnide asend:
if(motor == 1){
    digitalWrite(DIRA, inPin1);
    digitalWrite(BRKA, inPin2);
    analogWrite(PWMA, speed);
  }else{
    digitalWrite(DIRB, inPin3);
    digitalWrite(BRKB, inPin4);
    analogWrite(PWMB, speed);
  }

Siit saab kogu koodi millega robot sõidab edasi ja aeglasemalt tagasi. Tagasi kiirus 50 on liiga väike ja üks mootor ei oma piisavalt jõudu. Video leiab siit https://www.youtube.com/watch?v=RjrxIsvad6I


//mootoriplaadi REV 3 naidiskood

//Mootor A
int PWMA = 3; //Kiirus
int DIRA = 12; //Suund
int BRKA = 9; //Pidur

//Mootor B
int PWMB = 11; //Kiirus
int DIRB = 13; //Suund
int BRKB = 8; //Pidur

void setup(){
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);

  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);

 
  pinMode(BRKA, OUTPUT);
  pinMode(BRKB, OUTPUT);
}

void loop(){
  move(1, 100, 1); //mootor A, kiirus, suund
  move(0, 100, 1); //mootor B, kiirus, suund

  delay(1000); //oota 1 sekund
  stop(); //stop alamprogramm
  delay(500); //ootab kuni uue liikumiseni

  move(1, 50, 0); //mootor A, kiirus, suund
  move(0, 50, 0); //motor B, kiirus, suund

  delay(1000);
  stop();
  delay(500);
}


void move(int motor, int speed, int direction){
//Move alamprogramm
//motor: 0 mootor B ja 1 mootor A
//speed: 0 kuni 255 on kiirus
//direction: 0 suund, 1 teistpidi suund

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;
  boolean inPin3 = LOW;
  boolean inPin4 = HIGH;
 
  if(direction == 1){
    inPin1 = HIGH;
    inPin3 = HIGH;
    inPin2 = LOW;
    inPin4 = LOW;
  }else{
    inPin1 = LOW;
    inPin3 = LOW;
    inPin2 = LOW;
    inPin4 = LOW;
  }

  if(motor == 1){
    digitalWrite(DIRA, inPin1);
    digitalWrite(BRKA, inPin2);
    analogWrite(PWMA, speed);
  }else{
    digitalWrite(DIRB, inPin3);
    digitalWrite(BRKB, inPin4);
    analogWrite(PWMB, speed);
  }
}

void stop(){
//lubab standby ehk stop alamprogramm
 
}