-
-
Save boraini/5988cb1d312b6d6e4a6eb6d57e0b7711 to your computer and use it in GitHub Desktop.
PPM to PWM Conversion Program for Arduino Pro Mini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 2019 Boraini. | |
* | |
* All permissions are hereby granted to the end user. Use at your own risk. | |
*/ | |
//pin definitions. Using pin 2 is mandatory since it is the hardware interrupt pin of Pro Mini. | |
#define RX_PIN 2 | |
#define OUT_PIN 9 | |
#define ENABLE_PIN 13 | |
//RX pulse separation in microseconds | |
#define MINSIGNAL 1000 | |
#define MAXSIGNAL 2000 | |
//interval between motor output updates | |
#define UPDATE_INTERVAL 100 | |
//volatile declarations are necessary to be able to manipulate the values during an interrupt. | |
unsigned volatile long sigStart; //32 bits | |
unsigned volatile short sig; //16 bits | |
bool bin; //Logical | |
void setup() { | |
//RX reading is available in serial for convenience. | |
Serial.begin(9600); | |
pinMode(RX_PIN, INPUT); | |
pinMode(ENABLE_PIN, OUTPUT); | |
pinMode(OUT_PIN, OUTPUT); | |
//enable L298N | |
digitalWrite(ENABLE_PIN, HIGH); | |
//we detect both the rising and falling edges of the RX signal | |
attachInterrupt(digitalPinToInterrupt(RX_PIN), calcThrottle, CHANGE); | |
} | |
//this somehow works | |
void calcThrottle() { | |
if (digitalRead(RX_PIN)) | |
{ | |
//begin counting | |
sigStart = micros(); | |
} | |
else | |
{ | |
//end counting | |
sig = (micros() - sigStart); | |
} | |
} | |
void loop() { | |
//we disable interrupts "just in case" | |
noInterrupts(); | |
analogWrite(OUT_PIN, map(sig, MINSIGNAL, MAXSIGNAL, 0, 255)); | |
interrupts(); | |
//this doesn't block the readings since we use interrupts. Yay! | |
delay(UPDATE_INTERVAL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment