ATmega328p (Arduino Uno) + HD44780 LCD Display (16x02) (Læst 4115x)

Offline Danni-Hansen

  • µProcessoren
  • *
  • Indlæg: 544
  • Antal brugbare Indlæg: 17
    • Vis profil
ATmega328p (Arduino Uno) + HD44780 LCD Display (16x02)
« Dato: Oktober 04, 2012, 14:10:11 »
Hej folkens.

Prøver at få mit LCD Display til at skrive 2 linjers kode, så den selv smider tekst ned på næste linje efter 16 tegn.
Som nævnt i overskriften, er der 2 linjer på displayet.

Jeg får dog nogle fejl på det:
lcd_serial.cpp: In function ‘void loop()’:
lcd_serial.cpp:37:7: error: ‘input’ was not declared in this scope
lcd_serial.cpp:39:9: error: ‘inputt’ was not declared in this scope
lcd_serial.cpp:43:15: error: ‘input’ was not declared in this scope
lcd_serial.cpp:44:8: error: ‘inputt’ was not declared in this scope


Koden ser således ud:
Kode:
#include <LiquidCrystal.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int backLight = 13;    // pin 13 will control the backlight

void setup(){
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
 
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      char tekst = Serial.read();
      input += tekst;
      if(input.length() > 16){
        inputt += tekst;
      }
    }
      // display each character to the LCD
    lcd.write(input);
    if(inputt.length() > 0){
     lcd.setCursor(0,1);
     lcd.write(inputt);
    }
  }
}

På forhånd undskylder jeg for at jeg er lidt forvirret fortiden. Så der kan være total mess-up i koden.
Mvh. Danni Hansen.

 

Offline gerd

  • Administrator
  • µProcessoren
  • *****
  • Indlæg: 915
  • Antal brugbare Indlæg: 97
    • Vis profil
    • Hjemmeside med nogle af mine projekter
Sv: ATmega328p (Arduino Uno) + HD44780 LCD Display (16x02)
« Svar #1 Dato: Oktober 04, 2012, 17:10:14 »
Hej Danni,

der er 2 "data types" for tekst:
string og
String

string er en "char array" - det er bare en samling af tegn ( char []) .
String er en komfortabel klasse med mange funktioner.

http://arduino.cc/en/Reference/HomePage

For input.length() eller inputt += tekst du har brug for en String klasse.
Men lcd.write eller lcd.print vil gerne have en string ( bare tegn, char [] )

Med:
Kode:
..... int backLight = 13;    // pin 13 will control the backlight
String input="";
String inputt="";
char charArray[17];

void setup(){
...

input og inputt er en String klasse og charArray er en samling af tegn.

I stedet for ..
Kode:
lcd.write(input);
du kan  skrive:
Kode:
input.toCharArray(charArray,16);
lcd.print(charArray);


Her er hele koden:
Kode:
#include <LiquidCrystal.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int backLight = 13;    // pin 13 will control the backlight
String input="";
String inputt="";
char charArray[17];

void setup(){
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
 
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      char tekst = Serial.read();
      input += tekst;
      if(input.length() > 16){
        inputt += tekst;
      }
    }
      // display each character to the LCD
     input.toCharArray(charArray,16);
     lcd.print(charArray);
    if(inputt.length() > 0){
     lcd.setCursor(0,1);
     inputt.toCharArray(charArray,16);
     lcd.print(charArray);
    }
  }
}

gerd

 

Offline Danni-Hansen

  • µProcessoren
  • *
  • Indlæg: 544
  • Antal brugbare Indlæg: 17
    • Vis profil
Sv: ATmega328p (Arduino Uno) + HD44780 LCD Display (16x02)
« Svar #2 Dato: Oktober 05, 2012, 22:51:08 »
Hej Gerd.

Fantastisk :D
Tak!! Skal have prøvet det af ved næste "lege time" ;) hehe
Mvh. Danni Hansen.