Hey all,
I am attempting to automate my blinds by attaching a DC motor to where the wand normally attaches. I created an H bridge using N & P type MOSFETs to drive the motor in both directions. Ideally, I want to press the push button and the blinds will open; then when i press it again they will close.
Currently, when I upload the sketch to my Arduino Uno and press the button initially, the motor will turn for the time i set it to turn, then immediately reverse direction and turn for the same time, then forward again... and repeats non-stop. Pressing or holding the button once the button has been pressed initially does not stop or affect the cycle.
Here is my code:
const int FETSF = 2; // the pin for the forward direction mosfets
const int FETSR = 4; // the pin for the reverse direction mosfets
const int BUTTON = 7; // the input pin where the pushbutton is connected
int val = 0; // val will be used to store the state
// of the input pin
int but_state = 0; // button state
int dir_state = 1; // last direction state: 0 = last went forward, 1 = last went reverse
void setup() {
pinMode(FETSF, OUTPUT); // tell Arduino mosfet is an output
pinMode(FETSR, OUTPUT); // " " "
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); // read input value and store it
// check if the input is HIGH (button pressed)
// and change the state
if (val == HIGH) {
but_state = 1 - but_state;
}
if (but_state == 1) {
if (dir_state == 1){
digitalWrite(FETSF, HIGH); // turn motor forward
delay (4000); // for 4 seconds
digitalWrite(FETSF, LOW); // turn motor off
dir_state = 0; //set last direction state to forward
}
else {
digitalWrite(FETSR, HIGH); // turn motor reverse
delay (4000); // for 4 seconds
digitalWrite(FETSR, LOW); // turn motor off
dir_state = 1; //set last direction state to reverse
}
}
else {
digitalWrite(FETSF, LOW);
digitalWrite(FETSR, LOW);
}
}
Could you explain to me what I have done wrong (programming-wise) to have it act like I described? Something with the states?
Thanks,
EZE123