Käytin sitä sen ensimmäisen version koodia pohjana. Eli tuossa ei ole mitään näyttöjä tai summereita. Samoten poistelin ylimääräisiä skaalauksia ja multi raten siitä. Lisäyksinä sitten läjä nappeja digipinneihin.. Ei enää riittänyt napit laipoille asti, niin sen tein on-off-on -kytkimestä ja vastusjaosta niin sai kolme asentoa. Niin ja signaalit on invertoitu, kun käytän fettiä open-collectorina.
Vielä vähän hiomista tuossa koodissa. Esim. trimmit pitää vielä muuttaa reunaherkiksi, nyt vaan lisäävät tai poistavat trimmistä aina kun nappi on pohjassa, eli käytännössä paukauttavat trimmit jompaan kumpaan laitaan. Samoten pulssit pitää vielä laittaa oikean mittaisiksi, nyt eivät ole tuota 1500 us keskellä. Jotain tuohon suuntaan kuitenkin. Pinninumeroinnit yms. menevät toki uusiksi, mutta ei ole monimutkainen tuo koodi.
/*Analogue Inputs pins:
Pin 0 = Elevator potentiometer
Pin 1 = Aeleron potentiometer
Pin 2 = Rudder potentiometer
Pin 3 = Throttle potentiometer
Pin 4 = Aux potentiometer
Pin 5 =
Digital Input pins:
Pin 2 =
Pin 3 = Trim right
Pin 4 = Trim left
Pin 5 = Hat up
Pin 6 = Trigger
Pin 7 = Hat down
Pin 8 = Thumb Middle
Pin 9 = Hat left
Pin 10 = Thumb Right
Pin 11 = Hat right
Pin 12 = Thumb Left
Pin 13 =
PPM Spektrum:
1 Throttle
2 Ailerons
3 Vertical stabilizer
4 Rudder
5 Aux2 or Gear
6 Aux2 or Gear
7 Usually a mix
*/
int AI_Pin_AEL = 4; // Ana In - Aeleron potentiometer (Ana In Ch.0 playing up?)
int AI_Pin_ELE = 1; // Ana In - Elevator potentiometer
int AI_Pin_THR = 3; // Ana In - Throttle potentiometer
int AI_Pin_RUD = 2; // Ana In - Rudder potentiometer
int AI_Pin_FLAP = 5; // Ana In - three state FLAP input with voltage divider
int AI_Raw_AEL; // Ana In raw var - 0->1023
int AI_Raw_ELE; // Ana In raw var - 0->1023
int AI_Raw_THR; // Ana In raw var - 0->1023
int AI_Raw_RUD; // Ana In raw var - 0->1023
int AI_Raw_FLAP; // Ana In raw var - 0->1023
int AI_AEL; // Ana In var - 0->1023 compensated
int AI_ELE; // Ana In var - 0->1023 compensated
int AI_THR; // Ana In var - 0->1023 compensated
int AI_RUD; // Ana In var - 0->1023 compensated
int AI_FLAP; // Ana In var - 0->1023 compensated
int Aeleron_uS = 1100; // Ana In Ch.0 uS var - Aeleron
int Elevator_uS = 1100; // Ana In Ch.1 uS var - Elevator
int Throttle_uS = 1100; // Ana In Ch.2 uS var - Throttle
int Rudder_uS = 1100; // Ana In Ch.3 uS var - Rudder
int Flap_uS = 1120; // Ana In Ch.3 uS var - Rudder
// omat lisäykset:
int Hat_horizontal_uS = 1120;
int Hat_vertical_uS = 1120;
int Hat_horizontal_lowpass = 1120;
int Hat_vertical_lowpass = 1120;
int Trim_Aeleron = 50; // Thumb left/right
int Trim_Elevator = 50; // trigger / center
int Trim_Rudder = 50; // left / right
int Fixed_uS = 400; // PPM frame fixed LOW phase
int pulseMin = 600; // pulse minimum width minus start in uS
int pulseMax = 1600; // pulse maximum width in uS
int outPinPPM = 2; // digital pin 10
int inPinD3 = 3; // Trim right
int inPinD4 = 4; // Trim left
int inPinD5 = 5; // Hat up
int inPinD6 = 6; // Trigger
int inPinD7 = 7; // Hat down
int inPinD8 = 8; // Thumb middle
int inPinD9 = 9; // Hat left
int inPinD10 = 10; // Thumb right
int inPinD11 = 11; // Hat right
int inPinD12 = 12; // Thumb left
int outPinTEST = 13; // digital pin 8
ISR(TIMER1_COMPA_vect) {
ppmoutput(); // Jump to ppmoutput subroutine
}
void setup() {
// Serial.begin(9600) ; // Test
pinMode(outPinPPM, OUTPUT); // sets the digital pin as output
pinMode(inPinD3, INPUT); // sets the digital pin as input
digitalWrite(inPinD3, HIGH); // turn on pull-up resistor
pinMode(inPinD4, INPUT); // sets the digital pin as input
digitalWrite(inPinD4, HIGH); // turn on pull-up resistor
pinMode(inPinD5, INPUT); // sets the digital pin as input
digitalWrite(inPinD5, HIGH); // turn on pull-up resistor
pinMode(inPinD6, INPUT); // sets the digital pin as input
digitalWrite(inPinD6, HIGH); // turn on pull-up resistor
pinMode(inPinD7, INPUT); // sets the digital pin as input
digitalWrite(inPinD7, HIGH); // turn on pull-up resistor
pinMode(inPinD8, INPUT); // sets the digital pin as input
digitalWrite(inPinD8, HIGH); // turn on pull-up resistor
pinMode(inPinD9, INPUT); // sets the digital pin as input
digitalWrite(inPinD9, HIGH); // turn on pull-up resistor
pinMode(inPinD10, INPUT); // sets the digital pin as input
digitalWrite(inPinD10, HIGH); // turn on pull-up resistor
pinMode(inPinD11, INPUT); // sets the digital pin as input
digitalWrite(inPinD11, HIGH); // turn on pull-up resistor
pinMode(inPinD12, INPUT); // sets the digital pin as input
digitalWrite(inPinD12, HIGH); // turn on pull-up resistor
// 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;
}
void ppmoutput() { // PPM output sub
/*
1 Throttle
2 Ailerons
3 Vertical stabilizer
4 Rudder
5 Aux2 or Gear
6 Aux2 or Gear
7 Usually a mix
*/
// test pulse - used to trigger scope
// digitalWrite(outPinTEST, LOW);
// delayMicroseconds(100); // Hold
// digitalWrite(outPinTEST, HIGH);
// Channel 1
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Throttle_uS); // Hold for Aeleron_uS microseconds
// Channel 2
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Aeleron_uS); // Hold for Elevator_uS microseconds
// Channel 3
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Elevator_uS); // Hold for Throttle_uS microseconds
// Channel 4
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Rudder_uS); // Hold for Rudder_uS microseconds
// Channel 5 - Hat vertical
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Hat_vertical_uS); // Hold for hat vert microseconds
// Channel 6 - Hat horizontal
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Hat_horizontal_uS); // Hold for hat hor microseconds
// Channel 6 - Flap
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH);
delayMicroseconds(Flap_uS); // Hold for flap microseconds
// Synchro pulse
digitalWrite(outPinPPM, LOW);
delayMicroseconds(Fixed_uS); // Hold
digitalWrite(outPinPPM, HIGH); // Start Synchro pulse
}
void loop() { // Main loop
// Read analogue ports
AI_Raw_AEL = analogRead(AI_Pin_AEL);
AI_Raw_ELE = analogRead(AI_Pin_ELE);
AI_Raw_THR = analogRead(AI_Pin_THR);
AI_Raw_RUD = analogRead(AI_Pin_RUD);
AI_Raw_FLAP = analogRead(AI_Pin_FLAP);
// Compensate for discrepancy in pot inputs including centering offset.
// Also use this to invert inputs if necessary (swap x1 & y1)
// y=mx+c, x to y scales to x1 to y1
AI_AEL = map(AI_Raw_AEL, 0, 1023, 1300, 0) - 50; // Invert Aeleron pot and slight centre offset
AI_ELE = map(AI_Raw_ELE, 0, 1023, 1300, 0) - 50; // Invert Elevator pot and slight centre offset
AI_THR = map(AI_Raw_THR, 0, 1023, 1300, 0) + 0; // Throttle
AI_RUD = map(AI_Raw_RUD, 0, 1023, 200, 1400) - 100; // Rudder
// Map flaps:
if (AI_Raw_FLAP <= 200){ AI_FLAP = 1120;} // Min
if (AI_Raw_FLAP >= 800){ AI_FLAP = 600;} // Max
if (AI_Raw_FLAP > 200 && AI_Raw_FLAP < 800){ AI_FLAP = 850;}
// flap rate reducer
if (AI_FLAP > Flap_uS) {Flap_uS += 5;}
if (AI_FLAP < Flap_uS) {Flap_uS -= 5;}
if (Flap_uS > AI_FLAP - 10 && Flap_uS < AI_FLAP + 10) {Flap_uS = AI_FLAP;}
// Map analogue inputs to PPM rates for each of the channels
Aeleron_uS = (AI_AEL) + pulseMin + Trim_Aeleron - 50 - 60;
Elevator_uS = (AI_ELE) + pulseMin + Trim_Elevator - 50 - 60;
Throttle_uS = (AI_THR) + pulseMin - 150;
Rudder_uS = (AI_RUD) + pulseMin + Trim_Rudder - 50 - 60;
// Check limits
if (Aeleron_uS <= 600) Aeleron_uS = 600; // Min
if (Aeleron_uS >= 1600) Aeleron_uS = 1600; // Max
if (Elevator_uS <= 600) Elevator_uS = 600; // Min
if (Elevator_uS >= 1600) Elevator_uS = 1600; // Max
if (Throttle_uS <= 600) Throttle_uS = 600; // Min
if (Throttle_uS >= 1600) Throttle_uS = 1600; // Max
if (Rudder_uS <= 600) Rudder_uS = 600; // Min
if (Rudder_uS >= 1600) Rudder_uS = 1600; // Max
if (Flap_uS <= 600) Flap_uS = 600; // Min
if (Flap_uS >= 1120) Flap_uS = 1120; // Max
if (Hat_horizontal_lowpass <= 600) Hat_horizontal_lowpass = 600; // Min
if (Hat_horizontal_lowpass >= 1600) Hat_horizontal_lowpass = 1600; // Max
if (Hat_vertical_lowpass <= 600) Hat_vertical_lowpass = 600; // Min
if (Hat_vertical_lowpass >= 1600) Hat_vertical_lowpass = 1600; // Max
if (Trim_Aeleron <= 0) Trim_Aeleron = 0; // Min
if (Trim_Aeleron >= 100) Trim_Aeleron = 100; // Max
if (Trim_Elevator <= 0) Trim_Elevator = 0; // Min
if (Trim_Elevator >= 100) Trim_Elevator = 100; // Max
if (Trim_Rudder <= 0) Trim_Rudder = 0; // Min
if (Trim_Rudder >= 100) Trim_Rudder = 100; // Max
if (digitalRead(inPinD10) == 0) { // Trim_Aeleron
Trim_Aeleron += 2;}
if (digitalRead(inPinD12) == 0) {
Trim_Aeleron -= 2;}
if (digitalRead(inPinD6) == 0) { // Trim_Elevator
Trim_Elevator += 2;}
if (digitalRead(inPinD8) == 0) {
Trim_Elevator -= 2;}
if (digitalRead(inPinD3) == 0) { // Trim_Rudder
Trim_Rudder += 2;}
if (digitalRead(inPinD4) == 0) {
Trim_Rudder -= -2;}
if (digitalRead(inPinD5) == 0) { Hat_vertical_uS = 1600; } // Hat vertical UP
if (digitalRead(inPinD7) == 0) { Hat_vertical_uS = 600; } // Hat vertical Down
if (digitalRead(inPinD5) == 1 && digitalRead(inPinD7) == 1) { Hat_vertical_uS = 1120; } // Hat vertical center
if ( Hat_vertical_lowpass < Hat_vertical_uS) { Hat_vertical_lowpass += 1; }
if ( Hat_vertical_lowpass > Hat_vertical_uS) { Hat_vertical_lowpass -= 1; }
if (Hat_vertical_lowpass > Hat_vertical_uS - 5 && Hat_vertical_lowpass < Hat_vertical_uS + 5)
{ Hat_vertical_lowpass = Hat_vertical_uS; }
if (digitalRead(inPinD9) == 0) { Hat_horizontal_uS = 1600; } // Hat horizontal LEFT
if (digitalRead(inPinD11) == 0) { Hat_horizontal_uS = 600; } // Hat horizontal RIGHT
if (digitalRead(inPinD9) == 1 && digitalRead(inPinD11) == 1) { Hat_horizontal_uS = 1120; } // Hat horizontal center
if ( Hat_horizontal_lowpass < Hat_horizontal_uS) { Hat_horizontal_lowpass += 1; }
if ( Hat_horizontal_lowpass > Hat_horizontal_uS) { Hat_horizontal_lowpass -= 1; }
if (Hat_horizontal_lowpass > Hat_horizontal_uS - 5 && Hat_horizontal_lowpass < Hat_horizontal_uS + 5)
{ Hat_horizontal_lowpass = Hat_horizontal_uS; }
/*
Serial.print(Throttle_uS + Fixed_uS);
Serial.print("\t");
Serial.print(Aeleron_uS + Fixed_uS);
Serial.print("\t");
Serial.print(Elevator_uS + Fixed_uS);
Serial.print("\t");
Serial.print(Rudder_uS + Fixed_uS);
Serial.print("\t");
Serial.print(Hat_vertical_lowpass + Fixed_uS);
Serial.print("\t");
Serial.print(Hat_horizontal_lowpass + Fixed_uS);
Serial.print("\t");
Serial.println(Flap_uS + Fixed_uS);
*/
}