Hello,
I am working on a simple project which requires stepper motor control and sensor readings. I would like to send desired PWM to stepper motor while sensors are reading data.
The simplified code I am working can be found below. MotorPulse() sends the PWM signal, loadcell() reads data. Because of the loadcell() function there exists an undesirable delay between digitalWrite commands of the MotorPulse() function which in turn affects the stepper motors speed. Is there any way I can overcome this problem ? I want to be able to drive the stepper motor at desired speeds while the sensor is reading. (Because of the delay created by loadcell(), it can't get fast, as the PWM signals are slow)
I am thinking about putting the digitalWrite commands inside the "serial.read" functon so that they are executed while the serial.read is working. I am also currently looking at some scheduler libraries which I hope can run these two lines simulatenously, eliminating the delay. I am open to suggestions. I hope I expressed myself clear enough.
Thanks in advance
void MotorPulse() {
// SEND PWM SIGNAL
digitalWrite(stepPin, HIGH);
delay(1);
digitalWrite(stepPin, LOW);
}
void loadcell() {
//LOADCELL READ
units = scale.read( );
}
void setup() {
Serial.begin(9600);
//Set motor pins
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
//Set HX711-Loadcell parameters
scale.set_scale(calibration_factor);
scale.tare();
//Set motor direction
digitalWrite(dirPin, Direction); // Low for düz, HIGH for ters hareket
//Start main loop
while (stopcon=true) {
MotorPulse();
loadcell();
Serial.println(units); // Loadcell Values print
// STOP CONDITION
if (units<maxloadcell*3/4){
break;
}
}
}
void loop() {
}