Skip to content

Instantly share code, notes, and snippets.

@PhilippMeissner
Last active March 31, 2018 15:04
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 PhilippMeissner/9d4e303ba304f7cb22842ef9d3a03193 to your computer and use it in GitHub Desktop.
Save PhilippMeissner/9d4e303ba304f7cb22842ef9d3a03193 to your computer and use it in GitHub Desktop.
Receiver
#include <SPI.h>
#include <Wire.h>
#include <RF24.h>
// https://quadmeup.com/generate-ppm-signal-with-arduino/
#define CHANNEL_NUMBER 1 //set the number of channels
#define CHANNEL_DEFAULT_VALUE 1000 //set the default servo value
#define FRAME_LENGTH 22500 //set the PPM frame length in microseconds (1ms = 1000µs)
#define PULSE_LENGTH 300 //set the pulse length
#define ON_STATE 0 //set polarity of the pulses: 1 is positive, 0 is negative
#define SIGNAL_PIN 10 //set PPM signal output pin on the arduino
#define CE_PIN 7
#define CSN_PIN 8
#define SENDER_MIN_VALUE 0
#define SENDER_MAX_VALUE 1023
#define MOTOR_MIN_VALUE 1500
#define MOTOR_MAX_VALUE 2250
#define MAGIC_STORAGE_ADDRESS 0xE8E8F0F0E1LL
/*this array holds the servo values for the ppm signal
change theese values in your code (usually servo values move between 1000 and 2000)*/
int ppm[CHANNEL_NUMBER] = {CHANNEL_DEFAULT_VALUE};
RF24 radio(CE_PIN, CSN_PIN);
const uint64_t pipe = MAGIC_STORAGE_ADDRESS;
int message;
long mappedValue = 0;
void setup() {
Serial.begin(57600);
pinMode(SIGNAL_PIN, OUTPUT);
digitalWrite(SIGNAL_PIN, !ON_STATE); //set the PPM signal pin to the default state (off)
// C+P
cli();
TCCR1A = 0; // set entire TCCR1 register to 0
TCCR1B = 0;
OCR1A = 100; // compare match register, change this
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS11); // 8 prescaler: 0,5 microseconds at 16mhz
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
sei();
// Setup RECEIVER
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&message, sizeof(message));
// SET PPM
mappedValue = map(message, SENDER_MIN_VALUE, SENDER_MAX_VALUE, MOTOR_MIN_VALUE, MOTOR_MAX_VALUE);
ppm[0] = (int) mappedValue;
Serial.print("Message received: ");
Serial.println(message);
Serial.print("Value sent to ppm: ");
Serial.println((int) mappedValue);
delay(10);
} else {
Serial.println("Radio is dead.");
}
}
// C+P
ISR(TIMER1_COMPA_vect) {
static boolean state = true;
TCNT1 = 0;
if (state) { //start pulse
digitalWrite(SIGNAL_PIN, ON_STATE);
OCR1A = PULSE_LENGTH * 2;
state = false;
} else { //end pulse and calculate when to start the next pulse
static byte cur_chan_numb;
static unsigned int calc_rest;
digitalWrite(SIGNAL_PIN, !ON_STATE);
state = true;
if (cur_chan_numb >= CHANNEL_NUMBER) {
cur_chan_numb = 0;
calc_rest = calc_rest + PULSE_LENGTH;//
OCR1A = (FRAME_LENGTH - calc_rest) * 2;
calc_rest = 0;
}
else {
OCR1A = (ppm[cur_chan_numb] - PULSE_LENGTH) * 2;
calc_rest = calc_rest + ppm[cur_chan_numb];
cur_chan_numb++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment