So In my project I am trying to rotate a servo using a angle counter then display this on an lcd screen. I have the buttons to move the servos and the LCD working. When I run the LCD example in the arduino ide it works perfectly but when I try to implement the LCD within my code it will work properly for a few seconds then everything goes haywire on the screen. I had problems with the numbers or text moving and printing in random locations while I reset the cursor every loop. And random characters also stream past the screen as well after awhile. So I am assuming there is something wrong with my code since the components seem to work individually but not in unison. I have tried to use lcd.clear() everyloop then set cursor but this also make the screen go crazy after a few seconds. If I don't use the clear function the LCD will start putting numbers in the wrong location as I stated earlier.
Any help is appreciated
//Use poteniometer to vary plate angle with a serial read out of current angle. Use increments of 0.5 or 1 for largest values.
#include <Servo.h>
// include the library code:
#include <LiquidCrystal.h>
Servo myservo1;
Servo myservo2; // create servo object to control a servo
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(6, 7, 5, 4, 3, 2);
// twelve servo objects can be created on most boards
const int buttonPin1 = 8;
const int buttonPin2 = 9;
float pos = 0;
float angle = 0; // variable to store the servo position
//pos goes from 0 to 90 convert to plate angle
void setup() {
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
myservo1.attach(6);
myservo2.attach(5);// attaches the servo on pin 9 to the servo object
lcd.begin(16, 2);
}
void loop() {
// buttonstate1 = digitalRead(buttonPin1);
//buttonstate2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (digitalRead(buttonPin1) == HIGH) {
// turn LED on:
pos = pos+3;}
else if (digitalRead(buttonPin2) == HIGH){
pos = pos-3;
}
pos=constrain(pos,0,90);
angle = mapfloat(pos,0,90,0,30);
myservo1.write(pos);
myservo2.write(180-pos);// tell servo to go to position in variable 'pos'
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(angle);
//Serial.println(angle);
delay(100);
}
float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}
-Tyler Scofield
Research Aid