/* ======================================= Sequenceur 3BANDES 4DELAIS 4ETATS ====================================== Delais D8-D11 */ // Define global variables: boolean pinValue; boolean oldPTT; // LOW is TX; HIGH is RX boolean newPTT; // same boolean new1PTT; // temporary value int fourBitValue; int delayValue; // Define global constants: const int factor = 10; //multiply by 15 const int pinD8 = 8; //pin D08, use internal pull-up const int pinD9 = 9; //pin D09, use internal pull-up const int pinD10 = 10; //pin D10, use internal pull-up const int pinD11 = 11; //pin D11, use internal pull-up const int pinPTT = 12; //pin D12 const int event1 = 2; //pin D02 const int event2 = 3; //pin D03 const int event3 = 4; //pin D04 const int event4 = 5; //pin D05 const int debounceDelay = 20; //20 milliseconds void setup() { // Configure 4 pins (used for jumpers) pinMode(pinD8, INPUT_PULLUP); digitalWrite(pinD8, HIGH); //turn on pull-up resistor pinMode(pinD9, INPUT_PULLUP); digitalWrite(pinD9, HIGH); //turn on pull-up resistor pinMode(pinD10, INPUT_PULLUP); digitalWrite(pinD10, HIGH); //turn on pull-up resistor pinMode(pinD11, INPUT_PULLUP); digitalWrite(pinD11, HIGH); //turn on pull-up resistor // Configure PTT pin pinMode(pinPTT, INPUT); // pinPTT is input // Configure built-in LED pinMode(LED_BUILTIN, OUTPUT); // D13 LED monitors PTT // Configure digital pins pinMode(event1, OUTPUT); // Event1 pin pinMode(event2, OUTPUT); // Event2 pin pinMode(event3, OUTPUT); // Event3 pin pinMode(event4, OUTPUT); // Event4 pin // Set RX mode oldPTT = HIGH; // set RX mode sequenceRX(); // set relevant ports to RX mode // Calculate delay calculateDelayValue(); //calculate delayValue // Send delayValue to PC via serial interface Serial.begin(9600); //speed 9600 baud Serial.print("Delay = "); // debug Serial.print(delayValue); // debug Serial.println(" millisekunder"); // debug } void loop() { // decide what to do if PTT status has changed newPTT = digitalRead(pinPTT); // read PTT pin if (newPTT != oldPTT) { // PTT has changed, but not debounced yet delay(debounceDelay); // wait and see if pinPTT is still the same new1PTT = digitalRead(pinPTT); // read PTT pin again if (new1PTT == newPTT) { // debouncing done, new state is stable if (newPTT) { // newPTT HIGH => change to RX mode digitalWrite(LED_BUILTIN, LOW); // built-in LED turned OFF sequenceRX(); // see details in the function below oldPTT = newPTT; // save the new PTT status } else { // newPTT LOW => change to TX mode digitalWrite(LED_BUILTIN, HIGH); // built-in LED turned ON sequenceTX(); // see details in the function below oldPTT = newPTT; // save the new PTT status } } } } ////////////////////////////////////////////////////////////////// void calculateDelayValue() { // read D08-D11 and calculate delayValue fourBitValue = 0; // initialize fourBitValue // read D08-D11 and copy each bit into four_bit_value pinValue = digitalRead(pinD11); // read bit 0 at D11 bitWrite(fourBitValue,0,pinValue); // write the bit into fourBitValue pinValue = digitalRead(pinD10); // read bit 1 at D10 bitWrite(fourBitValue,1,pinValue); // write the bit into fourBitValue pinValue = digitalRead(pinD9); // read bit 2 at D09 bitWrite(fourBitValue,2,pinValue); // write the bit into fourBitValue pinValue = digitalRead(pinD8); // read bit 3 at D08 bitWrite(fourBitValue,3,pinValue); // write the bit into fourBitValue // secure that delay=0 is replaced with delay=10 if (fourBitValue == 0) { // if fourBitValue is 0 (illegal value) fourBitValue = 8; //set fourBitValue = 8 (delay will then be 160 ms) } // calculate delayValue delayValue = fourBitValue * factor; // calculate delayValue } void sequenceTX() { // activate Event1 => Event2 => Event3 => Event4 digitalWrite(event1, LOW); // event1 LOW for TX delay(delayValue); digitalWrite(event2, HIGH); // event2 HIGH for TX delay(delayValue); digitalWrite(event3, HIGH); // event3 HIGH for TX delay(delayValue); digitalWrite(event4, HIGH); // event4 HIGH for TX delay(delayValue); } void sequenceRX() { // deactivate Event4 => Event3 => Event2 => Event1 digitalWrite(event4, LOW); // event4 LOW for RX delay(delayValue); digitalWrite(event3, LOW); // event3 LOW for RX delay(delayValue); digitalWrite(event2, LOW); // event2 LOW for RX delay(delayValue); digitalWrite(event1, HIGH); // event1 HIGH for RX delay(delayValue); }