Skip to content

Instantly share code, notes, and snippets.

@vo
Last active October 18, 2015 15:05
Show Gist options
  • Save vo/316ee786afa2b0fbf4c6 to your computer and use it in GitHub Desktop.
Save vo/316ee786afa2b0fbf4c6 to your computer and use it in GitHub Desktop.
Teensyduino code to output swtich data on PPM
#include <PulsePosition.h>
#define PIN_PPM;
const int pin_ppm = 10;
const int pin_led = 13;
const int pin_master = 22;
PulsePositionOutput ppmOut;
int master_switch_on;
int save_state[9];
void setup() {
pinMode(pin_led, OUTPUT);
digitalWrite(pin_led, HIGH);
ppmOut.begin(pin_ppm);
master_switch_on = 0;
// initialize the save state for the analog switches
for(int i=0; i<9; ++i) {
save_state[i] = analogRead(14+i);
}
}
// calcuate PPM value to output given an analog value input
int calc_ppm(int val) {
if (val < 300) return 1000;
else if (val < 900) return 2000;
else return 1500;
}
// check if the master switch has changed
// return true if so
boolean master_changed() {
int val = analogRead(pin_master);
if(abs(val - save_state[8]) >= 100) {
return true;
}
return false;
}
// check if any slave switch has changed
// return true if so
boolean slave_changed() {
boolean changed = false;
for(int i=0; i<8; ++i) {
int val = analogRead(14+i);
if(abs(val - save_state[i]) >= 100) {
return true;
}
}
return false;
}
void loop() {
if (master_switch_on == 1) {
// master switch mode
if(slave_changed()) {
master_switch_on = 0;
} else {
int master_read=analogRead(pin_master);
for(int i=0; i<8; ++i) {
ppmOut.write(i+1, calc_ppm(master_read));
}
save_state[8]=master_read;
delay(100);
}
} else {
// slave switch mode
if(master_changed()) {
master_switch_on = 1;
} else {
for(int i=0; i<8; ++i) {
int pin_read=analogRead(14+i);
ppmOut.write(i+1, calc_ppm(pin_read));
save_state[i]=pin_read;
}
delay(100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment