#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);
}
i am also using this code for scrolling
#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!");
delay(1000);
}
void loop() {
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
// wait a bit:
delay(150);
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
// delay at the end of the full loop:
delay(1000);
}
Does it print “hello world !” ? What’s the problem ?
Yes it does no problem but I need to make it so it changes text when the button is pressed and released it works fiñe the now. Brightness needs to be changed though as it loads very dark on the LCD
Seems to me you’re not trying to figure this out at all. You have code that prints to the serial monitor when the button is pressed. You have code that positions the cursor and prints to the LCD. Seriously, have you not thought to combine the two ? Set the cursor to the start of the line and “print” to the LCD what you want it to when the button isn’t being pressed. Then do the same, but with a different message, when the button is pressed. Give it a try.
i have combined the two. i see the servo flash whenever the button is pressed and released but no text is displayed for some reason. gonna take to my lecturer to have a look tomorrow as it lights up but no text
here is the code
#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
Serial.begin(9600);
myservo.attach(9);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!"); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 90, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
Serial.print("Val = ");
Serial.println(val);
delay(15);
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000); // waits for the servo to get there
}
What were you trying to display on the LCD ? If the “hello world” works, then the wiring and setup are good. You may find that your trying to update the LCD too often. Increase the delay from 15 msec to 100 msec. But, so far as I can see, there will be no difference in what the LCD displays, button pressed vs button not pressed. What part of the code would make you think that there would be any difference ?
FWIW your mention of mapping the analog value of the pin the button attached to is workable but unusual. You might want to read this tutorial and use that method instead. I would wire the button a little differently but what’s shown bellow will work.
http://arduino.cc/en/Tutorial/Button
http://arduino.cc/en/Tutorial/DigitalPins
hi again the next step is editing the code so i can change the value of the lcd when the button is pressed/released. the lcd is lighting up and you can see the blocks where text “should” be displayed but no text is there. the arduino is picking it up and loading the lcd so the wiring is right i just dont understand why no text is shown even in the hello world sketch.
tyson642:
i just dont understand why no text is shown even in the hello world sketch.
I will assume when you say
**[u]no text[/u]** you really mean no text once you get past the "hello world" display.
I would direct your attention to a typical LCD datasheet, specifically the rise/fall times under optical characteristics. IOW how long it takes to make the LCD to go on from off, off from on.
https://www.sparkfun.com/datasheets/LCD … tended.pdf
Did you increase the delay time ? Perhaps to even longer.
i mean there is no text at all even when hello world is run. all cables look ok all that is shown is a row of white boxes and a row of black boxes no text though. no idea why this is done
im an idiot it was a cabling error which i have since fixed. its working now but the text is quite dark. as i am not using a Potentiometer and a button instead how can i increase the brightness of the lcd?
i have since found problems with combining those 2 codes but i am using the analogInOutSerial which when combined with hello world works well
[code]/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground
created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
can anyone help me though with the when button is pressed it says something when released it says something else[/code]
i have changed the cabling around slightly here is the current set up which works fine
here is what i was trying though when i press the button the lcd changes from dark to light. it goes too light though and you cant see the text. the servo no longer works with this set up either. not sure what ive done or should i stick with the way i first had it
ive since looked into the code and i have an idea to use digitalread and if statement but not 100% how i will accomplish it. got a feeling that this is the last piece of the puzzle just wanting anyones ideas and help for finishing the code
any ideas please ive been fretting over this the lcd respons to the button so it knows when its been pressed i now need to set it so that it changes the text when pressed.
i have the if statement working now i am looking to expand to using multiple values. how can i do this i know to set up numbered variations of the text i am considering an if staement that says when the button is pressed/released select a value from one of the texts could this work?
here is my current code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0; // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to
// read the value of the button to see if it is high or low
int sensorValue = 0; // value read from the pot
int outputValue = 0; // output value to the LCD and Servo
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// tell arduino how many rows and collums lcd has
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Press The Button!");
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
if (outputValue == 0)
// if statement saying if the button is not pressed (0) then print one value
{ lcd.print ("Thankyou") ;
}else
// when button is pressed say this value
{lcd.print ("HELP ME PLEASE!");
}
//set the cursor to the first point on the lcd to display values
lcd.setCursor(0, 1);
}
i have noticed though when the text changes any squares that dont change have the original text still in them. is there a clear function i can set up so it removes any text left over
tyson642:
i have noticed though when the text changes any squares that dont change have the original text still in them. is there a clear function i can set up so it removes any text left over
Yes, there is.
http://arduino.cc/en/Reference/LiquidCrystal
http://arduino.cc/en/Reference/LiquidCrystalClear
So your code, revised, might look like this:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0; // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to
// read the value of the button to see if it is high or low
int sensorValue = 0; // value read from the pot
int outputValue = 0; // output value to the LCD and Servo
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// tell arduino how many rows and collums lcd has
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Press The Button!");
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
//set the cursor to the first point on the lcd to display values
lcd.setCursor(0, 1);
lcd.clear();
if (outputValue < 20)
// if statement saying if the button is not pressed (0) then print one value
{
lcd.print ("Thankyou") ;
}
else
// when button is pressed say this value
{
lcd.print ("HELP ME PLEASE!");
}
}
I also changed the exactly == 0 condition as that might be hard to achieve all the time due to noise. I wonder why you’re using the analog read to determine a button push and what you’re doing with the analogWrite() function. It’s outputting a PWM stream that I guess you’re using for something. It is not the proper function to use to control a servo. You had code that used the servo library and had the servo (finally working). Why did you get rid of those parts of the code ? Is your intent to add that back in when this is working ?
the way it is currently programmed is working as when i set the old code the servo began to not fuction as it would spin very slowly and sounded like it was going to blow up at some point. this code allows the servo to spin as i want and work with the lcd.