loading

Arduino code

Hello, I have been trying to make code for my arduino. I would like code to tell my arduino to receive a servo signal from my rc receiver, then detect whether the signal is telling the esc to move forwards or backwards. If the signal is forwards, it simply relays that code to the ESC. If the signal tells the ESC to go backwards, then it sends a signal to go forwards at the speed it would have gone in reverse, while sending out a power source to power a relay. Chowmix12 came up with some code that may help and I put  "//" before all of the code that was not needed for his application to work. Also, this link may be helpful for multitasking. 

#include <Servo.h>

Servo ThrottleServo;

//int PWMpin = 2; // connect RC receiver here.

//int ServoVal;

//int RCinput;

void setup()

{

//Serial.begin(9600);

//ThrottleServo.attach(9); // attaches the servo on pin 9 to the servo object

//ThrottleServo.write(97); // go to nuetral

pinMode(13, OUTPUT);

}

void loop()

{

//RCinput = pulseIn(PWMpin, HIGH, 20000);

//ServoVal = map(RCinput, 520, 2370, 0, 180);

//if(RCinput== 0) { // Signal timed out!

digitalWrite(13, HIGH); // ALERT!

ThrottleServo.write(97); // Go to neutral throttle position

}

//else{

// digitalWrite(13, LOW); // everything's fine.

// ThrottleServo.write(ServoVal); // Repeat the data to the truck's ESC

// Serial.print(" PulseMicros "); // This part is used to debug the values for calibration

// Serial.print(RCinput);

//Serial.print(" ServoRead ");

// Serial.println(ServoVal);

// }

//}

My program so far is as follows.

/* created 2005
by David A. Mellis
and chowmix12
modified by Justin Baker
*/
#include<Servo.h>

const int relay =  10;

Servo ThrottleServo;
int ServoVal;
int RCinput;
int PWMpin=2;
int relayState = LOW;      
long previousMillis = 0;   


long interval = 1000;         


void setup() {
  pinMode(relay, OUTPUT); 
Serial.begin(9600);
pinMode(9, OUTPUT); 
}


void loop()
{


RCinput = pulseIn(PWMpin, HIGH, 20000);

ServoVal = map(RCinput, 520, 2370, 0, 180);

if(ServoVal>90) {

digitalWrite(9, HIGH);

ThrottleServo.write(ServoVal);
}

else{
ServoVal=180-ServoVal;

digitalWrite(9, HIGH);

ThrottleServo.write(ServoVal); }

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
  
    previousMillis = currentMillis;  

    if (ServoVal >= 90)
      relayState = LOW;
    else
      relayState = HIGH; //this output will turn a mosfet off, turning the relay off
      digitalWrite(relay, relayState);
  }
}

sort by: active | newest | oldest

You seem to have commented out the stuff that actually reads the RC receiver signal !

jbaker22 (author)  steveastrouk2 years ago

Somehow it still works, do you know why?

What is the point of your question ? If it works, it drives your relay as you wished.

As it is, your program really looks like this to the compiler.

#include <Servo.h>
Servo ThrottleServo;
void setup()
{
pinMode(13, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH); // ALERT!
ThrottleServo.write(97); // Go to neutral throttle position
}

....and I don't see any reference to a relay there at all.

jbaker22 (author)  steveastrouk2 years ago

I can't figure out the part where I make the esc always go forwards (except at idle postion), at varying speed. I commented out most of the code, because you might be able to learn from chowmix12's code, but you can't really do much with that code the parts commented out. I am hoping to make it easier for someone to help me figure out what it takes to get it to create this code.

Try this minimalist version, and note my comments about what does what.


/* created 2005
by David A. Mellis and chowmix12 modified by Justin Baker*/

#include<Servo.h>
const int relay = 10;
Servo ThrottleServo;

int ServoVal;
int RCinput;
int PWMpin=2;
int relayState = LOW;

void setup() {
pinMode(relay, OUTPUT);

pinMode (PWMpin,INPUT);
}


void loop()
{
RCinput = pulseIn(PWMpin, HIGH, 20000); //measure input pulse
ServoVal = map(RCinput, 520, 2370, 0, 180); //scale input pulse
if(ServoVal>90) digitalWrite(relay, HIGH); //Turn ON reversing
else{

ServoVal=180-ServoVal;

digitalWrite(relay,LOW); //Turn OFF reversing
}

ThrottleServo.write(ServoVal);
}

jbaker22 (author)  steveastrouk2 years ago

This code lets me have little esc control. The esc goes fast and the speed sent gradually declines, then it does that again. The only control is if I tell the receiver to go in reverse, then there is is a short burst in the opposite direction before it starts the loop to go fast and decline in speed.

/* created 2005

by David A. Mellis

modified 8 Feb 2010

by Paul Stoffregen

http://www.arduino.cc/en/Tutorial/BlinkWithoutDela...

*/

#include<Servo.h>

const int relay=10;//relay pin

Servo ThrottleServo;//used to set the relay

int ServoVal;// create servo object to control a servo

int RCinput;//stores receiver's voltage

int PWMpin=2;//the pin where you connect to receiver data pin

int relayState = HIGH;//keeps mosfet off

long previousMillis = 0; //last time led was updated

long interval = 1000; //needed for interval for confusing multitasking code

void setup() {

pinMode(relay, OUTPUT); //make relay spot an output

ThrottleServo.attach(9); // attaches the servo on pin 9 to the servo object

pinMode(9,OUTPUT);//make servo an output,a needed part of chowmix's code

pinMode(PWMpin,INPUT);//chow mix did not say you need to declare the PWM, but to be safe...

}

void loop()

{

RCinput = pulseIn(PWMpin, HIGH,20000);//read frequency of receiver

ServoVal = map(RCinput, 520, 2370, 0, 180);//scale input pulses

if(ServoVal>90) {

digitalWrite(9,HIGH);//code from chowmix

ThrottleServo.write(ServoVal); //should send signal to that position it reads

}

else{

ServoVal=180-ServoVal;//reversing

digitalWrite(9,HIGH);

ThrottleServo.write(ServoVal);

}

unsigned long currentMillis = millis();//seconds progam has been running

if(currentMillis - previousMillis > interval) { //make sure relay was updated in under a second

previousMillis = currentMillis;

if (ServoVal >= 90)

relayState = LOW;//this will leave a mosfet on, turning the relay on

else relayState = HIGH;

digitalWrite(relay, relayState);//set relay state

}

}

So what did my code do ? Your code is full of stuff you can't justify, like the digitalWrite (9, ) , what does that DO. And why do you mess around with a little timer ? That whole timer section seems to mess up what you say you want to do.

jbaker22 (author)  steveastrouk2 years ago

Your code made my esc go as forward no matter what and the relay did not work:(. The timer thing is supposed to multitask. Do you know how else to run a servo and control a servo at the same time?

The timers are hardware, that once set, run on their own. The servo code is a good example. AFAIK PulseIn is compatible with the servo library. You aren't "multitaksing" - which the arduino can only do under pressure, you're making a measurement, and telling the hardware to get on with it, and you want that to happen as fast as you can. That's why I don't see the point of the interval code at all.

Try putting a serial print statement in, and displaying the result of PulseIn. Make sure that's working.

Define your forward and backward numbers ? Do I assume you want "90" to be "stopped", 0 full forwards and 180 full backwards ?

jbaker22 (author)  steveastrouk2 years ago

Thank you very much for your help and code. I simply added "ThrottleServo.attach(9);" in the void setup and changed "ServoVal=180-ServoVal;" to 187 to make it work like a champ.

Oops. My bad. I missed that line commented out in the original code - I automatically skip comments when I read code quickly!

Was I right about how you want the codes to run ? If I am then ServoVal = abs(ServovVal-90) should work too.