Hi Guys
I am trying to show the position of a servo on a display. I have a sketch that moves the servo with a pot that works and the display lights up. Here is the sketch that I have so far. For full disclosure do not know what I am doing but I am trying to learn ! hahahaha I almost forgot to say that I am using a Arduino UNO.
Thanks Gary
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
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()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, -10, 160); // 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
delay(15); // waits for the servo to get there
}
Hi John
I put those lines into the sketch and then got a message 'lcd.Print' does not have class type
Gary
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
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()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(3,0);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, -10, 160); // 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
lcd.setCursor(3,0);
lcd.print.(" "); // This covers up the previous value
lcd.setCursor(3,0);
lcd.print(val);
delay(15); // waits for the servo to get there
}
Hi John
That was it ! Thanks You Very Much ! I am a happy guy. I will include the sketch so that anyone who wants to do this will have it.
Gary
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
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()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(3,0);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, -10, 160); // 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
lcd.setCursor(3,0);
lcd.print(" "); // This covers up the previous value
lcd.setCursor(3,0);
lcd.print(val);
delay(15); // waits for the servo to get there
}
Hey man this is some great code! Do you by chance have a video up on what screen you used and how you made it all work together, or even a diagram of how you hooked it all up? Thanks
Hi R Power
The screen that I used is this one http://www.ebay.com/itm/Arduino-Serial-IIC-I2C-TWI-2004-204-Character-LCD-Module-Display-/190637600054?pt=LH_DefaultDomain_0&hash=item2c62e2ed36 I also used the Arduino Sensor Shield v5.0 which made it easier to hook it all up. There is ground to one end of the pot and five volts to the other and then the middle tap goes to analog pin 0 and the servo white lead goes to digital pin 9. The black lead form the servo goes to ground and the red lead goes to five volts. For the II-12C LCD screen VCC goes to 5 volts, GND goes to ground, SDA goes to Analog pin 4 and SCL goes to Analog pin 5. That is all there is. If you use the sensor shield there are pins that are marked SCL,SDA, - and + so it is really easy to hock up on it. I had a bit of trouble getting my LCD to work at first because I found that all <LiquidCrystal_I2C.h> files are not the same. It took a bunch of tries to get the screen working but I used the simple Hello World sketch to get it working before I tried this sketch and I was lucky to get some help from Terry at YouArduino. Then I got some help here from John getting this sketch working. It seems that some times it is hard to get some help. I have been trying to figure out how to log a temp and humidity sensor to a SD card but haven't found a solution yet. Good luck and make sure to have some fun !
Nice code I just added some adjustment to your code that maybe useful, check the lines with (<------)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
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()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(3,0);
myservo.write(0); // <------ put the servo at zero
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, -10, 160); // scale it to use it with the servo (value between 0 and 180)
pos = map(val, 0, 1023, 0, 180); // <----- (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
lcd.setCursor(3,0);
lcd.print(" "); // This covers up the previous value
lcd.setCursor(3,0);
lcd.print(val);
delay(15); // waits for the servo to get there
}