Throttle fail/save Code

Hey Guys,

My name is Lucas and i'm buiding an RC troll motor and had a question about some coding part. I have a simple RC PWM to 0-100% PWM code running on my arduino.

But when i turn on the arduino and lets say I forgot that the throttle is all the way up I don't want the motor to start spinning full force.

Is there maybe an easy code that only outputs the PWM when the throttle first has been to zero?

This is the code I have running right now:

 /* RCtoPWM by Phillip Schmidt, 08/14
 *
 *  Attach the RC  signal to pin 3
 *  Attach the PWM output to pin 9
 *
 */

#define MinPulseLength 1350  // these are pretty typical numbers for many RC systems
#define MaxPulseLength 2140

int RCinputPin = 3;
int pin3interrupt = 1; // for Atmega328 based boards

int PWM_Pin = 9; // PWM output pin

unsigned long volatile RCpulseLastChangeTime = 0;



void setup() {
  
  pinMode(PWM_Pin, OUTPUT);
  attachInterrupt(pin3interrupt, pinChangeISR, CHANGE); // call the function pinChangeISR each time the input changes state

}

void loop() {
  
  // signal drop safety check
  if(micros() - RCpulseLastChangeTime > 500000) // check if it has been more then 0.5s since last pulse was seen
  {
    analogWrite(PWM_Pin, 0); // set PWM output to zero
  }
  
  delay(100); // wait 0.1s before checking again

}

void pinChangeISR()  // here's where the magic happens
{
  unsigned long timeNow = micros(); // record time
  
  if(digitalRead(RCinputPin)) // pulse now high
  {
    RCpulseLastChangeTime = timeNow; // save the time when the pulse went high
  }
  else // pulse now low
  {
    int pulseLength = int(timeNow - RCpulseLastChangeTime); // measure pulse length
    pulseLength  = constrain(pulseLength, MinPulseLength, MaxPulseLength); // correct bad readings
    analogWrite(PWM_Pin, map(pulseLength, MinPulseLength, MaxPulseLength, 0, 255)); // update PWM signal
  }
}

With +-1350 being my throttle zero and +-2140 being full throttle.

Thank you very much! (I'm new to arduino)

Lucas

Lucas112: Hey Guys,

My name is Lucas and i'm buiding an RC troll motor and had a question about some coding part. I have a simple RC PWM to 0-100% PWM code running on my arduino.

But when i turn on the arduino and lets say I forgot that the throttle is all the way up I don't want the motor to start spinning full force.

Is there maybe an easy code that only outputs the PWM when the throttle first has been to zero?

This is the code I have running right now:

 /* RCtoPWM by Phillip Schmidt, 08/14
 *
 *  Attach the RC  signal to pin 3
 *  Attach the PWM output to pin 9
 *
 */

define MinPulseLength 1350  // these are pretty typical numbers for many RC systems

define MaxPulseLength 2140

int RCinputPin = 3; int pin3interrupt = 1; // for Atmega328 based boards

int PWM_Pin = 9; // PWM output pin

unsigned long volatile RCpulseLastChangeTime = 0;

void setup() {     pinMode(PWM_Pin, OUTPUT);   attachInterrupt(pin3interrupt, pinChangeISR, CHANGE); // call the function pinChangeISR each time the input changes state

}

void loop() {     // signal drop safety check   if(micros() - RCpulseLastChangeTime > 500000) // check if it has been more then 0.5s since last pulse was seen   {     analogWrite(PWM_Pin, 0); // set PWM output to zero   }     delay(100); // wait 0.1s before checking again

}

void pinChangeISR()  // here's where the magic happens {   unsigned long timeNow = micros(); // record time     if(digitalRead(RCinputPin)) // pulse now high   {     RCpulseLastChangeTime = timeNow; // save the time when the pulse went high   }   else // pulse now low   {     int pulseLength = int(timeNow - RCpulseLastChangeTime); // measure pulse length     pulseLength  = constrain(pulseLength, MinPulseLength, MaxPulseLength); // correct bad readings     analogWrite(PWM_Pin, map(pulseLength, MinPulseLength, MaxPulseLength, 0, 255)); // update PWM signal   } }





With +-1350 being my throttle zero and +-2140 being full throttle. 

Thank you very much!
(I'm new to arduino)

Lucas

I looked through your code several times and I can find nothing identified as "throttle". What is it and where do you do anything with it?

Paul

Hey Paul,

This is a simple code to convert RC PWM to real PWM.

My RC reciever gives 1350(no throttle) to 2140 (full throttle) Pulselength to pin 3 and the code converts it to 0-100% PWM out pin 9 to go into a PWM to voltage board that is connected to a speedcontroller.

All I want is that if the board recieves a input signal on pin 3 that is above 1350 it gives no output on pin 9 untill I pull the throttle down and up again.

Hope this is clear :)

Lucas

Create a global boolean called something like ThrottleActivated and initialize it to false. Check that it's true before you issue any non-zero analogWrites. When you get a pulse width that's less than say 1360 (to give a bit of margin) set ThrottleActivated to true.

Thanks! I’ll try :)