2

I am completely new to Arduino, and all sorts of hardware and software. I have a stepper motor, which I want to rotate by a number of rotations when I press a button, and then go backwards by a number of rotations when pressed again. I'm not getting any errors with the code, but it does not seem to do what I want. Before adding the button, the stepper motor worked fine, so I don't think it's malfunctioning. When the button is pressed, the LEDs on the H-bridge flicker, but there's no response at all from the motor. I don't know if it's a problem with the code or the set up.

#include <Stepper.h>
const float STEPS_PER_REVOLUTION = 4096;
const float GEAR_REDUCTION = 4;
const float TOTAL_STEPS = STEPS_PER_REVOLUTION * GEAR_REDUCTION
int StepsRequired = 16384;
int switchPin = 7;
boolean current = LOW;
boolean last = LOW;
boolean isOpen = LOW;
Stepper steppermotor (STEPS_PER_REVOLUTION, 8, 10, 9, 11);

void setup() {
  pinMode(switchPin, INPUT);
}

boolean debounce(boolean inLast) {
  boolean inCurrent = digitalRead(switchPin);
  if(inLast != current) {
    delay(5);
    inCurrent = digitalRead(switchPin);
    }
  return inCurrent;
}

void garageAction(float factor){
  StepsRequired = TOTAL_STEPS*factor;
  steppermotor.setSpeed(1000);
  steppermotor.step(StepsRequired);
  if (isOpen == LOW) {
    delay(3000);
  } else if (isOpen == HIGH) {
    delay(5);
  }
}

void loop() {
  current = debounce(last);

  if (current == HIGH && last == LOW && isOpen == LOW) {
    garageAction(2);
    isOpen = !isOpen;
  }

  if(current == HIGH && last == LOW && isOpen == HIGH) {
    garageAction(-2);
    isOpen = !isOpen;
  }
}

I would really appreciate any help with this - as mentioned, I'm completely new to any sort of hardware and software, and I'm also under time pressure to get this done, so I'd really appreciate any help in getting this solved. I could just be missing something really silly, but I'm completely lost.

Thanks in advance

1
  • all this program will do is constantly alternate isOpen and call garageAction. This will happen so fast that nothing will actually happen in the real world.Now that I've reformatted your code, perhaps you can reconsider the other two clauses in your if statements relating to current and last. That is untile the debounce function sets current to be low - after that nothing will happen until current is reset to HIGH. Commented Jul 16, 2019 at 22:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.