/*
Original question :
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
couple of MOSFETs that supplies the power to the solenoid poles
*/
#include "onePulsePin.h" // The non-blocking one shot pulse out class.
#include <mechButton.h> // Button debouncing library.
#define PULSE_PIN_R 2 // The right pulse will be run on pin #2
#define PULSE_PIN_L 3 // The left pulse will be run on pin #3
#define SWITCH_PIN_R 4 // The button for the thing will be on pin #3
#define SWITCH_PIN_L 5 // The button for the thing will be on pin #3
#define PULSE_MS 100
onePulsePin pulseR(PULSE_PIN_R); // Create the right pulse object. (The pin that pulses)
onePulsePin pulseL(PULSE_PIN_L); // Create the left pulse object. (The pin that pulses)
mechButton switchR(SWITCH_PIN_R); // Create the right switch object.
mechButton switchL(SWITCH_PIN_L); // Create the left switch object.
void setup() {
switchR.setCallback(doSlideR); // Tell switch to to call this function when changed.
switchL.setCallback(doSlideL); // Tell switch to to call this function when changed.
digitalWrite(PULSE_PIN_R,LOW);
digitalWrite(PULSE_PIN_L,LOW);
}
// When there's a change on the right switch pin, this function will be called.
void doSlideR(void) {
if (!switchR.trueFalse()) { // If the switch pin has been grounded..
pulseR.pulse(PULSE_MS,HIGH); // Do a HIGH pulse to the thing for this amount of ms.
}
}
// When there's a change on the left switch pin, this function will be called.
void doSlideL(void) {
if (!switchL.trueFalse()) { // If the switch pin has been grounded..
pulseL.pulse(PULSE_MS,HIGH); // Do a HIGH pulse to the thing for this amount of ms.
}
}
void loop() {
idle(); // idle() lets all the magic happen behind the scenes.
}