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:
#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.
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:
..... 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 ..
du kan skrive:
input.toCharArray(charArray,16);
lcd.print(charArray);
Her er hele koden:
#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