#define DIR_PIN 12
#define STEP_PIN 13
#define POTI_PIN 4
unsigned long systemtime = 0;
unsigned long target = 0;
int poti = 0;
int speed = 0;
int old_speed = 0;
bool direction = true; // clockwise
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-C6!");
systemtime = millis();
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
digitalWrite(DIR_PIN, direction);
pinMode(POTI_PIN, INPUT);
}
void calc_target_time(){
target = systemtime + speed*10;
}
void get_speed(){
poti = analogRead(POTI_PIN);
int new_speed = map(poti, 0, 4095, 200, 10); // map poti to speed
if(new_speed != old_speed){
speed = new_speed;
Serial.println(new_speed);
old_speed = new_speed;
systemtime = millis();
calc_target_time();
Serial.println(target);
}
}
void loop() {
// put your main code here, to run repeatedly:
get_speed();
// 1.8° / step -> 200 steps for one turn
systemtime = millis();
if(systemtime >= target){
Serial.println("stepping");
calc_target_time();
Serial.println(target);
digitalWrite(STEP_PIN, HIGH); // pulse step pin
delayMicroseconds(500); // wait half a ms to be sure
digitalWrite(STEP_PIN, LOW);
}
/*
for(int i = 0; i < 200; i++){ //Steppers have 200 steps usually
Serial.println("stepping");
digitalWrite(STEP_PIN, HIGH); // pulse step pin
delay(10);
digitalWrite(STEP_PIN, LOW);
delay(100);
}
Serial.println("turning around");
direction = !direction; // turn around
digitalWrite(DIR_PIN, direction);
*/
delay(1); // this speeds up the simulation, default: 10
}