Show Posts
Pages: [1]
1  International / Software / Proyecto para controlar dos servos y funciones de una cámara con ethernet shield on: November 11, 2012, 07:14:37 am
Hola a todos.

Soy un novato en Arduino, estoy aprendiendo a través de un proyecto de control de una cámara con dos servos para hacer la función pan-tilt, zoom y enfoque a través de una web alojada en el propio Arduino. La idea es conectar el arduino a un router wifi y controlarlo todo dentro de una red local.

Estuve atascado porque la encriptación del router creaba interferencias en el control de los servos (desconozco porqué). Leyendo y probando (y copy-pasteando código smiley-roll-sweat) he conseguido que me funcione todo. Pero no perfectamente. En algún momento los servos se "confunden" y no hacen lo que deberían.

El codigo es este:

Code:
/*
  Web Server
 Proyecto de controlador web para camara
 */

#include  <SPI.h>
#include  <Ethernet.h>
#include  <Servo.h>  // Incluimos la libreria servo

//mac, IP
byte mac[] = { 0x00, 0x02, 0x0A, 0x0D, 0x06, 0x01 };
byte ip[] = { 192, 168, 1, 177 }; //IPAddress ip(192,168,1,177);
byte gateway[] = { 192, 168, 1, 1 }; // ip router
byte subnet[] = { 255, 255, 255, 0 }; // mascara subnet
EthernetServer server(80); // puerto ethernet

//pines out Trans
int zoom1 = 7;
int zoom2 = 8;
int foco1 = 2;
int foco2 = 3;
Servo servohor;  // creando el objeto servo para control
Servo servover;  // otro

int servoPinh     =  6;    // control pin for servo motor  
int minPulseh     =  600;  // minimum servo position  
int maxPulseh     =  2400; // maximum servo position  
int turnRateh     =  40;  // servo turn rate increment (larger value, faster rate)  
int refreshTimeh  =  100;   // time (ms) between pulses (50Hz)  
int centerServoh;         // center servo position  
int pulseWidthh;          // servo pulse width  
int moveServoh;           // raw user input  
long lastPulseh   = 0;    // recorded time (ms) of the last pulse

int servoPinv     =  5;    // control pin for servo motor  
int minPulsev     =  1400;  // minimum servo position  
int maxPulsev     =  2000; // maximum servo position  
int turnRatev     =  40;  // servo turn rate increment (larger value, faster rate)  
int refreshTimev  =  100;   // time (ms) between pulses (50Hz)  
int centerServov;         // center servo position  
int pulseWidthv;          // servo pulse width  
int moveServov;           // raw user input  
long lastPulsev   = 0;    // recorded time (ms) of the last pulse  

boolean reading = false;
String readString;

void setup()
{
  
Serial.begin(9600);
//iniciar conexion
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();

  pinMode(2, OUTPUT); //pin selected to control
  pinMode(3, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control
  pinMode(5, OUTPUT);  // Set servo pin as an output pin  
  centerServoh = maxPulseh - ((maxPulseh - minPulseh)/2);  
  pulseWidthh = centerServoh;   // Give the servo a starting point (or it floats)
  pinMode(6, OUTPUT);  // Set servo pin as an output pin  
  centerServov = maxPulsev - ((maxPulsev - minPulsev)/2);  
  pulseWidthv = centerServov;   // Give the servo a starting point (or it floats)




}

void loop()
{

EthernetClient client = server.available();
if (client)
{
  while (client.connected())
  {
    if (client.available())
    {
      char c = client.read();
      if(readString.length() < 100)
      {
      readString += c;
      Serial.print(c);
      }
    if (c == '\n')
      {
      

     //HTML creacion de formulario
          Serial.println(readString);

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Control Camara posicion zoom y foco</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Arduino, Control Camara posicion zoom y foco</H1>");
        
         //Servohor
          client.println("<a href=\"/?right\"\">DERECHA</a>");
          client.println("<a href=\"/?center\"\">CENTRO</a>");
          client.println("<a href=\"/?left\"\">IZQUIERDA</a>");
          
          //servover
          client.println("<a href=\"/?up\"\">ARRIBA</a>");
          client.println("<a href=\"/?mid\"\">CENTRO</a>");
          client.println("<a href=\"/?down\"\">ABAJO</a>");
          
          // zoom y foco
          client.println("<a href=\"/?zoom1\"\">ZOOM+</a>");
          client.println("<a href=\"/?zoom2\"\">ZOOM-</a>");
          client.println("<a href=\"/?foco1\"\">FOCO+</a>");
          client.println("<a href=\"/?foco2\"\">FOCO-</a>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(100);
          //stopping client
          client.stop();
  
  
  

          ///////////////////// control arduino pin

    
          if(readString.indexOf("up") >0)  { pulseWidthv = pulseWidthv - turnRatev; }
          if(readString.indexOf("mid") >0)   { pulseWidthv = centerServov; }
          if(readString.indexOf("down") >0)  { pulseWidthv = pulseWidthv + turnRatev; }
          
            if (pulseWidthv > maxPulsev) { pulseWidthv = maxPulsev; }
            if (pulseWidthv < minPulsev) { pulseWidthv = minPulsev; }
        
            if (millis() - lastPulsev >= refreshTimev)
            {  
            digitalWrite(5, HIGH);   // start the pulse  
            delayMicroseconds(pulseWidthv);  // pulse width  
            digitalWrite(5, LOW);    // stop the pulse  
            lastPulsev = millis();
          }         // save the time of the last pulse  
        
        
          if(readString.indexOf("right") >0)  { pulseWidthh = pulseWidthh - turnRateh; }
          if(readString.indexOf("center") >0)   { pulseWidthh = centerServoh; }
          if(readString.indexOf("left") >0)  { pulseWidthh = pulseWidthh + turnRateh; }
          
            if (pulseWidthh > maxPulseh) { pulseWidthh = maxPulseh; }
            if (pulseWidthh < minPulseh) { pulseWidthh = minPulseh; }
        
            if (millis() - lastPulseh >= refreshTimeh)
            {  
            digitalWrite(6, HIGH);   // start the pulse  
            delayMicroseconds(pulseWidthh);  // pulse width  
            digitalWrite(6, LOW);    // stop the pulse  
            lastPulseh = millis();
          }         // save the time of the last pulse  
        
  
        
          if(readString.indexOf("zoom1") >0)
          {
            digitalWrite(7, HIGH);
            delay (300);
            digitalWrite (7, LOW);
                  
            Serial.println("Zoom+");
          }
                    
          if(readString.indexOf("zoom2") >0)
          {
            digitalWrite(8, HIGH);
            delay (300);
            digitalWrite (8, LOW);            
            Serial.println("Zoom-");
          }
          
          if(readString.indexOf("foco1") >0)
          {
            digitalWrite(2, HIGH);
            delay (300);
            digitalWrite (2, LOW);            
            Serial.println("Foco+");
          }
          
          if(readString.indexOf("foco2") >0)
          {
            digitalWrite(3, HIGH);
            delay (300);
            digitalWrite (3, LOW);          
            Serial.println("Foco-");
          }
          
          //clearing string for next read
          readString="";

 
 //break;
          }
      }
   }
}
}    

          
          

.

¿Alguna idea para limpiar o mejorar el código?

Gracias y un saludo.
2  Using Arduino / Project Guidance / Re: Web Control over a couple of servos, Pan-Tilt camara on: November 11, 2012, 05:30:30 am
Hi

Ok, I resolve my problem. It was on the Router.

Something was wrong with WPA encryption.

Thanks for all and regards.
3  Using Arduino / Project Guidance / Re: Web Control over a couple of servos, Pan-Tilt camara on: October 14, 2012, 06:54:41 pm
Doesn't works too.

I had a problem cause my router is far away from the computer. I install arduino soft in another laptop and get the response on serial monitor.

When i had conected the arduino to the router and open the web, shows;

Quote
server servo7pin 5 test 1.0
GET / HTTP/1.1

GET /favicon.ico HTTP/1.1

Led On

The servo moves one time but doesnt response after that...
4  Using Arduino / Project Guidance / Re: Web Control over a couple of servos, Pan-Tilt camara on: October 14, 2012, 05:38:31 pm
I dont send any GET request neither apeers anything in the serial monitor with this code. After write code to Arduino i work only through forms in web browser.
It's possible I forget anything?

The code which works for me and my servos with serial monitor is something like this:

Code:
/*
 * NewSerialServo
 * --------------
 * Servo control from the Serial port
 *
 * Alteration of the control interface to use < and > keys
 * to slew the servo horn left and right.  Works best with
 * the Linux/Mac terminal "screen" program.
 *
 * Created 10 December 2007
 * copyleft 2007 Brian D. Wendt
 * http://principialabs.com/
 *
 * Adapted from code by Tom Igoe
 * http://itp.nyu.edu/physcomp/Labs/Servo
 */  
  
/** Adjust these values for your servo and setup, if necessary **/  
int servoPin     =  2;    // control pin for servo motor  
int minPulse     =  600;  // minimum servo position  
int maxPulse     =  2400; // maximum servo position  
int turnRate     =  100;  // 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  
  
  
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)  
  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  
  if (Serial.available() > 0) {  
    // read the incoming byte:  
    moveServo = Serial.read();  
  
    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)  
    if (moveServo == 44) { pulseWidth = pulseWidth - turnRate; }  
    if (moveServo == 46) { pulseWidth = pulseWidth + turnRate; }  
    if (moveServo == 32) { pulseWidth = centerServo; }  
  
    // stop servo pulse at min and max  
    if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }  
    if (pulseWidth < minPulse) { pulseWidth = minPulse; }  
  
    // print pulseWidth back to the Serial Monitor (uncomment to debug)  
    // Serial.print("Pulse Width: ");  
    // Serial.print(pulseWidth);  
    // Serial.println("us");   // microseconds  
  }  
  
  // pulse the servo every 20 ms (refreshTime) with current pulseWidth  
  // this will hold the servo's position if unchanged, or move it if changed  
  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  
  }  
}

from http://principialabs.com/arduino-serial-servo-control/

Thanks and regards.

P.d. I was working with microseconds instead of degrees too but with the same result.
5  Using Arduino / Project Guidance / Re: Web Control over a couple of servos, Pan-Tilt camara on: October 14, 2012, 04:04:02 pm
Thanks for your answers.

I try to learn and understand more about this, not only resolve the problem...

I want to know why the code works for a switch high/low order and doesnt for a servo.write  order.

Thanks and regards.

6  Using Arduino / Project Guidance / Re: Web Control over a couple of servos, Pan-Tilt camara on: October 14, 2012, 01:53:50 pm
Sorry.  smiley-roll-sweat
Proccesing and htmls parts of my project are the most difficult for me.
Let me understand why i'm shooting myself in my foot with string class. I tried to captcha the html form and process the movement and functions orders trought arduino but it only works with functions (zoom and focus).

Thanks for all and regards.
7  Using Arduino / Project Guidance / Web Control over a couple of servos, Pan-Tilt camara on: October 14, 2012, 01:05:24 pm
Hi
I'm a newbie with Arduino and processing, but each day i'm working with this i'm happier.

My project is about a web control with Arduino One and Ethernet shield over a couple of servos for pan-tilt movement, and a several camara functions (zoom +/- and focus +/-)

The camera functions works with a 4 BC107 transistors which are operated through four digital outputs.
Servos are connected directly into arduino but externally powered.

Nowadays, I managed to work all the servos and camara functions through Serial Monitor with a simple code but when I try to modify the code for web control, Camara functions works ok with it but servos doesnt works.  smiley-cry

I dont know if the code is the problem or ethernet trafic or something else...


The code is:

Code:
/*
 Proyecto de controlador web para camara
 */

#include  <SPI.h>
#include  <Ethernet.h>
#include  <Servo.h>  // Incluimos la libreria servo

//mac, IP
byte mac[] = { 0x00, 0x02, 0x0A, 0x0D, 0x06, 0x01 };
byte ip[] = { 192, 168, 1, 177 }; //IPAddress ip(192,168,1,177);
byte gateway[] = { 192, 168, 1, 1 }; // ip router
byte subnet[] = { 255, 255, 255, 0 }; // mascara subnet
EthernetServer server(80); // puerto ethernet

//pines out Trans
int zoom1 = 7;
int zoom2 = 8;
int foco1 = 2;
int foco2 = 3;
Servo servohor;  // creando el objeto servo para control
Servo servover;  // otro

int servohorP = 110;
int servoverP = 110;
int MaxservoverP = 155;
int MinservoverP = 65;


String readString;

void setup()
{
 
Serial.begin(9600);
//iniciar conexion
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

  pinMode(2, OUTPUT); //pin selected to control focus+
  pinMode(3, OUTPUT); //pin selected to control focus-
  pinMode(7, OUTPUT); //pin selected to control zoom+
  pinMode(8, OUTPUT); //pin selected to control zoom-
  pinMode(5, OUTPUT);  // Set servo pin as an output pin 
  pinMode(6, OUTPUT);  // Set servo pin as an output pin 
 
  servohor.attach(6);
  servover.attach(5);
  servohor.write(servohorP);
  servover.write(servoverP);
}

void loop()
{

EthernetClient client = server.available();
if (client)
{
  while (client.connected())
  {
    if (client.available())
    {
      char c = client.read();
      if(readString.length() < 100)
      {
      readString += c;
      }
      Serial.print(c);
     
    if (c == '\n')
      {
     

     //HTML creacion de formulario
          Serial.println(readString);

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Control Camara posicion zoom y foco</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Arduino, Control Camara posicion zoom y foco</H1>");
         
         //Servohor
          client.println("<a href=\"/?right\"\">DERECHA</a>");
          client.println("<a href=\"/?center\"\">CENTRO</a>");
          client.println("<a href=\"/?left\"\">IZQUIERDA</a>");
         
          //servover
          client.println("<a href=\"/?up\"\">ARRIBA</a>");
          client.println("<a href=\"/?mid\"\">CENTRO</a>");
          client.println("<a href=\"/?down\"\">ABAJO</a>");
         
          // zoom y foco
          client.println("<a href=\"/?zoom1\"\">ZOOM+</a>");
          client.println("<a href=\"/?zoom2\"\">ZOOM-</a>");
          client.println("<a href=\"/?foco1\"\">FOCO+</a>");
          client.println("<a href=\"/?foco2\"\">FOCO-</a>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(10);
          //stopping client
         client.stop();
   
 
 

          ///////////////////// control arduino pin

     
          if (readString.indexOf("up") >0)
        {
          servoverP -= 2;
          if (servoverP < MaxservoverP) { servoverP += 2; }
          servover.write(servoverP);
          delay (15);
        }       
       
          if (readString.indexOf("down") >0)   
        {
          servoverP += 2;
          if (servoverP > MinservoverP) { servoverP -= 2; }
          servover.write(servoverP);
          delay (15);
        }
       
          if (readString.indexOf("mid") >0) 
        {
          servoverP == 110;
          servover.write(servoverP);
          delay (15);
        }
         
         
          if(readString.indexOf("zoom1") >0)
          {
            digitalWrite(7, HIGH);
            delay (300);
            digitalWrite (7, LOW);
                   
            Serial.println("Zoom+");
          }
                   
          if(readString.indexOf("zoom2") >0)
          {
            digitalWrite(8, HIGH);
            delay (300);
            digitalWrite (8, LOW);           
            Serial.println("Zoom-");
          }
         
          if(readString.indexOf("foco1") >0)
          {
            digitalWrite(2, HIGH);
            delay (300);
            digitalWrite (2, LOW);             
            Serial.println("Foco+");
          }
         
          if(readString.indexOf("foco2") >0)
          {
            digitalWrite(3, HIGH);
            delay (300);
            digitalWrite (3, LOW);           
            Serial.println("Foco-");
          }
         
         
          readString="";
         

 
// break;
          }
      }
   }
}
}     

The code includes only one servo functions because i used it for test.
Can you give me some ideas of where I'm wrong?

Thanks for all and regards.

P.D. Thanks for zoomkat. I used part of his code.
8  International / Software / Re: Duda Con Servos on: October 10, 2012, 04:00:31 am
Aprovecho a ver si alguien puede ayudarme con ideas de en que me puedo estar equivocando al adaptar el anterior código para funcionar a través del ethernet shield. Mi código es este:


Code:
/*
  Web Server
 Proyecto de controlador web para camara
 */

#include  <SPI.h>
#include  <Ethernet.h>
#include  <Servo.h>  // Incluimos la libreria servo

//mac, IP
byte mac[] = { 0x00, 0x02, 0x0A, 0x0D, 0x06, 0x01 };
byte ip[] = { 192, 168, 1, 177 }; //IPAddress ip(192,168,1,177);
byte gateway[] = { 192, 168, 1, 1 }; // ip router
byte subnet[] = { 255, 255, 255, 0 }; // mascara subnet
EthernetServer server(80); // puerto ethernet

//pines out Trans
int zoom1 = 7;
int zoom2 = 8;
int foco1 = 2;
int foco2 = 3;
Servo servohor;  // creando el objeto servo para control
Servo servover;  // otro

int servoPinh     =  6;    // control pin for servo motor  
int minPulseh     =  600;  // minimum servo position  
int maxPulseh     =  2400; // maximum servo position  
int turnRateh     =  50;  // servo turn rate increment (larger value, faster rate)  
int refreshTimeh  =  100;   // time (ms) between pulses (50Hz)  
int centerServoh;         // center servo position  
int pulseWidthh;          // servo pulse width  
int moveServoh;           // raw user input  
long lastPulseh   = 0;    // recorded time (ms) of the last pulse

int servoPinv     =  5;    // control pin for servo motor  
int minPulsev     =  1400;  // minimum servo position  
int maxPulsev     =  2000; // maximum servo position  
int turnRatev     =  50;  // servo turn rate increment (larger value, faster rate)  
int refreshTimev  =  100;   // time (ms) between pulses (50Hz)  
int centerServov;         // center servo position  
int pulseWidthv;          // servo pulse width  
int moveServov;           // raw user input  
long lastPulsev   = 0;    // recorded time (ms) of the last pulse  

boolean reading = false;
String readString;

void setup()
{
  
Serial.begin(9600);
//iniciar conexion
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();

  pinMode(2, OUTPUT); //pin selected to control
  pinMode(3, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control
  pinMode(5, OUTPUT);  // Set servo pin as an output pin  
  centerServoh = maxPulseh - ((maxPulseh - minPulseh)/2);  
  pulseWidthh = centerServoh;   // Give the servo a starting point (or it floats)
  pinMode(6, OUTPUT);  // Set servo pin as an output pin  
  centerServov = maxPulsev - ((maxPulsev - minPulsev)/2);  
  pulseWidthv = centerServov;   // Give the servo a starting point (or it floats)




}

void loop()
{

EthernetClient client = server.available();
if (client)
{
  while (client.connected())
  {
    if (client.available())
    {
      char c = client.read();
      if(readString.length() < 100)
      {
      readString += c;
      Serial.print(c);
      }
    if (c == '\n')
      {
      

     //HTML creacion de formulario
          Serial.println(readString);

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Control Camara posicion zoom y foco</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Arduino, Control Camara posicion zoom y foco</H1>");
        
         //Servohor
          client.println("<a href=\"/?right\"\">DERECHA</a>");
          client.println("<a href=\"/?center\"\">CENTRO</a>");
          client.println("<a href=\"/?left\"\">IZQUIERDA</a>");
          
          //servover
          client.println("<a href=\"/?up\"\">ARRIBA</a>");
          client.println("<a href=\"/?mid\"\">CENTRO</a>");
          client.println("<a href=\"/?down\"\">ABAJO</a>");
          
          // zoom y foco
          client.println("<a href=\"/?zoom1\"\">ZOOM+</a>");
          client.println("<a href=\"/?zoom2\"\">ZOOM-</a>");
          client.println("<a href=\"/?foco1\"\">FOCO+</a>");
          client.println("<a href=\"/?foco2\"\">FOCO-</a>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(100);
          //stopping client
          client.stop();
  
  
  

          ///////////////////// control arduino pin

    
          if(readString.indexOf("up") >0)  { pulseWidthv = pulseWidthv - turnRatev; }
          if(readString.indexOf("mid") >0)   { pulseWidthv = centerServov; }
          if(readString.indexOf("down") >0)  { pulseWidthv = pulseWidthv + turnRatev; }
          
            if (pulseWidthv > maxPulsev) { pulseWidthv = maxPulsev; }
            if (pulseWidthv < minPulsev) { pulseWidthv = minPulsev; }
        
            if (millis() - lastPulsev >= refreshTimev)
            {  
            digitalWrite(5, HIGH);   // start the pulse  
            delayMicroseconds(pulseWidthv);  // pulse width  
            digitalWrite(5, LOW);    // stop the pulse  
            lastPulsev = millis();
          }         // save the time of the last pulse  
        
        
          if(readString.indexOf("zoom1") >0)
          {
            digitalWrite(7, HIGH);
            delay (300);
            digitalWrite (7, LOW);                
            Serial.println("Zoom+");
          }
                    
          if(readString.indexOf("zoom2") >0)
          {
            digitalWrite(8, HIGH);
            delay (300);
            digitalWrite (8, LOW);            
            Serial.println("Zoom-");
          }
          
          if(readString.indexOf("foco1") >0)
          {
            digitalWrite(2, HIGH);
            delay (300);
            digitalWrite (2, LOW);            
            Serial.println("Foco+");
          }
          
          if(readString.indexOf("foco2") >0)
          {
            digitalWrite(3, HIGH);
            delay (300);
            digitalWrite (3, LOW);          
            Serial.println("Foco-");
          }
          
          //clearing string for next read
          readString="";

 
 //break;
          }
      }
   }
}
}    

Gracias y un saludo
9  International / Software / Re: Duda Con Servos on: October 10, 2012, 03:58:22 am
Hola.

Yo estoy trabajando en un proyecto para hacer eso mismo que buscas tú a través del Ethernet shield para mover remotamente dos servos. El problema es soy novato en processing y me cuesta bastante.

Buscando info encontré un código para hacerlo a través del Serial Monitor que creo te ayudará.

Code:
/*
 * NewSerialServo
 * --------------
 * Servo control from the Serial port
 *
 * Alteration of the control interface to use < and > keys
 * to slew the servo horn left and right.  Works best with
 * the Linux/Mac terminal "screen" program.
 *
 * Created 10 December 2007
 * copyleft 2007 Brian D. Wendt
 * http://principialabs.com/
 *
 * Adapted from code by Tom Igoe
 * http://itp.nyu.edu/physcomp/Labs/Servo
 */  
  
/** Adjust these values for your servo and setup, if necessary **/  
int servoPin     =  2;    // control pin for servo motor  
int minPulse     =  600;  // minimum servo position  
int maxPulse     =  2400; // maximum servo position  
int turnRate     =  100;  // 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  
  
  
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)  
  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  
  if (Serial.available() > 0) {  
    // read the incoming byte:  
    moveServo = Serial.read();  
  
    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)  
    if (moveServo == 44) { pulseWidth = pulseWidth - turnRate; }  
    if (moveServo == 46) { pulseWidth = pulseWidth + turnRate; }  
    if (moveServo == 32) { pulseWidth = centerServo; }  
  
    // stop servo pulse at min and max  
    if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }  
    if (pulseWidth < minPulse) { pulseWidth = minPulse; }  
  
    // print pulseWidth back to the Serial Monitor (uncomment to debug)  
    // Serial.print("Pulse Width: ");  
    // Serial.print(pulseWidth);  
    // Serial.println("us");   // microseconds  
  }  
  
  // pulse the servo every 20 ms (refreshTime) with current pulseWidth  
  // this will hold the servo's position if unchanged, or move it if changed  
  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  
  }  
}  

Extraído de: http://principialabs.com/arduino-serial-servo-control/


Pages: [1]