const int Play = 2;
const int PTT = 3;
const int buttonPinRec = 4;
const int buttonPinPlay = 5;
const int Rec = 6;
const int buttonPinPlayLoop = 7;
int buttonStateRec = 0;
int buttonStatePlay = 0;
int buttonStateLoop = 0;
int lastButtonState = 0; // previous state of the button
int startPressed = 0; // the time button was pressed
int endPressed = 0; // the time button was released
int timeHold = 0; // the time button is hold
int timeReleased = 0; // the time button is released
void setup() {
// initialize the button pin as a input:
pinMode(buttonPinRec, INPUT);
pinMode(buttonPinPlay, INPUT);
pinMode(Play, OUTPUT);
pinMode(Rec, OUTPUT);
pinMode(PTT, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonStateRec = digitalRead(buttonPinRec);
if (buttonStateRec ==HIGH){
digitalWrite(Rec, HIGH);
} else {
digitalWrite(Rec, LOW);
}
buttonStatePlay = digitalRead(buttonPinPlay);
if (buttonStatePlay ==HIGH){
digitalWrite(PTT, HIGH);
delay(1000);
digitalWrite(Play, HIGH);
delay (50);
digitalWrite(Play, LOW);
delay(timeHold+500);
digitalWrite(PTT, LOW);
}
else {
digitalWrite(Play, LOW);
digitalWrite(PTT, LOW);
}
buttonStateLoop = digitalRead(buttonPinPlayLoop);
if (buttonStateLoop ==HIGH){
digitalWrite(PTT, HIGH);
delay(1000);
digitalWrite(Play, HIGH);
delay (50);
digitalWrite(Play, LOW);
delay(timeHold+500);
digitalWrite(PTT, LOW);
delay(5000);
}
else {
digitalWrite(Play, LOW);
digitalWrite(PTT, LOW);
}
// button state changed
if (buttonStateRec != lastButtonState) {
// the button was just pressed
if (buttonStateRec == HIGH) {
startPressed = millis();
timeReleased = startPressed - endPressed;
if (timeReleased >= 500 && timeReleased < 1000) {
Serial.println("Button idle for half a second");
}
if (timeReleased >= 1000) {
Serial.println("Button idle for one second or more");
}
// the button was just released
} else {
endPressed = millis();
timeHold = endPressed - startPressed;
if (timeHold >= 500 && timeHold < 1000) {
Serial.println("Button hold for half a second");
Serial.println(timeHold / 1000);
}
if (timeHold >= 1000) {
Serial.println("Button hold for one second or more");
Serial.println(timeHold/1000);
}
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonStateRec;
}
PENAFIAN : EAWAN, 9M2SES sama sekali tidak akan bertanggungjawab diatas sebarang ketidaktepatan maklumat yang didapati dilaman blog ini dan segala kemungkinan yang timbul disebabkan ketidaktepatan maklumat tersebut.
Segala pengambilan maklumat dan ujikaji yang dilakukan melalui laman blog ini adalah diatas risiko sendiri