Jump to content

Recommended Posts

Posted (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 by lesthegrngo
Posted

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?

Posted

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;
}

 

  • Like 1
Posted

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.

  • Thanks 1
Posted

@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

  • Like 1

Regards, Vinc

real life: Royal Bavarian Airforce

online: VJS-GermanKnights.de

[sIGPIC][/sIGPIC]

Posted
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.  🤣

  • Thanks 1
Posted
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 

  • Like 3
Posted (edited)

Hi Les,

That's why I mostly skip Google's answers, containing posts of the Arduino forum 😉

Regards, Vinc 

Edited by Vinc_Vega
  • Like 2

Regards, Vinc

real life: Royal Bavarian Airforce

online: VJS-GermanKnights.de

[sIGPIC][/sIGPIC]

Posted
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! 🤣 🤣

  • Like 2
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...