/* -------------------------------------------------------------------------------------------------------------------------------- Universel NVM (Nederlandse Vereniging voor Modelbouw) 14 Functies MultiSwitch Decoder Module V1.0 NVM - Maarten Flederus / Written by Marten de Groot "april 2016" * | Gereserveerde pinnen: * | | * | Digital 0: * | Digital 1: * | Digital 2: Receiver input * | Digital 3: Led This Led starts flashing when the FailSafe function is activated * | Digital 4: * | Digital 5: * | Digital 6: Output 1 This is a Moment Switch function * | Digital 7: Output 2 This is a Moment Switch function * | Digital 8: Output 3 This is a Memory Switch function * | Digital 9: Output 4 This is a Memory Switch function * | Digital 10: Output 5 This is a Memory Switch function * | Digital 11: Output 6 This is a Memory Switch function * | Digital 12: Output 7 This is a Memory Switch function * | Digital 13: indication LED * | Digital 14: Output 8 This is a Memory Switch function * | Digital 15: Output 9 This is a Memory Switch function * | Digital 16: Output 10 This is a Memory Switch function * | Digital 17: Output 11 This is a Memory Switch function * | Digital 18: Output 12 This is a Memory Switch function * | Digital 19: Output 13 This is a Memory Switch function * | Digital 20: Output 14 This is a Memory Switch function * -------------------------------------------------------------------------------------------------------------------------------- */ #define PulsReset 5 // #define MomentReset 10 // #define RCmax 2200 // #define RCmid1 1300 // #define RCmid2 1700 // #define RCmin 800 // #define RCfailSafe 500 // #define TimeOut 260000 // #define Led 13 // #define Output1 6 // #define Output2 7 // #define Output3 8 // #define Output4 9 // #define Output5 10 // #define Output6 11 // #define Output7 12 // #define Output8 14 // #define Output9 15 // #define Output10 16 // #define Output11 17 // #define Output12 18 // #define Output13 19 // #define Output14 5 // #define RCport 2 // connect the desired channel (PPM signal) from your RC receiver to digital pin 2 on Arduino. #define Jumper1pin 3 // #define Jumper2pin 4 // int RCpulse = 0; int RCmid; int PulsCount = 0; int PulsResetCount = 0; int MomentResetCount = 0; int Jumper1; int Jumper2; int SetOutput1 = 0; int SetOutput2 = 0; int SetOutput3 = 0; int SetOutput4 = 0; int SetOutput5 = 0; int SetOutput6 = 0; int SetOutput7 = 0; int SetOutput8 = 0; int SetOutput9 = 0; int SetOutput10 = 0; int SetOutput11 = 0; int SetOutput12 = 0; int SetOutput13 = 0; int SetOutput14 = 0; //----------------------------------------------------------------------------------------------------------------------------------- void setup() // This function runs once { //All Outputs: pinMode(Output1, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output1, LOW); pinMode(Output2, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output2, LOW); pinMode(Output3, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output3, LOW); pinMode(Output4, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output4, LOW); pinMode(Output5, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output5, LOW); pinMode(Output6, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output6, LOW); pinMode(Output7, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output7, LOW); pinMode(Output8, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output8, LOW); pinMode(Output9, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output9, LOW); pinMode(Output10, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output10, LOW); pinMode(Output11, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output11, LOW); pinMode(Output12, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output12, LOW); pinMode(Output13, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output13, LOW); pinMode(Output14, OUTPUT); // declare the port as an OUTPUT digitalWrite(Output14, LOW); pinMode(Led, OUTPUT); // declare the port as an OUTPUT digitalWrite(Led, HIGH); // turn on the Led // Time = millis(); //PPM input from RC receiver pinMode(RCport, INPUT_PULLUP); //Pin 2 as input pinMode(Jumper1pin, INPUT_PULLUP); //Pin 3 as input pinMode(Jumper2pin, INPUT_PULLUP); //Pin 4 as input Jumper1 = digitalRead(Jumper1pin); // Check if there is a Jumper placed // Jumper2 = digitalRead(Jumper2pin); // Check if there is a Jumper placed // Serial.begin(9600); } //----------------------------------------------------------------------------------------------------------------------------------- void RCread(){ RCpulse = pulseIn(RCport, HIGH, TimeOut); // Reading the pulse from the receiver // Serial.print("RCpulse = "); // Serial.println(RCpulse); } void FailSafe(){ digitalWrite(Output1, LOW); digitalWrite(Output2, LOW); PulsResetCount = 0; MomentResetCount = 0; PulsCount = 0; while(RCpulse < RCmin || RCpulse > RCmax){ RCread(); digitalWrite(Led, LOW); delay(200); digitalWrite(Led, HIGH); if(RCpulse > RCmin && RCpulse < RCmax){break;} } } void MultiSwitch(){ // In each PulsCount Switch Case, there is a Memory Switch Activation or De-Activation // If the Ouput is On then the Output go's Off again... // ... If the Output is Off then the Output go's On again. switch (PulsCount){ case 0: digitalWrite(Output1, LOW); digitalWrite(Output2, LOW); MomentResetCount = 0; break; case 2: // Not needed for Moment Switch function 1 break; case 3: // Not needed for Moment Switch function 2 break; case 4: if(SetOutput3 == 0){ digitalWrite(Output3, HIGH); SetOutput3 = 1;} else if(SetOutput3 == 1){ digitalWrite(Output3, LOW); SetOutput3 = 0;} break; case 5: if(SetOutput4 == 0){ digitalWrite(Output4, HIGH); SetOutput4 = 1;} else if(SetOutput4 == 1){ digitalWrite(Output4, LOW); SetOutput4 = 0;} break; case 6: if(SetOutput5 == 0){ digitalWrite(Output5, HIGH); SetOutput5 = 1;} else if(SetOutput5 == 1){ digitalWrite(Output5, LOW); SetOutput5 = 0;} break; case 7: if(SetOutput6 == 0){ digitalWrite(Output6, HIGH); SetOutput6 = 1;} else if(SetOutput6 == 1){ digitalWrite(Output6, LOW); SetOutput6 = 0;} break; case 8: if(SetOutput7 == 0){ digitalWrite(Output7, HIGH); SetOutput7 = 1;} else if(SetOutput7 == 1){ digitalWrite(Output7, LOW); SetOutput7 = 0;} break; case 9: if(SetOutput8 == 0){ digitalWrite(Output8, HIGH); SetOutput8 = 1;} else if(SetOutput8 == 1){ digitalWrite(Output8, LOW); SetOutput8 = 0;} break; case 10: if(SetOutput9 == 0){ digitalWrite(Output9, HIGH); SetOutput9 = 1;} else if(SetOutput9 == 1){ digitalWrite(Output9, LOW); SetOutput9 = 0;} break; case 11: if(SetOutput10 == 0){ digitalWrite(Output10, HIGH); SetOutput10 = 1;} else if(SetOutput10 == 1){ digitalWrite(Output10, LOW); SetOutput10 = 0;} break; case 12: if(SetOutput11 == 0){ digitalWrite(Output11, HIGH); SetOutput11 = 1;} else if(SetOutput11 == 1){ digitalWrite(Output11, LOW); SetOutput11 = 0;} break; case 13: if(SetOutput12 == 0){ digitalWrite(Output12, HIGH); SetOutput12 = 1;} else if(SetOutput12 == 1){ digitalWrite(Output12, LOW); SetOutput12 = 0;} break; case 14: if(SetOutput13 == 0){ digitalWrite(Output13, HIGH); SetOutput13 = 1;} else if(SetOutput13 == 1){ digitalWrite(Output13, LOW); SetOutput13 = 0;} break; case 15: if(SetOutput14 == 0){ digitalWrite(Output14, HIGH); SetOutput14 = 1;} else if(SetOutput14 == 1){ digitalWrite(Output14, LOW); SetOutput14 = 0;} break; } } void loop() { RCread(); if(RCpulse < RCmin || RCpulse > RCmax){FailSafe();} // If there is no correct pulse, start the failsafe action and switch off output one and two else if(RCpulse > RCmin && RCpulse < RCmax){ // If the pulse is correct, do this: if (Jumper1 == LOW){ RCmid = RCmid1; while(RCpulse > RCmid){ // As long RCpulse is Lower then RCmid, Do this: RCread(); // Reading the RC input PulsResetCount ++; // Start counting the Reset Pulses if(PulsResetCount > PulsReset){ // If above the PulsReset valeu... MultiSwitch(); // ... Then Go to the Memory Switch State and activate/de-activate a output which is depend to the PulsCount valeu. if(PulsReset > 3){PulsCount = 0;} // If PulsReset is above 3 (all above 3 are Memory Switch channels) then reset PulsCount to 0. if(RCpulse < RCmin || RCpulse > RCmax){FailSafe();} // If there is no correct pulse, start the failsafe action and switch off output one and two if(RCpulse < RCmid){break;} // If RCpulse is Higher then RCmid, Stop the While loop and go to the next step... } } if(RCpulse < RCmid){ // Double Check if the RCpulse is Higher then RCmid, if yes... PulsCount ++; // .... Then start the PulsCounting... PulsResetCount = 0;} // .... And Reset the PulsResetCount back to 0 while(RCpulse < RCmid){ // As long RCpulse is Higher then RCmid, Do this: MomentResetCount ++; // Reset the MomentResetCount back to 0 RCread(); // Reading the RC input if(RCpulse > RCmid){break;} // If RCpulse is Lower then RCmid, stop this While loop // This is a special part to activate Moment Channel 1: if(MomentResetCount > MomentReset && PulsCount == 2){ // If the RCpulse is longer as MomentReset in a High valeu AND PulsCount is 2, then... digitalWrite(Output1, HIGH); // Activate Output 1 while(RCpulse < RCmid){ // As long the RCpulse is High, do Nothing... RCread(); // Just reading the RC input if(RCpulse > RCmid){ // If RCpulse is Lower then RCmid, then... PulsCount = 0; // Reset the PulsCount to 0 break;} // Stop the While loop } digitalWrite(Output1, LOW); // De-activate Output 1 MomentResetCount = 0; // Reset the MomentResetCount back to 0 } // This is a special part to activate Moment Channel 2: else if(MomentResetCount > MomentReset && PulsCount == 3){ // If the RCpulse is longer as MomentReset in a High valeu AND PulsCount is 3, then... digitalWrite(Output2, HIGH); // Activate Output 1 while(RCpulse < RCmid){ // As long the RCpulse is High, do Nothing... RCread(); // Just reading the RC input if(RCpulse > RCmid){ // If RCpulse is Lower then RCmid, then... PulsCount = 0; // Reset the PulsCount to 0 break;} // Stop the While loop } digitalWrite(Output2, LOW); // De-activate Output 2 MomentResetCount = 0; // Reset the MomentResetCount back to 0 } } // Start over again...... } else if(Jumper1 == HIGH){ RCmid = RCmid2; while(RCpulse < RCmid){ // As long RCpulse is Lower then RCmid, Do this: RCread(); // Reading the RC input PulsResetCount ++; // Start counting the Reset Pulses if(PulsResetCount > PulsReset){ // If above the PulsReset valeu... MultiSwitch(); // ... Then Go to the Memory Switch State and activate/de-activate a output which is depend to the PulsCount valeu. if(PulsReset > 3){PulsCount = 0;} // If PulsReset is above 3 (all above 3 are Memory Switch channels) then reset PulsCount to 0. if(RCpulse < RCmin || RCpulse > RCmax){FailSafe();} // If there is no correct pulse, start the failsafe action and switch off output one and two if(RCpulse > RCmid){break;} // If RCpulse is Higher then RCmid, Stop the While loop and go to the next step... } } if(RCpulse > RCmid){ // Double Check if the RCpulse is Higher then RCmid, if yes... PulsCount ++; // .... Then start the PulsCounting... PulsResetCount = 0;} // .... And Reset the PulsResetCount back to 0 while(RCpulse > RCmid){ // As long RCpulse is Higher then RCmid, Do this: MomentResetCount ++; // Reset the MomentResetCount back to 0 RCread(); // Reading the RC input if(RCpulse < RCmid){break;} // If RCpulse is Lower then RCmid, stop this While loop // This is a special part to activate Moment Channel 1: if(MomentResetCount > MomentReset && PulsCount == 2){ // If the RCpulse is longer as MomentReset in a High valeu AND PulsCount is 2, then... digitalWrite(Output1, HIGH); // Activate Output 1 while(RCpulse > RCmid){ // As long the RCpulse is High, do Nothing... RCread(); // Just reading the RC input if(RCpulse < RCmid){ // If RCpulse is Lower then RCmid, then... PulsCount = 0; // Reset the PulsCount to 0 break;} // Stop the While loop } digitalWrite(Output1, LOW); // De-activate Output 1 MomentResetCount = 0; // Reset the MomentResetCount back to 0 } // This is a special part to activate Moment Channel 2: else if(MomentResetCount > MomentReset && PulsCount == 3){ // If the RCpulse is longer as MomentReset in a High valeu AND PulsCount is 3, then... digitalWrite(Output2, HIGH); // Activate Output 1 while(RCpulse > RCmid){ // As long the RCpulse is High, do Nothing... RCread(); // Just reading the RC input if(RCpulse < RCmid){ // If RCpulse is Lower then RCmid, then... PulsCount = 0; // Reset the PulsCount to 0 break;} // Stop the While loop } digitalWrite(Output2, LOW); // De-activate Output 2 MomentResetCount = 0; // Reset the MomentResetCount back to 0 } } } // Start over again...... } }