lesthegrngo Posted December 26, 2024 Posted December 26, 2024 (edited) All, I am trying to set up a solenoid so that it moves when a toggle switch is moved from one position to another, but the output from the Arduino needs to be a pulse about 20ms long so that the solenoid doesn't remain powered. I am using the Arduino to drive a MOSFET The sketch below seems to keep repeating the pulse, whereas I want just one pulse, then only when the toggle is moved to the other position should it then send another pulse to the other pole of the solenoid. I looked at the change of state example in Arduino IDE but that seems to more tailored to counting button presses How can I stop the Arduino sending repeated pulses? #define control1 2 // pin that controls the MOSFET #define control2 3 // pin that controls the MOSFET #define pulsePin1 2 // pin that controls the MOSFET #define pulsePin2 3 // pin that controls the MOSFET bool buttonState1, buttonState2, buttonState3, buttonState4, buttonState5; const int BUTTON_PIN1 = 4; // the number of the pushbutton pin const int BUTTON_PIN2 = 5; // the number of the pushbutton pin void setup() { Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds pinMode(BUTTON_PIN1, INPUT_PULLUP); pinMode(BUTTON_PIN2, INPUT_PULLUP); pinMode(control1, OUTPUT); // define control pin as output pinMode(control2, OUTPUT); // define control pin as output } void loop() { buttonState1 = digitalRead(BUTTON_PIN1); buttonState2 = digitalRead(BUTTON_PIN2); if (buttonState1 == LOW) { digitalWrite(pulsePin1, 1); // Set output to 1 delayMicroseconds(20); // set this yourself digitalWrite(pulsePin1, 0); // Set output to 0 } if (buttonState2 == LOW) { digitalWrite(pulsePin2, 1); // Set output to 1 delayMicroseconds(20); // set this yourself digitalWrite(pulsePin2, 0); // Set output to 0 } } Cheers Les Edited December 26, 2024 by lesthegrngo
Biggus Posted December 26, 2024 Posted December 26, 2024 Could you use interrupts to trigger on the rising edge of the switch going high? Or save the button state so that as it loops, it doesn't trigger the solenoid repeatedly?
lesthegrngo Posted December 27, 2024 Author Posted December 27, 2024 The 'state change detection' seems to have something like that. I have been looking at the standard IDE example, but it seems to be more about counting button presses
lesthegrngo Posted December 27, 2024 Author Posted December 27, 2024 Someone on the Arduino forum helped out with this, works perfectly #define control1 2 #define control2 3 #define pulsePin1 2 #define pulsePin2 3 bool buttonState1, buttonState2; bool lastButtonState1, lastButtonState2; int pulseWidth = 20; // pulse width in milliseconds - suggested value 20ms int debounce = 20; // debounce delay to prevent multiple triggering const int BUTTON_PIN1 = 4; // the number of the pushbutton pin const int BUTTON_PIN2 = 5; // the number of the pushbutton pin void setup() { Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds pinMode(BUTTON_PIN1, INPUT_PULLUP); pinMode(BUTTON_PIN2, INPUT_PULLUP); pinMode(control1, OUTPUT); // define control pin as output pinMode(control2, OUTPUT); // define control pin as output } void loop() { buttonState1 = digitalRead(BUTTON_PIN1); buttonState2 = digitalRead(BUTTON_PIN2); if (buttonState1 != lastButtonState1) { if (buttonState1 == LOW) { digitalWrite(pulsePin1, 1); // Set output to 1 delay(pulseWidth); // pulse delay digitalWrite(pulsePin1, 0); // Set output to 0 } delay(debounce); // debounce delay } // save the current state as the last state, // for next time through the loop lastButtonState1 = buttonState1 ; if (buttonState2 != lastButtonState2) { if (buttonState2 == LOW) { digitalWrite(pulsePin2, 1); // Set output to 1 delay(pulseWidth); // pulse delay digitalWrite(pulsePin2, 0); // Set output to 0 } delay(debounce); // debounce delay } // save the current state as the last state, // for next time through the loop lastButtonState2 = buttonState2; } 1
No1sonuk Posted December 27, 2024 Posted December 27, 2024 WOW. Someone on an Arduino forum was actually helpful? It's a Christmas miracle! It might not have helped that you used delayMicroseconds(20); instead of delay(20); But yeah, you need to store the current state of the button so it doesn't re-trigger the next time round the loop. Not that code is "blocking" though - while the delays are running, nothing else will happen. You're only using the control1 and control2 labels in the pinmode commands. Why not use pulsePin1 and pulsePin2 like elsewhere instead? Aaand you're using "#define" for one set of constants and "const int" for another. 1
Vinc_Vega Posted December 27, 2024 Posted December 27, 2024 @lesthegrngo What @No1sonuk wanted to point out is, that a normal "delay()" command is in Milliseconds, therefore it is not the same time span if you'd use "delayMicroseconds()" (see your first sketch). Regards, Vinc 1 Regards, Vinc real life: Royal Bavarian Airforce online: VJS-GermanKnights.de [sIGPIC][/sIGPIC]
Biggus Posted December 28, 2024 Posted December 28, 2024 8 hours ago, No1sonuk said: WOW. Someone on an Arduino forum was actually helpful? It's a Christmas miracle! Okay, that's genuinely the funniest comment I've ever seen on these forums. 1
lesthegrngo Posted December 28, 2024 Author Posted December 28, 2024 12 hours ago, No1sonuk said: WOW. Someone on an Arduino forum was actually helpful? It's a Christmas miracle! It might not have helped that you used delayMicroseconds(20); instead of delay(20); But yeah, you need to store the current state of the button so it doesn't re-trigger the next time round the loop. Not that code is "blocking" though - while the delays are running, nothing else will happen. You're only using the control1 and control2 labels in the pinmode commands. Why not use pulsePin1 and pulsePin2 like elsewhere instead? Aaand you're using "#define" for one set of constants and "const int" for another. The original sketch in the initial post was using code culled from another sketch while researching how to do the pulse signal, so the delayMicroseconds(20); came from that. For the #define vs const int thing, again I suspect that was a legacy of Frankensteining bits of sketches together. I tidied it up and the revised sketch below is functionally working exactly the same #define pulsePin1 2 // pin that sends trigger pulse #define pulsePin2 3 // pin that sends trigger pulse #define BUTTON_PIN1 4 // pin that inputs command signal #define BUTTON_PIN2 5 // pin that inputs command signal bool buttonState1, buttonState2; bool lastButtonState1, lastButtonState2; int pulseWidth = 20; // pulse width in milliseconds - suggested value 20ms int debounce = 20; // debounce delay to prevent multiple triggering void setup() { Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds pinMode(BUTTON_PIN1, INPUT_PULLUP); pinMode(BUTTON_PIN2, INPUT_PULLUP); pinMode(pulsePin1, OUTPUT); // define control pin as output pinMode(pulsePin2, OUTPUT); // define control pin as output } void loop() { buttonState1 = digitalRead(BUTTON_PIN1); buttonState2 = digitalRead(BUTTON_PIN2); if (buttonState1 != lastButtonState1) { if (buttonState1 == LOW) { digitalWrite(pulsePin1, 1); // Set output to 1 delay(pulseWidth); // pulse delay digitalWrite(pulsePin1, 0); // Set output to 0 } delay(debounce); // debounce delay } // save the current state as the last state, // for next time through the loop lastButtonState1 = buttonState1; if (buttonState2 != lastButtonState2) { if (buttonState2 == LOW) { digitalWrite(pulsePin2, 1); // Set output to 1 delay(pulseWidth); // pulse delay digitalWrite(pulsePin2, 0); // Set output to 0 } delay(debounce); // debounce delay } // save the current state as the last state, // for next time through the loop lastButtonState2 = buttonState2; } As for the Arduino forum helping out, it wasn't before they tried to change my whole project, question my reason for existence and my whole philosophy about electronics 3
Vinc_Vega Posted December 28, 2024 Posted December 28, 2024 (edited) Hi Les, That's why I mostly skip Google's answers, containing posts of the Arduino forum Regards, Vinc Edited December 28, 2024 by Vinc_Vega 2 Regards, Vinc real life: Royal Bavarian Airforce online: VJS-GermanKnights.de [sIGPIC][/sIGPIC]
No1sonuk Posted December 28, 2024 Posted December 28, 2024 7 hours ago, lesthegrngo said: As for the Arduino forum helping out, it wasn't before they tried to change my whole project, question my reason for existence and my whole philosophy about electronics LOL! 2
Recommended Posts