Hi. I m trying to build an horizontal balance system using 2 mercury switches and 1 servo .
I m trying to modify this principialabs.com - This website is for sale! - principialabs Resources and Information. to meet my needs. What i v done so far is......
/** Adjust these values for your servo and setup, if necessary **/
int servoPin = 9; // control pin for servo motor
int minPulse = 600; // minimum servo position
int maxPulse = 2400; // maximum servo position
int turnRate = 10; // servo turn rate increment (larger value, faster rate)
int refreshTime = 20; // time (ms) between pulses (50Hz)
/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
int moveServo; // raw user input
long lastPulse = 0; // recorded time (ms) of the last pulse
int buttonPin1 = 2; // the number of the pushbutton pin
int buttonPin2 = 3;
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
centerServo = maxPulse - ((maxPulse - minPulse)/2);
pulseWidth = centerServo; // Give the servo a starting point (or it floats)
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
// Serial.begin(9600);
// Serial.println(" Arduino Serial Servo Control");
// Serial.println("Press < or > to move, spacebar to center");
// Serial.println();
}
void loop() {
// wait for serial input
// read the incoming byte:
moveServo = Serial.read();
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)
if (buttonState1 == HIGH) { pulseWidth = pulseWidth - turnRate; }
if (buttonState2 == HIGH) { pulseWidth = pulseWidth + turnRate; }
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
}
but...........is not working
Anyone can help me ???
Thnx in advance