Hi there,
I am currently working on a project, I am a kind of new to this and was wondering if I could get a little bit of help in getting it to work.
My circuit includes a stepper motor (step connected to pin 2 and direction connected to pin 3), a button (connected to pin 5) and a force sensitive resistor (connected to A0) and 2x 10k ohm
resistors for the button and the FSR.
I would like my circuit to work in the following way.
When the FSR detects a force of 500 or more pressed for 1 second or longer, the stepper motor turns 10 revolutions clockwise at 60rpm. However if the button is pressed for 2 seconds or less, and is released before the 10 revolutions have been achieved, the motor will stop exactly where it is when the button is released.
Then once the motor has stopped turning, if the button is held down for 5 seconds or longer, the motor will reset to it's starting position by turning anticlockwise (either 10 revolutions anticlockwise or however many revolutions it took if the motor was stopped early).
The stepper motor I am using is: https://uk.rs-online.com/web/p/stepper-motors/1805279
It is a 6 wire stepper motor but I am leaving the centre tap wires disconnected as I only want unipolar motion, so want it to function as if it was a 4 wire motor.
The FSR I am using is: https://uk.rs-online.com/web/p/force-sensors/2122467
I have included a wiring diagram and the code currently used.
Any help would be much appreciated. Thank you!
#include <Stepper.h>
// Define number of steps per revolution
const int stepsPerRevolution = 200;
// Define FSR pin
const int fsrPin = A0;
// Define stepper motor pins
const int stepPin = 2; // Changed to pin 2
const int dirPin = 3; // Changed to pin 3
// Define button pin
const int buttonPin = 5;
// Create stepper object
Stepper myStepper(stepsPerRevolution, stepPin, dirPin);
// Define variables
unsigned long startMillis = 0;
unsigned long currentMillis = 0;
const unsigned long sensingInterval = 1000; // Sensing interval in milliseconds
const int forceThreshold = 500; // Force threshold for activation
bool stopMotor = false;
long stepsTaken = 0; // To keep track of steps during clockwise rotation
bool clockwiseRotationComplete = false;
void setup() {
// Set up the FSR pin as input
pinMode(fsrPin, INPUT);
// Set up the motor pins
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Set up the button pin
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Read the value from the FSR
int fsrValue = analogRead(fsrPin);
// Check if the force is above the threshold
if (fsrValue >= forceThreshold) {
// If force is detected, start the timer
if (startMillis == 0) {
startMillis = millis();
}
// Check if the elapsed time is more than the sensing interval
currentMillis = millis();
if (currentMillis - startMillis >= sensingInterval) {
// If the sensing interval has passed, rotate the motor
rotateMotor();
// Reset the timer
startMillis = 0;
}
} else {
// If force is not detected, reset the timer
startMillis = 0;
}
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Start the timer when the button is pressed
unsigned long buttonPressStart = millis();
// Wait for the button to be released or timeout after 2 seconds
while (digitalRead(buttonPin) == LOW && millis() - buttonPressStart < 2000) {
delay(10);
}
// If the button is released before 2 seconds, stop the motor
if (millis() - buttonPressStart < 2000) {
stopMotor = true;
} else if (millis() - buttonPressStart >= 5000) {
// If the button is held for 5 seconds or more, return the motor to its starting position anticlockwise
returnToStart();
}
}
// If the motor was stopped by the button, update the flag
if (stopMotor) {
stopMotor = false;
clockwiseRotationComplete = false;
}
}
// Function to rotate the motor 10 revolutions at 60 RPM
void rotateMotor() {
// Set the direction to clockwise
digitalWrite(dirPin, HIGH);
// Calculate the delay between steps based on RPM
int rpm = 60;
int delayBetweenSteps = 60000 / (stepsPerRevolution * rpm);
// Rotate the motor for 10 revolutions or until stopped
for (int i = 0; i < 10 * stepsPerRevolution && !stopMotor; i++) {
// Step the motor
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayBetweenSteps);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayBetweenSteps);
// Increment steps taken during clockwise rotation
stepsTaken++;
}
// If the motor completes the rotation, set the flag
if (!stopMotor) {
clockwiseRotationComplete = true;
}
}
// Function to return the motor to the starting position anticlockwise
void returnToStart() {
// Set the direction to anticlockwise
digitalWrite(dirPin, LOW);
// Calculate the delay between steps based on RPM
int rpm = 60;
int delayBetweenSteps = 60000 / (stepsPerRevolution * rpm);
// Calculate the number of steps needed to return to the starting position
int stepsToStart = clockwiseRotationComplete ? stepsTaken : 0;
// Rotate the motor to return to the starting position
for (int i = 0; i < stepsToStart; i++) {
// Step the motor
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayBetweenSteps);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayBetweenSteps);
}
// Reset steps taken
stepsTaken = 0;
}