0

Using a 28BYJ-48 Motor with a ULN2000 Driver. The motor works fine since I can rotate it using serial input. Pushbutton is connected properly, Can light up an led if I connect one. I want the motor to rotate 90 degrees everytime I push the button. When I run the code below the LEDs on the driver just blink rapidly, when I press and hold the button they stop blinking, keeping the state they were on already. e.g. if the LED A on the board was on at the moment I press the button, It stays on. If it was off, It stays off. you get the Idea.

code used:

#include <Stepper.h>;


 // pushbutton pin
 const int buttonPin = 2;

 int STEPS = 32;
 Stepper stepper(STEPS,8,9,10,11);


//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
{
  pinMode(buttonPin, INPUT);
}


void loop()
{
 // local variable to hold the pushbutton states
  int buttonState;  

  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable
  buttonState = digitalRead(buttonPin);

  //if the button is pressed increment counter and wait a tiny bit to give us some          //time to release the button
  if (buttonState == LOW) // light the LED
  {
    counter++;
    delay(150);
  }

  if(counter == 1)
    stepper.step(512);  // zero degrees
  //else reset the counter to 0 which resets thr servo to 0 degrees
  else
   counter = 0;
}

EDIT 1: Modifying the code @Matej gave me, I was able to debug the problem that was causing the stepper to not rotate. the code:

#include <Stepper.h>


// pushbutton pin
const int buttonPin = 2;

const int STEPS = 32;
Stepper stepper(STEPS, 8, 9, 10, 11);
int val = 2048;

//create a variable to store a counter and set it to 0
int counter = 0;

unsigned long last_button_time = 0;   //for aving last millis 



void setup(){
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  stepper.setSpeed(1000);
}


void loop(){
  bool buttonState;   // local variable to hold the pushbutton states

  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable
  buttonState = digitalRead(buttonPin);
  //if the button is pressed incremen counter and wait a tiny bit to give us some          //time to release the button
  if(buttonState == HIGH && millis() - last_button_time > 200){ // light the LED  
    last_button_time = millis();
    counter = 1;
    Serial.println(counter);
  }

  if (counter == 1) {
    Serial.println("Please Write the desired angle");
    val = Serial.parseInt();
    delay(5000);
    counter = 0;
    if(Serial.available() > 0) {
    Serial.println("Stepping: ");
    Serial.print(val);
    stepper.step(val);
    counter = 0;
    Serial.println(counter);
    }
    else {
      Serial.println("ERROR LOOPING THROUGH IF STATEMENT");
    }
  }

}

The Serial output looks like this:

1
Please Write the desired angle
Stepping: 
00
1
Please Write the desired angle
Stepping: 
20480
1
Please Write the desired angle
Stepping: 
20480

please note that In the first attempt, I wrote "2048" in Serial input to define the value of the integer (val), and nothing happened. in the second and third attempt, I also wrote 2048 (0 at the end is the counter variable) and the motor started shaking, but it didn't rotate.

  • i don't see any code that moves the servo 90 degrees – jsotola Feb 5 at 0:20
  • I'm moving a stepper motor, not a servo ^__^ – MTXShift Games Feb 5 at 8:16
0

I edtied few things in your code. I am not sure if LOW value is correct, so I replaced it with HIGH. Second thing is timing. I removed all delay() functions instead of it you can see usage of millis(). That's for handling interval between button press and button release - second part of if will check if last time of button press is bigger than 200ms, if yes, move stepper. Last things is stepping. You can see here stepper.step(STEPS/4);. STEPS is variable which you declared on the top of code. It's called steps per revolution, one revolution is 360° so 90° will be STEPS/4.

Here is the code, read it and test it:

#include <Stepper.h>;


// pushbutton pin
const int buttonPin = 2;

const int STEPS = 32;
Stepper stepper(STEPS, 8, 10, 9, 11);


//create a variable to store a counter and set it to 0
int counter = 0;

unsigned long last_button_time = 0;   //for aving last millis 



void setup(){
  pinMode(buttonPin, INPUT);
  stepper.setSpeed(1000)
}


void loop(){
  bool buttonState;   // local variable to hold the pushbutton states

  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable
  buttonState = digitalRead(buttonPin);

  //if the button is pressed increment counter and wait a tiny bit to give us some          //time to release the button
  if(buttonState == HIGH && millis() - last_button_time > 200){ // light the LED  
    last_button_time = millis();
    counter++;
    stepper.step(STEPS/4);    //90° degrees 
  }
}
  • Still doesn't work :( in my old code the LEDs on the Driver where blinking when you weren't pressing the button, in your code they blink when I press the button. – MTXShift Games Feb 5 at 8:24
  • @MTXShiftGames And that's correct when leds on the driver board blinks only during button press, right? – Matej Feb 5 at 8:27
  • I and you both forgot to do stepper.setSpeed(1000); but that didn't help. the LEDs on the driver blink when I push the button but nothing happens. I forgot to mention I have the motor plugged into a breadboard and powered by a breadboard Power supply, in case that makes a difference. EDIT: Please Check Question EDIT. – MTXShift Games Feb 5 at 9:13
  • @MTXShiftGames And does motor work with example? Are steps per revolution 32 correct? – Matej Feb 5 at 9:17
  • yup it works. I have a program that rotates the motor based on serial input and the STEPS/Revolution is defined as 32 and that works. – MTXShift Games Feb 5 at 9:23

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.