There is a code in which the value of “H” is transmitted to the PC’s COM port when the button is pressed, and again when it is pressed “L”. Interested in how to make another event, namely: when you press the button, the value "H" is transmitted to the port, when you press it once - "P", and when you hold the key for one second, the value "L" must be transmitted to the COM port. I post the existing code, tell me, please. If something is not clear, I will explain:

int switchPin = 12; int ledPin = 13; boolean lastBut = LOW; boolean curBut = LOW; boolean ledOn = false; int val=0; void setup() { pinMode (switchPin, INPUT); pinMode (ledPin, OUTPUT); Serial.begin(9600); } boolean debounce(boolean last) { boolean cur=digitalRead(switchPin); if (last!=cur) { delay(5); cur = digitalRead(switchPin); } return cur; } void loop() { curBut=debounce (lastBut); if (lastBut == LOW && curBut == HIGH) { ledOn =!ledOn; } lastBut = curBut; digitalWrite(ledPin,ledOn); if (ledOn == HIGH) { Serial.println("H"); } else { Serial.println("L"); } delay(100); } 

    2 answers 2

    A piece of code generator startup program. Two buttons. When you click on one and hold the delayBefore (3000 ms), the trigger is triggered.

     void setup() { ...... previousTime = millis(); .... } void loop() { Serial.println(getEngineTemp()); byte buttonState = digitalRead(pinButtonStart)+2*digitalRead(pinButtonStop); switch (buttonState) { case 1:{ if (!getEngineStatus()){ if (millis()-previousTime > delayBefore) { StartEngine(); } } break; } case 2: if (getEngineStatus()) { StopEngine();} default: {previousTime = millis();} } 

      Use the tick system.

      In the main loop, count the number of certain periods of time (for example, 10 milliseconds), and after these 10 milliseconds, add some variable. Then you can find out how long the button has been pressed.