viernes, julio 15, 2011

Botonera para servos con Arduino UNO

Trasteando de nuevo con Arduino he hecho un montaje casero para almacenar y reproducir con sendos pulsadores la posición de un servo que se maneja o, mejor dicho, sigue a un potenciómetro. Esto me ha permitido jugar un poco con la librería Servo.h y las instrucciones para escribir y leer datos en la EEPROM de 1Kb que lleva el modelo Arduino UNO.

Ahí va la inmortalización del evento de pruebas:



El código fuente:
 
/*
Potentiometer-Servo-button-command
Author: Jorge Muñoz Rodenas jorgeroden1@gmail.com
License: GPL v.3
Date: 14/7/2011
Version: 0.2 (alpha)
Description: to do
 */

#include <eeprom.h>
#include <servo.h>

Servo myservo1;  
Servo myservo2;

// Constants

const int buttonPinMem1 = 2;               // digital pin Mem button1
const int buttonPinAct1 = 3;               // digital pin Act button1
const int buttonRelease = 12;              // digital pin RELEASE button
const int servo1Pin = 10;                  // Servo1 PWM pin
const int servo2Pin = 11;                  // Servo2 PWM pin
const int ledPin =  13;                    // the number of the LED pin (soldered on board)
const int potpin1 = 0;                     // analog pin used to connect the potentiometer1
const int potpin2 = 1;                     // analog pin used to connect the potentiometer2
const int addr0 = 0;                       // EEPROM 1st address  (Mem1-servo 1)
const int addr1 = 1;                       // EEPROM 2nd address  (Mem1-servo 2)

// Variables  

int val1 = 0;
int val2 = 0; 
int posPot1 = 0;
int posPot2 = 0;
byte pos1, pos2;                             //Servo position
int buttonStateMem1 = LOW;                   // memory pushbutton1 status
int buttonStateAct1 = LOW;                   // action pushbutton1 status
int buttonStateRelease = LOW;
boolean running = true;

void setup() {
  
   myservo1.attach(servo1Pin);                     // attaches the servo on pin 9 (digital) to the servo object 
   myservo2.attach(servo2Pin);                    // attaches the servo on pin 10 (digital) to the servo object 
   pinMode(ledPin, OUTPUT);          
   pinMode(buttonPinMem1, INPUT);  
   pinMode(buttonPinAct1, INPUT);  
}

void loop(){
    
  //Reads analog input
  
  val1 = analogRead(potpin1);              // reads the value of the potentiometer (value between 0 and 1023)   
  val1 = map(val1, 0, 1023, 0, 179);       // scale it to use it with the servo (value between 0 and 180) 
  val2 = analogRead(potpin2);              // reads the value of the potentiometer (value between 0 and 1023) 
  val2 = map(val2, 0, 1023, 0, 179);       // scale it to use it with the servo (value between 0 and 180)   
  
  //Reads digital input
  
  buttonStateMem1 = digitalRead(buttonPinMem1);
  buttonStateAct1 = digitalRead(buttonPinAct1);
  buttonStateRelease = digitalRead(buttonRelease);
  
  //Servo 1 and 2
  
  if (running == true)
  {
    myservo1.write(val1);
    myservo2.write(val2);
  }
 
 // Program 1
 
  if (buttonStateMem1 == HIGH) {    
    
    digitalWrite(ledPin, HIGH);          //LED 13 on
    EEPROM.write(addr0, val1);
    EEPROM.write(addr1, val2);
    digitalWrite(ledPin, LOW);           //LED 13 off
          
  } 
  
  if (buttonStateAct1 == HIGH) {     
 
    running = false;
    digitalWrite(ledPin, HIGH);                 //LED 13 on
    pos1 = EEPROM.read(addr0);
    pos2 = EEPROM.read(addr1); 
    myservo1.write(pos1);                       // sets the servo position according to the scaled value 
    myservo2.write(pos2);                       // sets the servo position according to the scaled value 
    digitalWrite(ledPin, LOW);                  //LED 13 off

    } 
 
    // Release button
  
  if (buttonStateRelease == HIGH) {    
    digitalWrite(ledPin, HIGH);                 //LED 13 on
    running = true;   
    delay(40);
    digitalWrite(ledPin, LOW);                 //LED 13 off      
  } 
   
}


Sobre el tema de las conexiones, los pines vienen en el software aunque si alguien lo quiere montar, y tiene dudas de cómo se conectan los servos, pulsadores y potenciómetros aquí dejo unos garabatos a mano (hoy paso del Eagle) a modo de recordatorio.

Ciao!


2 comentarios:

joselito dijo...

Hola, me ha gustado mucho el código, pero tengo una preguntílla que seguro que es sencillo pero con eso de que llevo muy poco tiempo en esto no caigo, la pregunta es la siguiente:
¿como puedo hacer para que se quede parado el servo con una pulsación aunque mueva el potenciómetro y pulsar otra vez para que siga su camino? ya se que no tiene casi nada que ver con el ejercicio, pero me hace falta para un proyecto en el que estoy envuelto.
Muchas gracias.

Unknown dijo...

And the other Button?