c0ryp1:
here is what i have so far
#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
Servo vert;
Servo hor;
void setup()
{
lcd.begin(16, 2); // define lcd size
vert.attach(8); //vertical servo data pin
hor.attach(9); // horizontal servo data pin
lcd.print("vert:"); //static variable line 1 on lcd
lcd.setCursor(0,1); //Move to second line
lcd.print("hor:"); //static variable line 2 on lcd
int pos = vert.read();
int pos2 = hor.read();
}
void migrate(Servo & vert, int newPos)
{
int wait=random(30,60);
int pos = vert.read(); //Read the current servo position
if (pos < newPos) {
for (int i=pos; i < newPos; i++) {
vert.write(i);
delay(wait);
lcd.setCursor(6, 0); // set vert servo position lcd print location
lcd.print(pos);
lcd.print(" ");
}
} else {
for (int i=pos; i > newPos; i--) {
vert.write(i);
delay(wait);
}
}
}
void migrate2 (Servo & hor, int newPos2)
{
int wait=random(30,60);
int pos2 = hor.read(); //Read the current servo position
if (pos2 < newPos2) {
for (int i=pos2; i < newPos2; i++) {
hor.write(i);
delay(wait);
lcd.setCursor(6, 1); // set vert servo position lcd print location
lcd.print(pos2);
lcd.print(" ");
}
} else {
for (int i=pos2; i > newPos2; i--) {
hor.write(i);
delay(wait);
}
}
}
void randomPosition() {
int rand=random(70,110);
migrate(hor, rand);
rand=random(40,135);
migrate(vert, rand);
}
void loop() {
randomPosition();
delay(2000);
}
the lcd is printing corectly to line 1 but i have a static 90 on line 2 but the servo is moving?
Looks good, just add some comments. At least generally what the code is doing.
It may seems petty, but you want to learn complex code and when code get complex it is easy to forget what are you trying to accomplish.
I would follow your "spec" , that way you avoid disorganized spaghetti code.
The old - you have to have a direction where you going.
Things are also easier to change if you have a direction.
LCD is great debugging tool, my code has 80% LCD debug code in it!
BTW Hitachi some of the standard LCD "stuff" is "zero" numbered - your two liner have line 0 and line 1.
You may find out later that is is wise to always clear the LCD before you print to it.
Also start looking at your variables "scope" - when to use local variable and when you need globals.
It also looks more "professional " when you use variables of sufficient size just to do the job.
Good luck.