// PPM Encoder Analog Add-ons for Devo7e // For use with Arduino Pro Mini 328P // J. Diehl 14/08/2015 // Based on a sketch by Ian Johnston 29/04/2010 // modified by Epyon and Cereal_Killer // Version 2.31 // Low cost way to add extra POT's to any radio that accepts PPM input for <$15 // using an arduino nano and a few POT's, visit DeviationTX.com for info. // POT's on A1 and A2, optional Thumbwheel on A3, optional switches on D6-D10 // Use On-Off-On switches on D6-D7, and D8-D9 for 3-way effect // PPM output on D13 // PPM inout to tip of 3.5 mono jack, this is the black wire on your Devo 7E DSC port // // This version disregards the Ring Shunt on D11 when shorted // // Original Sketch uses: // A1-3 Analog in for Pots 1, 2 and 3 // D6-9 Digital in for up to 2x3-way // D3-4 Digital in for 1 3-way or 2 2-way // D11 - Devo7e DSC Port "Ring Shunt" high; goes low when plug is inserted into DSC // D13 - PPM Output with LED int potPin_A1= A1; //Declare potPin_A1 to be analog pin A1 int potPin_A2= A2; //Declare potPin_A2 to be analog pin A2 int potPin_A3= A3; //Declare potPin_A3 to be analog pin A3 int AI_Raw_A1; // Analog In raw var - 0->1023 int AI_Raw_A2; // Analog In raw var - 0->1023 int AI_Raw_A3; // Analog In raw var - 0->1023 byte swPin_D1= 6; //Declare swPin_D1 to be Digital pin 6 byte swPin_D2= 7; //Declare swPin_D2 to be Digital pin 7 byte swPin_D3= 8; //Declare swPin_D3 to be Digital pin 8 byte swPin_D4= 9; //Declare swPin_D4 to be Digital pin 9 byte swPin_D5= 3; //Declare swPin_D5 to be Digital pin 3 byte swPin_D6= 4; //Declare swPin_D6 to be Digital pin 4 int outPinPPM = 13; // Declare ppmPin to be arduino pin 13 int Fixed_uS = 300; // PPM frame fixed LOW phase int pulseMin = 750; // pulse minimum width minus start in uS int pulseMax = 1700; // pulse maximum width in uS int pulseMid = pulseMin + (pulseMax - pulseMin) / 2; float adjustRng = ( pulseMax - pulseMin ) / 1023.0; // adjust range from 0-1023 int A1_uS = pulseMin; // Analog 1 uS var int A2_uS = pulseMin; // Analog 2 uS var int A3_uS = pulseMin; // Analog 3 uS var int sw1_uS = pulseMin; // Switch 1 combines result from swPin_D1 and D2 int sw2_uS = pulseMin; // Switch 2 combines result from swPin_D3 and D4 int sw3_uS = pulseMin; // Switch 3 combines result from swPin_D5 and D6 int sw4_uS = pulseMin; // Switch 4 ISR(TIMER1_COMPA_vect) { ppmoutput(); // Jump to ppmoutput subroutine } void setup() { pinMode(potPin_A1, INPUT); //set potPin_A1 to be an input pinMode(potPin_A2, INPUT); //set potPin_A2 to be an input pinMode(potPin_A3, INPUT); //set potPin_A3 to be an input pinMode(swPin_D1, INPUT); //set swPin_D1 to be an input digitalWrite(swPin_D1, HIGH); // turn on pull-up resistor pinMode(swPin_D2, INPUT); //set swPin_D2 to be an input digitalWrite(swPin_D2, HIGH); // turn on pull-up resistor pinMode(swPin_D3, INPUT); //set swPin_D3 to be an input digitalWrite(swPin_D3, HIGH); // turn on pull-up resistor pinMode(swPin_D4, INPUT); //set swPin_D4 to be an input digitalWrite(swPin_D4, HIGH); // turn on pull-up resistor pinMode(swPin_D5, INPUT); //set swPin_D5 to be an input digitalWrite(swPin_D5, HIGH); // turn on pull-up resistor pinMode(swPin_D6, INPUT); //set swPin_D6 to be an input digitalWrite(swPin_D6, HIGH); // turn on pull-up resistor pinMode(outPinPPM, OUTPUT); //set ppmPin to be an OUTPUT digitalWrite(outPinPPM, HIGH); // turn on LED // Setup timer TCCR1A = B00110001; // Compare register B used in mode '3' TCCR1B = B00010010; // WGM13 and CS11 set to 1 TCCR1C = B00000000; // All set to 0 TIMSK1 = B00000010; // Interrupt on compare B TIFR1 = B00000010; // Interrupt on compare B OCR1A = 22000; // 22mS PPM output refresh OCR1B = 1000; // Serial.begin(9600); // turn on Serial Port } void ppmoutput() { // PPM output sub // pinMode(outPinPPM, OUTPUT); //set ppmPin to be an OUTPUT // Channel 1 - Analog 1 digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(A1_uS); // Hold for A1_uS microseconds // Channel 2 - Analog 2 digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(A2_uS); // Hold for A2_uS microseconds // Channel 3 - Analog 3 digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(A3_uS); // Hold for A3_uS microseconds // Channel 4 - Switch 1 3-way digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(sw1_uS); // Hold for sw1_uS microseconds // Channel 5 - Switch 2 3-way digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(sw2_uS); // Hold for sw2_uS microseconds // Channel 6 - Switch 3 3-way digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(sw3_uS); // Hold for sw3_uS microseconds /*// Channel 7 - Switch 4 digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(sw4_uS); // Hold for sw4_uS microseconds // Channel 8 - Switch 5 digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); delayMicroseconds(sw5_uS); // Hold for sw5_uS microseconds */ // Synchro pulse digitalWrite(outPinPPM, LOW); delayMicroseconds(Fixed_uS); // Hold digitalWrite(outPinPPM, HIGH); // Start Synchro pulse /* Serial.print("You are writing a value of "); //for debugging print your values Serial.print(A1_uS); Serial.print(" "); Serial.print(A2_uS); Serial.print(" "); Serial.println(A3_uS); Serial.print(" "); Serial.print(sw1_uS); Serial.print(" "); Serial.print(sw2_uS); Serial.print(" "); Serial.println(sw3_uS); Serial.print(" "); Serial.print(sw4_uS); Serial.print(" "); Serial.print(sw5_uS); Serial.print(" "); Serial.println(" End"); */ } void loop() { // logic to read each analog input // Read analog pins AI_Raw_A1 = analogRead(potPin_A1); // Analog Pot 1 AI_Raw_A2 = analogRead(potPin_A2); // Analog Pot 2 AI_Raw_A3 = analogRead(potPin_A3); // Thumbwheel Pot 3 // Map analog inputs to PPM rates for each of the channels // compensate for analog 0-1023 vs expected 750-1700 A1_uS = AI_Raw_A1 * adjustRng + pulseMin; A2_uS = AI_Raw_A2 * adjustRng + pulseMin; A3_uS = AI_Raw_A3 * adjustRng + pulseMin; // Check limits if (A1_uS <= pulseMin) A1_uS = pulseMin; // Min if (A1_uS >= pulseMax) A1_uS = pulseMax; // Max if (A2_uS <= pulseMin) A2_uS = pulseMin; // Min if (A2_uS >= pulseMax) A2_uS = pulseMax; // Max if (A3_uS <= pulseMin) A3_uS = pulseMin; // Min if (A3_uS >= pulseMax) A3_uS = pulseMax; // Max if (digitalRead(swPin_D1) != 1) { // 3-way Switch 1 sw1_uS = pulseMin; } else { if (digitalRead(swPin_D2) != 1 ) { sw1_uS = pulseMax; } else { sw1_uS = pulseMid; } } if (digitalRead(swPin_D3) != 1) { // 3-way Switch 2 sw2_uS = pulseMin; } else { if (digitalRead(swPin_D4) != 1 ) { sw2_uS = pulseMax; } else { sw2_uS = pulseMid; } } if (digitalRead(swPin_D5) != 1) { // 3-way Switch 3 sw3_uS = pulseMin; } else { if (digitalRead(swPin_D6) != 1) { sw3_uS = pulseMax; } else { sw3_uS = pulseMid; } } }