Adding a limit switch to stop a pwm motor code.

I am struggling writing code for the Arduino, since I am a newbie. :roll_eyes:

With that said what I am trying to do is to turn on the arduino remotely and it will run the code ramping up the motor traveling a such distance, so far done. But now adding a limit switch that will stop the motor/code and then have the PWM code stopped to no throttle or brake mode for the ESC Motor controller so the motor controller will do the braking. In this code I am trying to modify I don’t need the reverse code or it to loop, just forward if that helps. So maybe an end if statment if limit switch is reached?

int servoPin = 7;
int fullCounter = 1200; // 1.2ms
int stopped = 1500; // 1.5ms
int fullClock = 1800; // 1.8ms
int dirInt = 1500; //stopped to start
int dir = 1; //add 1 ms per loop untill hitting 1800, then -1 until 1200
void setup()
{
pinMode(servoPin,OUTPUT);
}
void loop()
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(dirInt);
digitalWrite(servoPin,LOW);
delay(20); // 20ms
dirInt += dir;
if(dirInt >= fullClock)
{
dir = -1;
} else if(dirInt <= fullCounter) {
dir = 1;
}
}

Do you have a really good reason not to use the servo library?

Perhaps trigger an interrupt using the limit switch?

You don't need an interrupt.

I know you don't, but if it's a complicated bit of code that runs while the motor is being driven then an interrupt would allow you to easily come to a safe, triggered stop when necessary without worrying about your loop having to constantly check the latch.

Ok I have gone to the Library and tried to incorporate push button switch called Limit switch into the code. Wich I am not very good at. :roll_eyes: I am just 3 months experience in this but a fast learner. So I am not sure I could actually run it as a loop because of the ramping up of the motor to start and trying to get the code to continuously stay at the high 1800 frequency speed until reaching the limit switch which stops the code/ making the esc do the braking. So far have tested the RC esc and motor and 0 throttle does do the braking.

As for the loop should I be calling it a while loop according to the Library? Still not sure how to go about ending the loop once the limit switch is triggered.

Problems: 1. Best way to Incorporate switch into code to end program for braking motor 2. Need code that will keep motor running at 1800 frequency after ramp up of speed 3. How or what does the code look like for an interrupt switch.

Here is what I have so far: I had taken out the revese code and tried adding the limit switch code.

int servoPin = 7; const int buttonPin = 2; // the number of the Limit switch pin

int fullCounter = 1200; // 1.2ms int stopped = 1500; // 1.5ms int fullClock = 1800; // 1.8ms

int dirInt = 1500; //stopped to start int dir = 1; //add 1 ms per loop untill hitting 1800, void setup() {

pinMode(servoPin,OUTPUT); }

while loop()//??????? {

digitalWrite(servoPin,HIGH); delayMicroseconds(dirInt); digitalWrite(servoPin,LOW); delay(20); // 20ms

// variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { digitalWrite(servoPin,Low); }

}

I did some research on an interrupt switch and I think that work because this will be outside and the would affect the switching already had been tried before. :(

At the risk of repeating myself, do you have a really good reason not to use the servo library?

Servo library seconded by me..

yeah, I wouldn't want you to have to repeat yourself again that would be too much work. You probably copied and pasted that. Man I want your job. :grin:

What he's getting at is that you're overcomplicating the code..

What function exactly do you need to perform? Turn a motor on (servo or otherwise), then run it until you hit a limit switch?

yeah, I wouldn't want you to have to repeat yourself again that would be too much work. You probably copied and pasted that. Man I want your job.

So how you doing so far? Got it working? ;)

Yep, forward and backward. But that is too much. He is right it is too complicated. What I am trying to do is modify the first code with out reverse so that will shorten the code and to keep the motor running add a limit switch to stop a motor running in the forward motion. I have essentially made a vertical train at high speeds that I am trying to get to run autonomously using the arduino Nano ATmega 328. When it hits a bumber at the top will end the code stoping the motor. The motor controller will do the braking (No code needed) on the way down when it senses no PWM's. At least tested with receiver. With that all said it will be pretty cool when its done.

So the limit switch just stops the motor and the code, that's it. The second code above is just reworking the first code and had to start somewhere. Maybe an interupt is needed I had at first thought an interupt was an ir sensor but reading other servo codes could be any switch? I am not very good at mixing codes so I am stuck with this switch thing. :fearful:

I'm going to post a bit of a re-code for you so you can learn from the code.

Can you tell me which servo you are using so I can program it properly. From what you've written so far it's impossible to tell what type you're using. Link me to the website where you bought it..

I got to thinking and searching around on the internet and what if a switch is triggered that it could keep
a pulse at 1200 1.2ms that would keep the motor stopped and brake the motor (would that work far as code?) (1200 = stopped and brake for motor esc I think.) But not sure how to have it stay at a certain speed like 1800 //1.8ms.

Here is the data requested:
HXT 80-85-A 250Kv Brushless Outrunner (eq: 70-40)
Link to motor:

Castle Link 160amp ice lite ESC -They don’t show the 160amp for some reason anymore.
http://www.castlecreations.com/products/phoenix_ice.html

and this is a good web page for understanding the throttle pulses(mostly on the bottom of the page.

speeds for motor:
(center was always 1.52ms) <— 1500
ATV 100%, DR 100%
Full up = 1.95 <— 1800
Full down = 1.10 <— 1200

From code:
int fullCounter = 1200; // 1.2ms - was below or off
int stopped = 1500; // 1.5ms - center stick and starting motor
int fullClock = 1800; // 1.8ms - Full speed

Thanks a bunch I appreciate your help!! XD

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):

#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;

}

}

This RC ESC is PWM based

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

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.

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. :~

code.JPG

error1.JPG

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.

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.