1

So i'm currently coding a little device that rotates a step motor round one 8th of a full revolution every time a button is pushed. the motor is attached to a question reel with 8 questions on it, each one is viewable through a little window covering the reel after each button press. the thing im concerned about is that if even one of the question sections is slightly larger or smaller than the others, it will cause the question to become misaligned with the window after a certain number of run throughs.

in order to combat this I want to have a back up code that simply rotates the stepper while the button is held down. this will allow me to manually reset the reel to its proper positioning should the questions become misaligned. im not achieving this however, and was hoping someone may be able to detect where the issues are in my code?

#include <Stepper.h>

#define STEPS 2048

Stepper myStepper(STEPS, 8, 10, 9, 11);

const int  buttonPinBlue = 6;

void setup() {

  myStepper.setSpeed(5);
}

void stepp() {

 myStepper.step(1);

  pinMode(buttonPinBlue, INPUT);
}

void loop() {

 if (buttonPinBlue == HIGH) {

  Serial.print("pin is high");

   stepp();
  }

}

also just as a side note, the stepper runs fine using the 'define steps' value and the speed that I've set here, the issue appears to be that the button press is not being detected

0

You don't set the pin itself:

Change

if (buttonPinBlue == HIGH) {

to

if (digitalRead(buttonPinBlue) == HIGH) {

Also you don't have to set the pin every time, only one time is enough: in function setup should be the following line (and it can be removed from the function stepp:

pinMode(buttonPinBlue, INPUT);

Note that if you want to use a 'click' to move to the next item, you have to debounce the button; on the Arduino site check for button debouncing: https://www.arduino.cc/en/Tutorial/Debounce

| improve this answer | |
  • 1
    Ahhh that's sorted it, should have known that really. Thanks for your help! – Rossage1 Dec 5 '19 at 15:49
  • I also aligned your code slightly (next time select it and press ctrl-k), to make it better, remove the empty lines and align the code nicer. – Michel Keijzers Dec 5 '19 at 15:49
  • You're welcome (if it is the correct solution, upvote and accept the answer). Welcome to Arduino Stack Exchange. – Michel Keijzers Dec 5 '19 at 15:49
  • 1
    I was thinking of a while loop since the title of the OP says "while" but this works just as well if the OP is only spinning a motor in the loop() method. – KingDuken Dec 5 '19 at 17:55
  • @KingDuken True, probably if he sees the motor is running he has a starting point how to proceed – Michel Keijzers Dec 5 '19 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.