Hi all,
I'm working on a project where I have to control stepper motor with potentiometer and would like to monitor motor's speed with 4-digit 7 segment led display screen.
I'm using TMC2100 driver and NEMA17 stepper with 200 steps/rev. I connected 2 buttons for start and stop and Hall effect sensor for stopping the motor at the exact position. I've managed to figure out how to control everything and it's working fine accept monitoring the speed on a monitor. The screen is pulsating and the motor is spinning very slow, I can't even control the speed any more.
My question is, how to implement the screen into the code not to have that strange loop delay?
I hope I explained my situation well, so maybe someone can help me.
#include <Arduino.h>
#include <TM1637Display.h>
// defines pins numbers
#define DIR_PIN 9
#define STEP_PIN 8
#define EN_PIN 7
#define POT_PIN A0
#define On 12
#define Off 13
#define SENSOR 2
#define CLK 3
#define DIO 4
int pot;
int motorSpeed;
int state = 0, Loadstate = 0;
TM1637Display display(CLK, DIO);
void setup() {
Serial.begin(9600);
pinMode(DIR_PIN,OUTPUT);
pinMode(STEP_PIN,OUTPUT);
pinMode(EN_PIN,OUTPUT);
pinMode(On, INPUT);
pinMode(Off, INPUT);
pinMode(SENSOR, INPUT);
digitalWrite(EN_PIN,LOW);
digitalWrite(DIR_PIN,HIGH); // Enables the motor to move in a particular direction
digitalWrite(SENSOR, HIGH); //Hall effect sensor read
}
void loop() {
motorSpeed = potValue();
motorStep(motorSpeed);
if (state == 0 && digitalRead(Off) == HIGH && digitalRead(SENSOR) == LOW) {
state = 1;
Loadstate = !Loadstate;
}
if (state == 1 && digitalRead(On) == HIGH) {
state = 0;
Loadstate = !Loadstate;
}
if (Loadstate == HIGH) {
digitalWrite(EN_PIN, HIGH);
}
if (Loadstate == LOW) {
digitalWrite(EN_PIN, LOW);
}
}
void motorStep(int STEP){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(STEP);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(STEP);
}
int potValue()
{
int val = analogRead(POT_PIN);
int Speed = map(val,0,1023,100,500);
return Speed;
}
and the part for the display
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
display.setSegments(data);
display.showNumberDec(motorSpeed, false, 3,1);
