trouble with controlling a single servo [solved]

Hi,

After an absence of about 2 years I have started again with Arduino.
I’m repeating my previous experiments quickly to get familiar with the coding again.

I have trouble controlling a RC servo ending up in 1 dead servo until now.

I’m really puzzled because this code worked fine 2 years ago (since then I have thrown away the joystick but the code should still be fine)

// Controlling servo positions using a hacked joystick (Logitech WingmanAttackII) 
// by Leo Groeneveld

#include <Servo.h> 
 
Servo myservo2;   // create servo object to control servo2 (pitch servo in my case)
Servo myservo3;   // create servo object to control servo3 (roll servo in my case)

int potpin2 = 2;  // analog pin used to connect the potentiometer2
int val2;         // variable to read the value2 from the analog pin2
int potpin3 = 3;  // analog pin used to connect the potentiometer3
int val3;         // variable to read the value3 from the analog pin3
int servopos2;    // variable to set the postition of servo2
int servopos3;    // variable to set the postition of servo3

void setup() 
{ 
  myservo2.attach(2);  // attaches the servo on pin 2 to the servo object 
  myservo3.attach(3);  // attaches the servo on pin 3 to the servo object 
  Serial.begin (9600); // start serial connexion to monitor
} 
 
void loop() 
{ 
  val2 = analogRead(potpin2);                  // reads the value of potentiometer2 (value between 0 and 1023) 
  val3 = analogRead(potpin3);                  // reads the value of potentiometer3 (value between 0 and 1023) 

  if (val2 > 510 and val2 < 610) {             // dead-band
  servopos2 = 89;                              // when in dead-band servo2 postition is neutral
  myservo2.write(servopos2);                   // sets servo2 position to neutral
  }
  else {
    if (val2 > 240 and val2 <= 510) {
      servopos2 = map(val2, 240, 510, 15, 89); // scale it to use it with servo2 (value between 15 and 89) 
      myservo2.write(servopos2);               // sets servo2 position according to the scaled value 
    }
    else {
      if (val2 <=240){                         // when joystick potentiometer2 gets "overturned"
        servopos2 = 15;                        // postition of servo2 must be 15
        myservo2.write(servopos2);             // keeps servo2 in maximum position
      }
      else {
        if (val2 >= 610 and val2 < 880) {
          servopos2 = map(val2, 610, 880, 89, 164);    // scale it to use it with servo2 (value between 89 and 164) 
          myservo2.write(servopos2);                   // sets servo2 position according to the scaled value 
        }
        else {
          if (val2 >= 880) {                           // when joystick potentiometer gets "overturned"
            servopos2 = 164;                           // position of servo2 must be 164
            myservo2.write(servopos2);                 // keeps servo2 in maximum position
          }}}}}
          
// seperator between servos (DIRECTION OF SERVO3 IS REVERSED!!!)

if (val3 > 460 and val3 < 560) {               // dead-band
  servopos3 = 89;                              // when in dead-band servo3 postition is neutral
  myservo3.write(servopos3);                   // sets servo3 position to neutral
  }
  else {
    if (val3 > 190 and val3 <= 460) {
      servopos3 = map(val3, 191, 460, 164, 89);  // scale it to use it with servo3 (value between 164 and 89) 
      myservo3.write(servopos3);                 // sets servo3 position according to the scaled value 
    }
    else {
      if (val3 <=190){                           // when joystick potentiometer3 gets "overturned"
        servopos3 = 164;                         // postition of servo3 must be 164
        myservo3.write(servopos3);               // keeps servo3 in maximum position
      }
      else {
        if (val3 >= 560 and val3 < 830) {
          servopos3 = map(val3, 560, 829, 89, 15);     // scale it to use it with servo3 (value between 89 and 15) 
          myservo3.write(servopos3);                   // sets servo3 position according to the scaled value 
        }
        else {
          if (val3 >= 830) {                           // when joystick potentiometer gets "overturned"
            servopos3 = 15;                            // position of servo3 must be 15
            myservo3.write(servopos3);                 // keeps servo3 in maximum position
          }}}}}          
          
  delay(15);                                           // waits for the servos to get in the desired positions
   Serial.print("potstand2 = ");
   Serial.print(val2); 
   Serial.print(" servo2 stand = ");
   Serial.println(servopos2);
   //   Serial.print("potstand3 = ");
     // Serial.print(val3); 
   //   Serial.print(" servo3 stand = ");
     // Serial.println(servopos3);
}

Now I have written this code to control a single servo using a potentiometer.

#include <Servo.h>

Servo myservo;     // create servo object to control pwm signal

int potPin = 2;    // select the input pin for the potentiometer
int val;           // variable to store the value coming from the potmeter
int servopos;      // variable to set the value of the pwm signal

void setup()
{
  myservo.attach(7);   // attaches the servo on pin 7 to the servo object
  Serial.begin (9600);
}

void loop ()
{
  val = analogRead(potPin);               // read the value from the potmeter
  servopos = map (val, 0, 1023, 10, 169); // scale the value from the potmeter to fade value to be written to digital pin 9
  myservo.write(servopos);                // set servo position to scaled value
  Serial.print("servopos = ");
  Serial.println(servopos);
  Serial.print("val = ");
  Serial.println(val);
  delay(1500);
}

All it does is putting the servo in one max angle and overloading the thing.
When I look at the values using the serial monitor everything seems to be fine.

The setup is:
HP 8710p laptop running windows 8.1 (is this the problem?, my previous OS was windows XP)
Arduino MEGA 1280 clone, used it the previous time

What am I doing wrong?

Thanks for your help.

Leo

After uploading this version of the sketch it now works fine???

#include <Servo.h>

Servo myservo;     // create servo object to control pwm signal

int potPin = 2;    // select the input pin for the potentiometer
int val;           // variable to store the value coming from the potentiometer
int servopos;      // variable to set the value of the pwm signal

void setup()
{
  myservo.attach(7);   // attaches the servo on pin 7 to the servo object
  Serial.begin (9600);
}

void loop ()
{
  val = analogRead(potPin);               // read the value from the potentiometer
  servopos = map (val, 0, 1023, 10, 169); // scale the value from the potentiometer to pwm value to be written to digital pin 7
  myservo.write(servopos);                // set servo position to scaled value
  Serial.print("servopos = ");
  Serial.println(servopos);
  Serial.print("val = ");
  Serial.println(val);
  delay(15);
}

It’s a complete mistery to me but at least it’s working now.

Thanks,

Leo

How are you powering the servo? 95% of servo problems here are to do with inadequate power or due to sharing power between servo and Arduino (don't do that).

Hi Mark,

I have a seperate power supply (5V 1A DC) for the servo with common ground with/to the arduino which is fed by the USB cable of the laptop.

Leo

I recall having a problem where (I think) some strange character got embedded in my code and refused to be deleted by the editor (probably because I coudn't see it). I think it went away when I deleted and completely retyped a few lines. Problem is I can't remember whether it was in an Arduino (compiled) program or a JRuby (interpreted) program.

...R