Yes
Ok, thanks for your help!
Try this code.
A switch connected between pin 8 and GND closing will make the stepper run for 5 seconds.
//********************************************^************************************************
// Version YY/MM/DD Description
// 1.00 22/10/14 Running sketch
//
//
#include <AccelStepper.h>
//ULN2003 Motor Driver Pins
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define FULLSTEP 4
#define HALFSTEP 8
#define ENABLED true
#define DISABLED false
#define PRESSED LOW
#define RELEASED HIGH
AccelStepper myStepper(HALFSTEP, IN1, IN3, IN2, IN4);
const byte heartbeatLED = 13;
const byte mySwitch = 8;
boolean motorFlag = DISABLED;
byte lastMySwitchState;
//timing stuff
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long motorTime;
const unsigned long motorInterval = 5000ul;
//********************************************^************************************************
void setup()
{
Serial.begin(9600);
for (int i = 2; i <= 4; i++)
{
pinMode(i, OUTPUT);
}
pinMode(heartbeatLED, OUTPUT);
pinMode(mySwitch, INPUT_PULLUP);
//*************************************
//continuous rotation stepper movement
myStepper.setMaxSpeed(500);
myStepper.setSpeed(400); //CW
//myStepper.setSpeed(-400); //CCW
} //END of setup()
//********************************************^************************************************
void loop()
{
//************************************* h e a r t b e a t T I M E R
//to see if the sketch is blocking,
//toggle the heartbeat LED every 500ms
if (millis() - heartbeatTime >= 500)
{
//restart the TIMER
heartbeatTime = millis();
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************* s w i t c h T I M E R
//is it time to check the switches ? (every 50ms)
if (millis() - switchTime >= 50)
{
//restart the TIMER
switchTime = millis();
//go and check the switches
checkSwitches();
}
//************************************* m o t o r T I M E R
//if enabled, has the motor TIMER expired ?
if (motorFlag == ENABLED)
{
if (millis() - motorTime < motorInterval)
{
//must be executed each time through loop() for the motor to turn
myStepper.runSpeed();
}
else
{
//turns off the motor
motorFlag = DISABLED;
}
}
//*************************************
//other non blocking code goes here
//*************************************
} //END of loop()
//********************************************^************************************************
void checkSwitches()
{
//********************************************* m y S w i t c h
//mySwitch code
byte currentState = digitalRead(mySwitch);
//**********************
//was there a change in state ?
if (lastMySwitchState != currentState)
{
//update to the new state
lastMySwitchState = currentState;
//**********************
//when the motor is not turning, is the switch PRESSED ?
if (motorFlag == DISABLED && currentState == PRESSED)
{
//enable the motor TIMER
motorFlag = ENABLED;
//restart the motor TIMER
motorTime = millis();
}
} //END of mySwitch code
} //END of checkSwitches()
I actually already have working code for the stepper, thanks anyway!
So i tried 4 aa batteries, and there is even less power, am I doing something wrong?
We will never know because you are not showing us what you have done.
Sorry, I did exactly like that schematic i sent you.
But you have not shown us a good image of your actual wiring for us to review.
It'll be hard to show, but i can try
Is that a 2 AA battery holder (3v) or 4 AA battery holder (6v) ?
You need 6v (4 AA batteries).
The + side of the batteries goes to the motor driver board 5v terminal.
The Arduino is powered from an external supply.
Hi,
What 4 pins are you using for control?
Are they the same as in the code you are using?
Using one of those buttons to switch power is not good, they have some resistance when ON.
Thanks.. Tom...




Hope you aren’t trying to power the Arduino from the batteries !
Its a 4
Show us a close up picture of your Arduino wiring.
Yeah, believe so, what switch would you recommend?
Ok, i will when i get home
If you have a voltmeter, measure the battery voltage when your sketch is running.
I think maybe thats what im doing, sorry, i dont know much about this🤦