Adding a limit switch to stop a pwm motor code.

@WizenedEE

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

Analogwrite is the way to go

Did you read the duty cycle and frequency from the OP's post? It's between about 6 and 9% duty cycle at a little under 50Hz.

I have modified the code so it can now work with a castle link esc 160 amp or other ESC's.

  1. Added the needed colons
  2. ESC wouldn't work with 255 so changed forward to idle speed for testing is 130 and 180 for full speed. 3 Took off the reverse because the ESE is for planes only so If you have a car ESE that would work for you.

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,180) ;}

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 } else {; // limit switch pulled low motor_fwd(); }

limit_sw_state_last = limit_sw_state_cur;

}

-------> One question I have yet Is in this code the limit switch is needed to be clicked once to run then hold the limit switch in manually, and the limit switch twice and it causes a brake/stop. I have to hold down the limit switch to brake and once released the motor begins running.

What I am trying to do is to make it so the arduino once powered on runs the code and motor running if the limit switch is clicked that the switch acts like a latched switch and holds the brake without me holding down the switch. I tried digital write and I am still learning how to write code. Other than that the code works great. To power on the arduino, I would just power it on with another switch.

Any help would be apreciated Thanks!!

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

If you are not yet at the point in your learning of C/C++ that you know where ; belong, and where they do not, I think you need to put the Arduino away for a while, and concentrate on learning the language.

  1. Added the needed colons

C uses very few colons. Semicolons, on the other hand, are used in a lot more places, but no where near as many as you are indiscriminately sprinkling them.

  1. ESC wouldn’t work with 255 so changed forward to idle speed for testing is 130 and 180 for full speed.

What happened why you tried? An ESC typically has a range of values, similar to a servo, that it moves to. Why are you PWMing an ESC anyway? Typically, they are driven using the Servo library?

3 Took off the reverse because the ESE is for planes only so If you have a car ESE that would work for you.

What’s an ESE? Why are you removing functionality that others might need?

It is ESC. It is a motor controller used for airplanes. I'm sure you could have figured that one out. I'm using it for a project that is light weight so that is why I chose it. As for the semicolons the first code above didn't work with out them, and debugging it that is where arduino said the semicolons needed to go. As I said I am fairly new at this, but doesn't mean I should give up. I already am achieving what I came to achieve, and that is to make a ESC brake with a limit switch in a certain way. If people want the original code it is above. And if you come on here to criticize then you need to get a day job. :P

analogWrite(ESC_PWM_PIN,180)

How many more times do we need to tell you not to use analogWrite?

As for the semicolons the first code above didn’t work with out them, and debugging it that is where arduino said the semicolons needed to go.

There are plenty of C examples available, and I’ve never seen this

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

as an example of semicolon usage before.
The first semicolon is an empty statement and doesn’t need to be there. The second belongs at the end of the analogWrite statement, not on the next line.

(And before anyone jumps in, yes, I know the compiler doesn’t care which line the semicolon goes on or whether or not an empty statement is there, but the next poor sod who has to read it might)

The funny thing is that the arduino took it with all the semicolons. If it works than it works for me. Your right I should use analog write I meant to put that instead. But back to my problem after the first click of the limit switch the motor runs and then have to hold the switch in to brake. What I was trying to do is make it so that the second click of the switch that it locks the brakes instead of me holding the switch. The rest of the code works great. One thing that I had no idea is that the ESC needed to be told to brake were I had origanially thought that it senses not input and then brakes, but that is why the 120 sent to the ESE, so I understand that after seeing the code.

How many more times do we need to tell you not to use analogWrite?

Your right I should use analog write I meant to put that instead. But back to my problem

The problem seems to one of reading comprehension.

Your right I should use analog write

NO you should use Servo.write.

The funny thing is that the arduino took it with all the semicolons. If it works than it works for me.

As I said, the compiler doesn't care how much whitespace exists between the end of a statement and the semicolon, so long as the statement is lexically correct. However, the next person who reads your code may decide that your eccentric, non-standard layout suggests that either a) You're a maverick who simply doesn't care or b) ignorant.

The rest of the code works great

I wonder how stressed the ESC is, and by how much you may have shortened its working life.

Ah Servo.write Thanks!! If I come across as ignorant sorry, Just wanting to learn. I think the ESC will handle it. It is a 160 amp with data logging can monitor pretty much everything from temperature to current. I am an electronics expert so if anything brakes I can fix. But the coding I am not so good at since I got my arduino 4 month ago. I am just optimistic. Can't put a man on the moon if you don't try!! :D