Servo control

Dear all,

I am newbie with arduino.

We try to run a hacked servo, so far its working.

We have 3 positions, left, stop, right.

In fact the problem is that we do not start in stop position.
And that is what we want.

Here our code:

#include <Servo.h>
#include <IRremote.h>


unsigned long Value1 = 16591063; // where value is Left button
unsigned long Value2 = 16607383; // where value is Right button
unsigned long Value3 = 16623703; // where value is STOP button

 
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
Servo servo1;
int pos;         // variable to store the servo position 
int Speed;       // Number of degrees to move each time a left/right button is pressed


// the setup routine runs once when you press reset:
void setup() 
{              

Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

// initialize the digital pin as an output.

servo1.attach(9); // attack servo to digital pin 9
pos = 90;         // start at midpoint 90 degrees
}

// the loop routine runs over and over again forever:
void loop() {

if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
}

if(results.value == Value1) {
servo1.write(93);
}
if(results.value == Value2) {
servo1.write(96);
}
if(results.value == Value3) {
servo1.write(95);
}

Regards,

David

You can do a Servo.write before you do the attach, if that's what you mean.

Please use code tags when posting code.

Hi AWOL,

We have done, problem solved.

#include <Servo.h>
#include <IRremote.h>


unsigned long Value1 = 16591063; // where value is Left button
unsigned long Value2 = 16607383; // where value is Right button
unsigned long Value3 = 16623703; // where value is STOP button

 
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
Servo servo1;
int pos;         // variable to store the servo position 
int Speed;       // Number of degrees to move each time a left/right button is pressed

// the setup routine runs once when you press reset:
void setup() 
{              

Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

// initialize the digital pin as an output.

servo1.attach(9); // attack servo to digital pin 9
servo1.write(95); // start at stop position
}

// the loop routine runs over and over again forever:
void loop() {

if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
}

if(results.value == Value1) {
servo1.write(93);
}
if(results.value == Value2) {
servo1.write(96);
}
if(results.value == Value3) {
servo1.write(95);
}
}

So easy, why i did not think about this.

Regards,

David

Or you can put the write after the attach.

Servo code that might help you test your continuous rotation servo.

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a 
// to decrease) and enter to change servo position 
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position 
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continuous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

void loop()
{
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    if(readString.indexOf('x') >0) { 
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos); 
    }
  }
  readString=""; //empty for next input
}