hi there i have made this code up,i would like to add another function, when a switch pin goes high the stepper will return to its 0 position and when the pin goes low the stepper will return the where the pot is set to. how do we do this?
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(150);
}
void loop()
{
// get the sensor value
int potVal = analogRead(0);
int stpPos = map(potVal, 0, 1023, 0, 100);
stepper.step(stpPos - previous);
// remember the previous value of the sensor
previous = stpPos;
}
You want an if-else statement. If the input is high, undo the steps you've made, else if the input is low, go to the pot position:
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(150);
pinMode(1, INPUT); // This is the input pin
}
void loop()
{
// get the sensor value
int potVal = analogRead(0);
int stpPos = map(potVal, 0, 1023, 0, 100);
// I added this if-else statement
if (digitalRead(1) == HIGH) {
stepper.step(-previous);
}
else {
stepper.step(stpPos - previous);
}
// remember the previous value of the sensor
previous = stpPos;
}
no just runs one way and when button go's high then go's the other way
but thinking about it, the code is runing over and over so it will keep doing it?
so to shop this i have to do somthing like this?
int ledPin = 13; // LED connected to digital pin 13
int button = 2; // button pin
int count = 0; // Our blink counter
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
if (digitalRead(button)== HIGH) // buttin when high
{
if (count < 1) {
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
count++; // add one (1) to our count
}
}
else {
if(count == 1) count =0; // resetting the count
}
}
if (digitalRead(1) == HIGH) {
stepper.step(-previous);
// reset the previous value to zero
previous = 0;
}
else {
stepper.step(stpPos - previous);
// remember the previous value of the sensor
previous = stpPos;
}