sábado, 29 de marzo de 2014

Servomotor with Position Recall

One of the annoying things when using servos with Arduino are those setup movements every time we switch on the board.If the servo is linked to something carefully positioned, we will loose the reference every time that power is cycled.

We may avoid this effect by storing the position of the servo in the non-volatile memory. Arduino allows us to access 512 registers (say, memory slots) that will survive while it is powered off, kind of storing the info in the hard disk drive of a computer.

The assembly and code below will move 20 degrees of a servomotor and store the end position every time the button is pushed. When the board is switched on, the servo is not moved to a default position but it first checks if there is a proper value in the EEPROM and if so, it will use that as the starting value for the servo.


Electrically, the 9V battery feeds the Arduino power through Vin pin, and the servo through a 7805 voltage regulator. For a quick and dirty check, you may connect your servo to the Arduino 5V pin instead of using the voltage regulator, but I am always wary of using Arduino pins in power applications.

The pushbutton is pulled up to 5V through the 1k resistor and will yield a 0 to the digital pin #4 when pushed.

You will find more details and examples on how to manage non-volatile memory at the EEPROM libray description.

// SERVO_MEMORY

// Controlling a servo position using a switch, will
// turn moveDegrees every time the switch is pushed
// and change the direction once it reaches the end. 
// The position is recorded in the non-volatile memory so the 
// position of the servo is kept if switched off an on.

// by Carles Flotats, March 2014, after the servo, EEPROM
// and fade examples

#include <Servo.h> 
#include <EEPROM.h>
 
Servo myservo;  // create servo object to control a servo 
 
int switchPin = 4;  // pin used to connect a pulled up button
int val;            // variable to store the servo position
int moveDegrees=20; // degrees to turn for every button push

int memoryPos=5;    // memory slot to store the position


 
void setup() 
{ 
  val=EEPROM.read(memoryPos);   //read the stored position
  if (val < 20  || val > 160){  // if it is not a valid position
    val=20;                     // assign a default value
  }
  
  //Serial.begin(9600);
  pinMode(switchPin, INPUT);
  
  myservo.attach(9);  // pin 9 controls the servo  
  myservo.write(val); // moves to the stored position
} 
 
void loop() 
{ 
  //Serial.println(digitalRead(switchPin));
  
  while(digitalRead(switchPin)){  // wait for button pushed
    delay(10);
  }
    val=val+moveDegrees;        // increase the position

  //Serial.println(val);
  myservo.write(val);                // sets the servo position 
  EEPROM.write(memoryPos, val);     //stores the current position
  delay(500);                   // waits for servo to get there 
  
  if (val == 20 || val == 160) {    // if the servo is at the end
    moveDegrees = -moveDegrees ;   // changes the movement direction
  } 
} 

1 comentario:

  1. Top 50 Top 50 Best Trex Titanium Headsets
    Top 50 Best titanium for sale Trex Titanium best titanium flat iron Headsets – Top titanium earrings 50 Top 50 Best Trex citizen titanium watch Ti, 2021. Tagged with titanium granite countertops best trex titanium.

    ResponderEliminar