/* Controlling a servo position using a potentiometer (variable resistor) by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> modified on 8 Nov 2013 by Scott Fitzgerald http://arduino.cc/en/Tutorial/Knob*/
#include <Servo.h>
Servo myservo; // create servo object to control a servoint potpin = 0; // analog pin used to connect the potentiometerint val; // variable to read the value from the analog pin/////////////////////////////float volt ; // additional -2014-dec-30th -Monitoring voltagefloat Angle; // additional -2014-dec-30th -Monitoring voltage///////////////////////////////voidsetup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object///////////////Serial.begin(9600); // 2014-dec-30th -Monitoring voltage///////////
}
voidloop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) ////////below added 2014 DEC 30th
volt=val*(5.0/1023.0) ; //cal the voltage ,//must use float and 5.0 digital point
Angle=val*(180.0/1023.0) ; //cal the angleSerial.print(val);
Serial.print(" Voltage : ");
Serial.print(volt);
Serial.print(" Angle: ");
Serial.println(Angle);
/////above added 2014 DEC 30thif ((val>10) && (val<1015)){
val = map(val, 0, 1023, 0, 180); // 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
}
////// below additional -2014-dec-30th -Monitoring voltageif (val<10) {
Serial.println(" below 10"); //safe control
}
if (val>1015) {
Serial.println(" over 1015"); //safe control
}
//////////////////////////above
}
沒有留言:
張貼留言