2 mercury switch & 1 servo horizontal balance

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 :frowning:
Anyone can help me ???
Thnx in advance :slight_smile:

You can simplify the code if you use the arduino servo library. Here is your sketch modifed to do this. I have not tried it and you will probably need to do some work to debounce the switch. Also make sure you are using pull-down resistors. If you are not experienced using switches, try some of the switch tutorials.

#include <Servo.h>

Servo myServo;  // create servo object to control a servo 

/** Adjust these values for your servo and setup, if necessary **/
int servoPin     =  9;    // control pin for servo motor  
int turnRate     =  2;    // degrees per switch press  

int buttonDown = 2;     // the number of the pushbutton pin
int buttonUp = 3;

int buttonUpState = 0;
int buttonDownState = 0;

void setup() {  
  myServo.attach(9);  // attaches the servo on pin 9 to the servo object 

  // initialize the pushbutton pin as an input:
  pinMode(buttonUp, INPUT);  
  pinMode(buttonDown, INPUT);  
}  

void loop() {  
  // read the state of the pushbutton value:
  buttonUpState = digitalRead(buttonUp);
  buttonDownState = digitalRead(buttonDown);

  if (buttonDownState == HIGH) { 
     if(myServo.read() > turnRate){
         myServo.write(myServo.read() - turnRate); 
     }
  }  
  if (buttonUpState == HIGH) { 
     if(myServo.read() < 180 - turnRate){
         myServo.write(myServo.read() + turnRate); 
     }
  }  
}

have fun!

Thnx mem for your reply :slight_smile:
The code WORKING fine :wink:
I v just added a delay (10); to make motion smooth
Thnx again !!!