ArduShop.ro

ArduShop.ro
cod Z4A413ZN reducere 5%, pentru vizitatorii paginilor mele !

miercuri, 16 ianuarie 2013

Arduino si o intrare analogica, afisare pe LCD

    Am trecut la alt pas, citirea unei tensiuni pe o intrare analogica si afisarea ei pe un LCD cu 2 randuri si 16 coloane.
    Schema de conectare este simpla: potentiometrul (am folosit de 200kohmi) are extremitatile alimentate intre +5V si masa GND) iar cursorul este conectat la intrarea analogica AN0. Partea de alimentare si comanda a LCD-ului se face, clasic, cum am prezentat si in materialul anterior.



    Valorile citite la intrare sunt convertite in 1024 trepte (2 la puterea 10... de la 0 la 1023).

   Dupa materialele obtinute din exemplele programului Arduino IDE si de pe site-ul lor (http://arduino.cc/en/Tutorial/AnalogInput si http://arduino.cc/en/Tutorial/AnalogInOutSerial) am adaptat 2 sketch-uri, primul in care se afiseaza numarul de trepte, iar in al doilea la ce procent din valoare este cursorul potentiometrului.


    Primul sketch:

/*
  LiquidCrystal Library - display() and noDisplay()

 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 uses the 
 display() and noDisplay() functions to turn on and off
 the display.

 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
 * 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://arduino.cc/en/Tutorial/LiquidCrystalDisplay
 http://arduino.cc/en/Tutorial/AnalogInput
 http://www.tehnic.go.ro
 */

// 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);

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor


void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("www.tehnic.go.ro");
  lcd.setCursor(0, 1);
  lcd.print("    by niq_ro");
  delay(3000);
  lcd.clear();  
}

void loop() {
  lcd.print("potentiometer is at");
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);     
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  lcd.print("/1023 value");
  delay(1000);
  lcd.clear();
}

Al doilea sketch:
/*
  LiquidCrystal Library - display() and noDisplay()
 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 uses the 
 display() and noDisplay() functions to turn on and off
 the display.
 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
 * 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.

 */

// 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);

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int outputValue = 0;        // value output to the PWM (analog out)


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

void loop() {
  lcd.print("potentiometer is at");
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
// map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 100);  
  // change the analog out value:
  // analogWrite(analogOutPin, outputValue);           
  lcd.setCursor(0, 1);
  lcd.print("at ");
  lcd.print(outputValue);
  // lcd.print(" %");
  lcd.print(" %");
  delay(1000);
  lcd.clear();
}

     Dupa ce m-am mai documentat un pic pe forumul Arduino pentru a folosi si simboluri la afisare (din memoria interna a afisajului LCD 1602), am modificat ultimul sketch conform ideii de la http://arduino.cc/forum/index.php/topic,21371.0.html obtinand:

Sketch-ul modficat este:


/*
  LiquidCrystal Library - display() and noDisplay()

 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 uses the 
 display() and noDisplay() functions to turn on and off
 the display.

 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
 * 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://arduino.cc/en/Tutorial/LiquidCrystalDisplay
 http://arduino.cc/en/Tutorial/AnalogInput
 http://arduino.cc/forum/index.php/topic,21371.0.html
 http://www.tehnic.go.ro
 */

// 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);

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int outputValue = 0;   // value output to the %
int outputValue1 = 0;   // value output to cursor


void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("www.tehnic.go.ro");  
  lcd.setCursor(0, 1);
  lcd.print("    by niq_ro");
  delay (5000);
  lcd.clear();
}

void loop() {
  lcd.print("potentiometer is at");
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
// map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 100);  
  outputValue1 = map(sensorValue, 0, 1023, 0, 200);  
        
  lcd.setCursor(0, 1);
  lcd.print("at ");
  lcd.print(outputValue);
  // lcd.print(" %");
  lcd.print("% (");
  // value in kohms (full value is 200kohms)
  lcd.print(outputValue1);
  // lcd.print("kohms");
  lcd.print("k");
  lcd.write(0b11110100);
  lcd.print(")");
  delay(1000);
  lcd.clear();
}


    Am adaptat si in limba romana deoarece am fost "certat" ca e textul doar in engleza, obtinand:


Sketch-ul a devenit:


/*

 Circuit pentru afisaj LCD cu 16 coloane si 2 randuri (1602):
 * pin RS de la OCD la intrarea digitala 12 a lui Arduino
 * 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
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 * potentiometru este alimentat intre 5v si masa (GND) iar cursorul la intrarea analogica AN0

 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://arduino.cc/en/Tutorial/LiquidCrystalDisplay
 http://arduino.cc/en/Tutorial/AnalogInput
 http://arduino.cc/forum/index.php/topic,21371.0.html
 http://www.tehnic.go.ro
 */

// 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);

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int outputValue = 0;   // value output to the %
int outputValue1 = 0;   // value output to cursor


void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("www.tehnic.go.ro");  
  lcd.setCursor(0, 1);
  lcd.print(" creat de niq_ro");
  delay (5000);
  lcd.clear();
  
  lcd.print("afisare pozitie");  
  lcd.setCursor(0, 1);
  lcd.print("cursor !!!");
  delay (5000);
  lcd.clear();
  
}

void loop() {
  lcd.print("cursorul este la");
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
// map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 100);  
  outputValue1 = map(sensorValue, 0, 1023, 0, 200);  
        
  lcd.setCursor(3, 1);
  lcd.print(outputValue);
  // lcd.print(" %");
  lcd.print("% (");
  // value in kohms (full value is 200kohms)
  lcd.print(outputValue1);
  // lcd.print("kohms");
  lcd.print("k");
  lcd.write(0b11110100);
  lcd.print(")");
  delay(1000);
  lcd.clear();
}

Niciun comentariu:

Trimiteți un comentariu