Hi, I've been trying to make a stepper motor to run at half the speed of another motor, however the motor refuses to spin. The motor works but not whith this code. The RPM of the controlling motor is measured using a hall sensor switch. I am using the Adruino Uno R3 and the stepper motor is wired according to the schematic given on Arduinos website with a 9-volt battery as secondary power source:
The picture has a potentiometer to control the speed of the motor but I instead connected a hall sensor to PWM2.
This text will be hidden
The code itself is essentially in two parts. The first part is just the tachometer to get a RPM, and then a simple motor control to set the speed of the stepper motor to half
of that measured. Code below:
#include <Stepper.h>
const int hall_pin = 2;
const float hall_thresh = 50; //number of times the hall sensor is tripped before rpm is calculated
const int stepsPerRevolution = 200;
void setup() {
Serial.begin(115200);
pinMode(hall_pin, INPUT);
}
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void loop() {
//Tachometer
bool on_state = true;
float hall_count = 1.0;
float start = micros();
while (true) {
if (digitalRead(hall_pin) == 0) {
if (on_state == false) {
on_state = true;
hall_count++;
}
} else {
on_state = false;
}
if (hall_count >= hall_thresh) {
break;
}
}
float end_time = micros();
float time_passed = ((end_time - start) / 1000000.0);
Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (hall_count / time_passed) * 60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
//Motorcontrol
int motorspeed = rpm_val / 2;
if (motorspeed > 0) {
myStepper.setSpeed(motorspeed);
Serial.print(motorspeed);
Serial.println(" motorspeed");
}
}
The issue I am experiencing is that I can't get the motor to run at all. I've tried the different parts of the code separately and they both work on their own, but when I try to combine them only the rpm meter works, with no rotation on of the steppermotor. I've also tried to set the motorspeed manually but without any success.
Thank you in advance for any help