#include "stepper_motor.h"
int step_count = 0;
uint32_t period = 40825UL << 16; // 20412,5 us fixed point arithmetic time = 0
stepper_motor::stepper_motor()
{
//pinMode(BUILTIN_LED,OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(en_pin, OUTPUT);
alive_bool = true;
position_bool = false;
step_count = 0;
}
static void stepper_motor::callback()
{
if (step_count < 240)
{
uint32_t new_period = sqrt(2 * step_count / 4.8);
period -= new_period;
step_count++;
}
else
{
return;
}
}
void stepper_motor::step_pwm()
{
digitalWrite(en_pin, HIGH);
delay(0.005);
digitalWrite(dir_pin, HIGH);
//LOW - Move towards the sensor
//HIGH - Move away from the sensor
delay(0.005);
timer1.initialize(period);
timer1.attachInterrupt(callback);
timer1.pwm(step_pin, 512);
}