Go Down

Topic: Adding a limit switch to stop a pwm motor code. (Read 4 times) previous topic - next topic

ds2ktj

I think you are going about solving your problem the wrong way.  This RC ESC is PWM based you could easily use analogWrite to control the frequency of the pin without servicing it in a busy loop.  analogWrite is the perfect way to do this.  Test it out with a servo hooked up to one of the PWM pins.  The Servo library is way overkill for controlling one PWM (servo/esc) and a limit switch (or two).

IE (pseudocode - not tested or compiled but similar to something I have used for higher powered motor contro):

Code: [Select]

#define ESC_PWM_PIN 3 // The pin the ESC/SERVO is connected to
#define LIMIT_SW_PIN 2 // The Limit Switch Pin (INT0)
                                 // Limit switch connected to VDD through 10K pullup
                                 //                  PIN 2
                                 //           10K    |
                                 //     VDD---VVVV---+----o/  o--- GND

void motor_fwd () {
   // FORWARD FULL SPEED
   analogWrite(ESC_PWM_PIN,255)
}

void motor_rev() {
   // REVERSE FULL SPEED
   analogWrite(ESC_PWM_PIN, 0)
}

void motor_brk() {
   // SET BRAKE ON MOTOR  (you might need to tune this value where the brake is set on the ESC - and I know the castle has a configurable brake)
   analogWrite(ESC_PWM_PIN, 120);
}



void setup () {
  // Initialize the ESC PIN as an output and set the brake
  pinMode(ESC_PWM_PIN,OUTPUT);
  motor_brk();
  // INT0 - PIN2 - call motor_brk when the switch closes
  attachInterrupt(0,motor_brk,LOW);
}


bool limit_sw_state_last = 0;

void loop() {
bool limit_sw_state_cur = digitalRead(LIMIT_SW_PIN);

   // toggle direction versus limit sw state.
   if (limit_sw_state_cur == limit_sw_state_last) {
     // do nothing
   } elseif (limit_sw_state) {
    // limit switch pulled high
    motor_rev();
  } else {
    // limit switch pulled low
    motor_fwd();
  }

  limit_sw_state_last = limit_sw_state_cur;

}

}



AWOL

#16
Apr 13, 2011, 06:07 pm Last Edit: Apr 13, 2011, 06:11 pm by AWOL Reason: 1
Quote
This RC ESC is PWM based

The output may be, but the input is usually PPM.

Quote
The Servo library is way overkill for controlling one PWM (servo/esc)

That depends on much abuse your servo/ESC can withstand.

Hint: Arduino PWM a shade under 500Hz with poor resolution in the 5 to 10% duty cycle region,versus Servo library at 50Hz with very good resolution.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Chadwick

Hey thanks,

I'll let you know if that works. As of now I just have to fix the 11 errors I was getting. I think I was going at this the wrong way. I am kinda lost on this one, it was more than I expected!!

Yeah I will probably only run the motor for 30min or less. It is a 12,000 RPM motor even though it is geared down so I kinda don't need that speed, I'll definately be tweeking the code.  For errors see attached pictures. :~

WizenedEE

Put semicolons after every line that doesn't have an open bracket in it.

Also, for those saying to use the servo library: He's not using a servo.

Analogwrite is the way to go  8)

Also, I hope you're not trying to drive the motor directly, and are using some sort of transistor, or else you'll blow out that pin on the arduino.

zoomkat

Quote
Also, for those saying to use the servo library: He's not using a servo.
Analogwrite is the way to go 


The OP said he is using an RC ESC with the motor. Most of these ESCs are made to be conrolled via servo control pulses (PPM) so they can be plugged directly into the RC receivers with which they are normally used. Bottom line, the OP should have some idea of how the equipment he has works.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Go Up