Seek help on controlling servo

Hey guys,

I am trying to use the ArduinoUno board to control a servo motor so that it would go to a commanded angular position. At the same time, I would like to use an lcd screen to display the amount of current that is going through the motor. I have written this program that is shown below, but I am having trouble compiling it, so please help! It is greatly appreciated!

include <Servo.h>

define rxPin 0

define txPin 2

Servo lcd(rxPin, txPin);

int analogInputAmps = A5;

int readAmpsADC = 0;
float amps = 0.0;

Servo legservo;// This is our servo

void setup()
{
pinMode(analogInputAmps, INPUT);

lcd.begin(9600);
lcd.print("\r"); // Initialize VT100 Controller with ‘CR’).
delay(500);
lcd.print("\e[2J"); // Clear display (VT100 Command).
delay(300);
lcd.print(“Ready”);
lcd.print("\e[2m"); // Cursor OFF (VT100 Command)
delay(2000);
}

float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

legservo.attach(9);//attach pin 9 to legservo
pinMode(2,INPUT);//set button to input
legservo.write(90);
}

void loop()
{
onButtonState=digitalRead(2);//read and save the variable “2” to the state of button
if (onButtonState==High){
runServo=1;}
if (onButtonState==Low){
runServo=0;}
if (runServo==1){//make servo spin
legservo.write(50);
}
else if (runServo==0){//stops servo from spinning
legservo.write(90);
}
else {legservo.write(90);
}
readAmpsADC = analogRead(analogInputAmps);
amps = fabs(fmap(readAmpsADC, 0.0, 1023.0, 0.01, 5.0));
amps = amps * 10;

lcd.print("\e[1;1HAmps: “); //Position cursor row 1; col 1 (VT100 Command)
delay(200);
lcd.print(amps);
lcd.print(” ");
delay(500);

}

Servo lcd(rxPin, txPin);

What is this? You have a servo called lcd? Why is the servo/lcd connected to the hardware serial pin?

Please start here

What is A5 connected to? Most servo's only have a control pin + (power and grd).

Mark

Have you first made seperate working code for servo control, LCD display, and current measuring?

Ok Hello Everyone, sorry for the confusion in the code I posted previously, I do have two separate working code and I am not sure how to combine the two so I would have the lcd screen display the amount of current that is going through the servo.

Here is the first code to control the servo and command it to rotate to a given angle:

# include <Servo.h>

Servo legservo;// This is our servo
int onButtonPin = 2; // set on button pin
int onButtonState = 90; // set onButtonState
int runServo=0; // 0=servos off ,  1=servos on

void setup()
{
 legservo.attach(9); // attach the 9 pin to legservo
 pinMode(onButtonPin, INPUT); // set button to input
  legservo.write(90);
}


void loop(){
  onButtonState = digitalRead(onButtonPin); 
 
  if (onButtonState == HIGH){
    runServo = 1;
  }
  if (offButtonState == LOW){
    runServo = 0;
  }
 if (runServo == 1){    //makes servos spin
  legservo.write(50);
 }
 else if (runServo == 0){    //stops servo from spinning
   legservo.write(90);
 }
 else{
   legservo.write(90);
 }
 
}

This code works fine for controlling the servo with a button, but I am trying to combine the lcd control I found here (Measuring Current with the Arduino) with the above code. So could I get some help on that please?

Thanks!

Look at button debouncing, look at the blink without delay example and then at FSM's in the playground.

Mark