Hi, I just got my Arduino kit from Sunfounder. So far nothing works out of the box. I've fixed the first few problems but this one has me stumped.
When I print multiple characters only the first character prints unless I print one character at a time.
i.e. lcd.print("cm"); will show 'c' on the LCD, but lcd.print("c"); lcd.print("m"); will display 'cm'
Sunfounder knows about this problem because they worked around it in their Hello World sketch by putting the string in an array and then printing one character at a time in a loop. Here is their code:
#include <LiquidCrystal.h>
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//
char array1[]=" Sunfounder "; //the string to print on the LCD
char array2[]="hello, world! "; //the string to print on the LCD
int tim = 250; //the value of delay time, 500 for 250 ms or 2 for each 1 ms
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
}
//
void loop()
{
lcd.setCursor(15,0); // set the cursor to column 15, line 0
for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array1[positionCounter1]); // Print a message to the LCD.
delay(tim); //wait for 250 microseconds
}
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
lcd.setCursor(15,1); // set the cursor to column 15, line 1
for (int positionCounter = 0; positionCounter < 26; positionCounter++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array2[positionCounter]); // Print a message to the LCD.
delay(tim); //wait for tim which is set above
}
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
}
/****/
But they don't do this in their other sketches. The one I'm working on now is Ultrasonic. And it only prints the first character. Here is the code:
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
// include the library code
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
lcd.init();
lcd.backlight();
}
void loop() {
delay(100); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print(uS / US_ROUNDTRIP_CM);
lcd.setCursor(12, 1);
lcd.print("cm");
}
Please help me.
My first question is "How do I fix this?"
Other questions:
How do I find out which library a function is in? For example the Serial.print("Ping: "); Where do I find the code? I want to see it as an example.
How does the lcd.print() function work? Again, where is the code? I can't find it in the LiquidCrystal_I2C library, even though print is in the keywords?
Thanks in advance for the help.