Skip to content

Instantly share code, notes, and snippets.

@firstnevyn
Last active October 23, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firstnevyn/0255d4702d28d7f3d017b232193123b1 to your computer and use it in GitHub Desktop.
Save firstnevyn/0255d4702d28d7f3d017b232193123b1 to your computer and use it in GitHub Desktop.
GardenRailway
#include <PulsePosition.h>
// The orange R610x only outputs 6 channels if we change to frsky this will need to be 16
const int8_t maxchans = 6 ;// this is nchannels
int8_t numchans = maxchans;
float rcvals[(maxchans+1)];
// ppm channel order for dsm2/dsmx
#define AILCHAN 1
#define ELECHAN 2
#define THRCHAN 3
#define RUDCHAN 4
#define AUXCHAN 5
#define GEARCHAN 6
//pin definitions for motor control
#define DEBUG_MOTOR 0 // set to 1 to print motor stuff
#define MCDIR1 33 // connected to inA 1
#define MCDIR2 34 // connected to inA 2
#define MCPPM 35 //connected to L298 inA enable
// Create PulsePosition and define a pin for the RX /MAX
#define DEBUGRC 0
PulsePositionInput myIn;
#define RCPPMINPUT 23 // recieves PPM from Orange R610x
void setup() {
myIn.begin(RCPPMINPUT);
pinMode(MCDIR1, OUTPUT);
pinMode(MCDIR2, OUTPUT);
Serial.println("Hi there we started...");
}
void getrcvalues() {
// Every time new data arrives, store it in an array
int8_t num = myIn.available();
// Check if there is new data
if (num > 0) {
numchans=num;
for (int8_t i = 1; i <=maxchans; i++) {
rcvals[i] = myIn.read(i);
}
if(num > maxchans){
numchans=maxchans; //reset the global to maxchans
Serial.print(num);
Serial.println(" <== ppm availible exceeds maxchans channels");
// read the myIn discarding the data as the frame is probably bogus
for (int8_t i = maxchans; i<=num; i++) {
myIn.read(i);
}
}
}
}
void printrcvalues() {
//only print if we have data.
//Serial.println(numchans);
for(int i=1; i<=maxchans; i++){
Serial.print("\t");
Serial.print(rcvals[i]);
}
Serial.println();
}
void controlmotor(int8_t dir, int16_t speed) {
switch (dir) {
case 1: // set forward
analogWrite(MCPPM, speed);
digitalWrite(MCDIR1, HIGH);
digitalWrite(MCDIR2, LOW);
//Serial.println("GOING FORWARD");
break;
case 0 : //set stop
analogWrite(MCPPM, 0);
digitalWrite(MCDIR1, HIGH);
digitalWrite(MCDIR2, LOW);
//Serial.println("GOING FISHIN'");
break;
case -1 : //set reverse
analogWrite(MCPPM, speed);
digitalWrite(MCDIR1, LOW);
digitalWrite(MCDIR2, HIGH);
// Serial.println("GOING BACkWARD");
break;
default ://set stop
analogWrite(MCPPM, 0);
digitalWrite(MCDIR1, LOW);
digitalWrite(MCDIR2, LOW);
//Serial.println("GOING NOWHERE");
break;
}
}
void loop() {
getrcvalues();
//Decode the aux channel and use it to set direction
int8_t motdir;
if (rcvals[AUXCHAN]>1800)
motdir=1;
else if (rcvals[AUXCHAN] <1300)
motdir = -1;
else
motdir = 0;
float throttle = (rcvals[THRCHAN]-1110)/3.34;
controlmotor(motdir, throttle);
if (DEBUGRC) {
printrcvalues();
}
if(DEBUG_MOTOR) {
Serial.print(numchans);
Serial.print(" ");
Serial.print(maxchans);
Serial.print(" ");
Serial.println(throttle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment