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!
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;
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?