/* Ventilator The circuit: - Pulse+ of the stepper motor driver attached to pin 3 - Pulse- of the stepper motor driver attached to ground - NCC Pushbutton attached from pin 2 to +5V - 10 kilohm resistor attached from pin 2 to ground This code was written by Wolfgang Smidt and is in public domain (CC0). Visit worldwidewolf.de/vent.html for project details. */ // Defininition of the pins numbers const int buttonPin = 2; const int stepPin = 3; // Variable to hold sleep time in microseconds (for changing motor speed) int sleepTime = 70; // Variable to hold the current button state // Note: I use an normally closed contact (NCC) as push button, that is on HIGH, when it is not! pressed. // You need to modify the code if you use a normally open contact (NOC) button. int buttonState = HIGH; // Variable to hold button state from previuos loop (Modify if you use an NOC) int buttonPreviousState = 1; void setup() { // Initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); // Initialize the step pin as an Output pinMode(stepPin,OUTPUT); digitalWrite(stepPin,LOW); } void loop() { // Send 100 pulses to the motor, so it moves 90 degrees. for(int x = 0; x < 100; x++) { digitalWrite(stepPin,HIGH); delayMicroseconds(sleepTime); digitalWrite(stepPin,LOW); delayMicroseconds(sleepTime); } // Change the speed in case the pushbutton is pressed. buttonState = digitalRead(buttonPin); if ((buttonState == LOW) and (buttonPreviousState == 1)) { // The (opener!) pushbutton is now pressed AND in the previous loop it was NOT yet pressed // so the speed speed of the motor needs to be changed by changing the sleepTime if (sleepTime == 70) { // Faster (Sleeping shorter between the pulses) sleepTime = 60; } else if (sleepTime == 60) { // Faster sleepTime = 50; } else if (sleepTime == 50) { // Fastest sleepTime = 40; } else if (sleepTime == 40) { // Back to Slowest sleepTime = 30; } else if (sleepTime == 30) { // Back to Slowest sleepTime = 70; } // As the pushbutton is now pressed, keep that in mind for the next loop. buttonPreviousState = 0; delayMicroseconds(500000); } else { // The pushbutton is not pressed, keep that in mind for the next loop. buttonPreviousState = 1; } }