Go Down

Topic: Freeing up analogWrite() on pin 10 after servo.detach() (Read 124 times) previous topic - next topic

ShankerD

Hi,

I have project that I need to use one of the servo.h libraries and a motor shield that makes use of pins 10-13 for PWM and direction..

The servo will only need to function when the robot is still so I was thinking to initialise (myservo.attach()) and detach (detach ()) the servo when in use as needed; then carry on using analogWrite to continue controlling the motor with the PWM on pin 10.

However, when I try to do the myservo.detach() after running the servo, analogWrite still does not seem to work on pin 10.


Here is some sample code that im using to test this out.


Code: [Select]
#include <Servo.h>
/**Shield occupies d pins 10 - 13 for motor control**/
const int E1Pin = 10;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;

Servo myservo;
/**Alias names for motor direction and speed**/
typedef struct {
  byte enPin;
  byte directionPin;
} MotorContrl;
const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;

const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;

/**sets direction of current flow from M1 & M2.**/
const int Backward = HIGH; //0V
const int Forward = LOW; //5V



/**program**/
void setup() {
 
  initMotor();
//  myservo.write(90);  // set servo to mid-point
}

void loop() {

  moveMotor(M1, Forward, 100);
  moveMotor(M2, Forward, 100);
  delay (2000);
  moveMotor(M1, Forward, 0);
  moveMotor(M2, Forward, 0);

 
  myservo.attach(3);
  myservo.write(1550);
  delay(5000);
  myservo.detach() ;
  initMotor();
 
  digitalWrite(E1Pin, LOW);
  pinMode(E1Pin, OUTPUT);
  moveMotor(M1, Forward, 100);
  moveMotor(M2, Forward, 100);
  delay (2000);

}

/**function to initialise i motors for OUTPUT**/
void initMotor( ) {
  int i;
  for ( i = 0; i < MotorNum; i++ ) {
    digitalWrite(MotorPin[i].enPin, LOW);

    pinMode(MotorPin[i].enPin, OUTPUT);
    pinMode(MotorPin[i].directionPin, OUTPUT);
  }
}

void moveMotor( int motorNumber, int direction, int speed) {
  setMotorDirection( motorNumber, direction );
  setMotorSpeed( motorNumber, speed );
}

/**  Set the direction as of M1/M2 to forward/backward.
     motorNumber:        M1, M2
     direction:          Forward(LOW), Backward(HIGH) **/
void setMotorDirection( int motorNumber, int direction ) {
  digitalWrite( MotorPin[motorNumber].directionPin, direction);
}

/**Function to regulate & set speed of motor (PWM)**/
inline void setMotorSpeed( int motorNumber, int speed ) {
  /*Writes to pin the desired PWM output*/
  analogWrite(MotorPin[motorNumber].enPin, (255.0 * 6 / 6.8) * (speed / 100.0) ); //PWM
}







Any help is greatly appreciated as my project is due in uni tomorrow :(

Thanks,
Shanker

rtek1000

Quote
I was thinking to initialise (myservo.attach()) and detach (detach ()) the servo when in use as needed; then carry on using analogWrite to continue controlling the motor with the PWM
It makes no sense to me. The servo library is used with servo motor. PWM is usually used with DC motor which is different from servo motor.

Regarding the detach problem, it makes sense that this type of problem occurs because of the timer setting change, which is the timebase of these functions.
Please avoid private messages, your question may be someone's answer in the future!

Grumpy_Mike

Quote
I was thinking to initialise (myservo.attach()) and detach (detach ()) the servo when in use as needed; then carry on using analogWrite to continue controlling the motor with the PWM on pin 10.
No.

Even if the software could cope how would the hardware attached to this pin cope? It would still be receiving pulses when in the servo mode which would cause erratic control of the motor when you don't want it to move.

Go Up