I am new to electronics and even programming. I bought an Arduino Nano and a starter kit. I decided to play around with some code I found on the internet. I was wanting one button to drive the motor clockwise and the other to drive it counter. However, when I use the code the motor moves around randomly. I was wondering if it was my wiring or the program. Any help would be appreciated. I have attached a photo of the wiring and the code file to this. Any help would be appreciated.
#include <Stepper.h> //including stepper motor library
//defining pins section
int stepIN1Pin = 4;
int stepIN2Pin = 5;
int stepIN3Pin = 6;
int stepIN4Pin = 7;
int stepsPerRevolution = 64; // amount of steps per revolution
const int button1Pin = 2; // pushbutton 1 pin for clockwise rotation
const int button2Pin = 3; // pushbutton 2 pin for counter clockwise rotation
Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);
void setup() {
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
myStepper.setSpeed(64);
}
void loop() {
// A == B means "EQUIVALENT". This is true if both sides are the same.
// A && B means "AND". This is true if both sides are true.
// A || B means "OR". This is true if either side is true.
// !A means "NOT". This makes anything after it the opposite (true or false).
//This section layouts the button scheme one button is clockwise rotation, the other counter clockwise
rotation
int button1State, button2State;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (((button1State == LOW) && !(button2State == LOW))) // if we're pushing button 1 OR button 2
myStepper.step(stepsPerRevolution);
if (((button2State == LOW) && !(button1State == LOW))) // if we're pushing button 1 OR button 2
myStepper.step(-stepsPerRevolution);
}
Without PULLUP, your button entry pin is floating and so you will read garbage on it. You don’t need to add resistors. They are built in your arduino and you activate them with the code above
Your motor seems to be the common BYJ48 STEPPER MOTOR which comes with a Breakout using ULN2003 As a Motor driver chip usually and to Power you Motor it is Recommanded to use an external Power Supply with 5V-500mA at least , Don't power it directly from arduino 5V pin.
There are thousands of tutorials on line for this setup, there is a built in library for steppers or the accelstepper library. Read about those too
Just as a courtesy - when you post a picture, please don't use a pizza box as the background.
It makes the detail of the picture harder to see, and also makes us hungry!
lastchancename:
Just as a courtesy - when you post a picture, please don't use a pizza box as the background.
It makes the detail of the picture harder to see, and also makes us hungry!