GoForSmoke:
It could use more cleaning up, it needs code to stop the motor and led where the comments says to.
It's not tested or even compiled but this should do the job.Kudos to you for not having blocking code in the first place. It made this easy.
#include <Stepper.h>
const int stepsPerRevolution = 200; //number of steps per revolution
// initialize the stepper library on pins 2 through 5:
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);
int stepCount = 0; // number of steps the motor has taken
byte processState; // 0 is OFF, 1 is ON
byte pin7prevState;
byte pin8prevState;
byte readState;
void setup()
{
setMode( 7, INPUT_PULLUP ); // wire this pin to a switch and the switch to ground
setMode( 8, INPUT_PULLUP ); // and put a 1 uF cap across both switches to debounce them
}
void loop()
{
readState = digitalRead( 7 );
if ( pin7prevState != readState )
{
pin7prevState = readState );
if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 1;
}
}
readState = digitalRead( 8 ); // note that the stop button is read last. press both makes stop
if ( pin8prevState != readState )
{
pin8prevState = readState );
if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 0;
}
}
if ( processState )
{
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0)
{
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}
else
{
// turn the motor and led off
}
}
Hi there, kind thanks for the reply and time.
I got a hold of some 1uF capacitors, and have put them across the switches (not entirely sure which way round these were supposed to go)
and I tried to compile the code you suggested, as shown below:
#include <Stepper.h>
const int stepsPerRevolution = 200; //number of steps per revolution
// initialize the stepper library on pins 2 through 5:
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);
int stepCount = 0; // number of steps the motor has taken
int ledPin = 13; // set LED pin
byte processState; // 0 is OFF, 1 is ON
byte pin7prevState;
byte pin8prevState;
byte readState;
void setup()
{
setMode( 7, INPUT_PULLUP ); // wire this pin to a switch and the switch to ground
setMode( 8, INPUT_PULLUP ); // and put a 1 uF cap across both switches to debounce them
}
void loop()
{
readState = digitalRead( 7 );
if ( pin7prevState != readState )
{
pin7prevState = readState );
if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 1;
}
}
readState = digitalRead( 8 ); // note that the stop button is read last. press both makes stop
if ( pin8prevState != readState )
{
pin8prevState = readState );
if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 0;
}
}
if ( processState )
{
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0)
{
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
digitalWrite(ledPin, HIGH);
}
else
{
// turn the motor and led off
int motorSpeed = 0;
digitalWrite (ledPin, LOW);
}
}
but unfortunately came to an error... exit status 1
'setMode' was not declared in this scope
Hope someone can help!
Thanks