//constant value which will not change:
const int PTT = 6;
const int REL1 = 2;
const int REL2 = 3;
const int REL3 = 4;
const int REL4 = 5;
//values which will change:
int PTTState = 0;
int PTTPushCounter = 0;
int lastPTTState = 0;
unsigned long timeLastPush = 0; //Millisek. since last time PTT was pressed..
unsigned long timeOutPTT = 8000; // 2,0sek
void setup() {
pinMode(PTT,INPUT);
pinMode(REL1,OUTPUT);
pinMode(REL2,OUTPUT);
pinMode(REL3,OUTPUT);
pinMode(REL4,OUTPUT);
Serial.begin(9600);
Serial.println("PTT FUNCTION");
}
void loop() {
// read the pushbutton input pin:
PTTState = digitalRead(PTT);
// compare the PTTState to its previous state
if (PTTState != lastPTTState && lastPTTState == 0) { //If changed PTT state AND lastPTTState = 0,
if ((millis() - timeLastPush) > timeOutPTT ) { //If timeout-limit for PTT reached:
timeLastPush = millis(); //Set new timecouter start now,
PTTPushCounter = 0; //Set PTT counter to 0.
}
// if the state has changed, increment the counter
if (PTTState == HIGH) {
// if the current state is HIGH then the PTT went from off to on:
PTTPushCounter++;
Serial.println("on");
Serial.print("number of PTT pushes: ");
Serial.println(PTTPushCounter);
} else {
// if the current state is LOW then the PTT went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastPTTState = PTTState;
if (PTTPushCounter % 4 == 3) {
digitalWrite(REL1, HIGH);
}
if (PTTPushCounter % 6 == 5) {
digitalWrite(REL2,HIGH);
}
if (PTTPushCounter % 8 == 7) {
digitalWrite(REL3,HIGH);
}
if (PTTPushCounter % 10 == 9) {
digitalWrite(REL4,HIGH);
}
else if (PTTPushCounter == 10) {
digitalWrite(REL1,LOW);
digitalWrite(REL2,LOW);
digitalWrite(REL3,LOW);
digitalWrite(REL4,LOW);
}
}
This is now the updated test code.
Function: First: I have two radios UV-5r, one is used to transmit, the other is used to receive. I am not talking with the radios, just selected a free VHF channel and do PTT pushing thats all.
So the code works like this:
When I push ptt on the transmitting radio 3 times; 1st relay turns on .
When I push ptt on the transmitting radio 5 times; 2nd relay turns on.
When I push ptt on the transmitting radio 7 times; 3rd relay turns on.
When I push ptt on the transmitting radio 9 times; 4th relay turns on.
When I push ptt on the transmitting radio 10 times; All relays off.
My recieving radio has its speaker jack connected to the arduino with a 10k (pullup) resistor on the read pin (top pin). The speaker jack's ground pin is connected to ground on the arduino. And from the arduino a relay 4-channel relay board is also connected which is what i toggle.
So, Note that only the top ring and ground ring on the jack is connected to arduino with that 10k resistor. The middle ring is not connected.
Can someone help me with some better millis() delay code. So that If I toggle the first relay with my ptt transmitting radio, wait 5 mins or something and then it turns of the relay after that millis() delay ?
Thanks guys