Hi guys,
i tried to get some easy programming. I recognised that the easy stuff is sometimes not that easy!
I wanna have two posible conditions for a switch. The first is a absolute movement for a stepper, the other is a relative movement for a stepper.
Absolute movement:
1 Poti is for movement
1 Poti is for speed
Relative movement:
only 1 Poti for speed (next step will be a absolute middle point...but that is the sound of the future )
So now the problem , in the "Relative movement"-program-part the line with the mapping of the poti valueis causing a delay when the value is around "zero"! BUT i can see in the serial monitor that there is only a problem when its near the minimum value of the poti. I can see that the data in the serial monitor stops for 2-5 seconds and then the program is running on. It looks like a buffer overflow or something else?!?!?!
Has somebody a solution for me??
//Stepper Bibliothek
#include <Stepper.h>
#define STEPS 24 // change this to the number of steps on your motor
Stepper stepper(STEPS, 8, 9, 10, 11); // create an instance of the stepper class, define Steps, define Mototr Pins
// Eingaenge
int wahlAbsolutPin = 12; // Wahlschalter Absolut
int wahlRelativPin = 13; // Wahlschalter Relativ
// Ausgaenge
int enablePin1 = 6;
int enablePin2 = 7;
// Poti Parameter
int sensorSpeed = 0;
int sensorSteps = 0;
int potiMitte = 512;
// Parameter
int prevPotVal = 0;
int motorMove = 0;
int motorSpeed = 0;
int previousmotorSpeed = 0;
const int stepsPerRevolution = 200;
void setup()
{
Serial.begin(115200);
pinMode(wahlAbsolutPin,INPUT); // Wahlschalter Absolut
pinMode(wahlRelativPin,INPUT); // Wahlschalter Relativ
pinMode(enablePin1,OUTPUT);
pinMode(enablePin2,OUTPUT);
}
void loop()
{
sensorSteps = analogRead(A0);
sensorSpeed = analogRead(A1);
if (digitalRead(wahlAbsolutPin) == HIGH) {
digitalWrite(enablePin1, HIGH);
digitalWrite(enablePin2, HIGH);
motorMove = map(sensorSteps, 0, 1023, 24, 0); // get potiMove and map it
motorSpeed = map(sensorSpeed, 0, 1023, 150, 1500); // get potiSpeed and map it
stepper.step(motorMove - prevPotVal);
stepper.setSpeed(motorSpeed);
prevPotVal = motorMove;
}
else if (digitalRead(wahlRelativPin) == HIGH) {
digitalWrite(enablePin1, HIGH);
digitalWrite(enablePin2, HIGH);
motorSpeed = map(sensorSpeed, 0, 1023, 10, 1000); // get potiSpeed and map it
if (motorSpeed > 0) {
stepper.setSpeed(motorSpeed);
stepper.step(stepsPerRevolution/100);
}
}
else {
digitalWrite(enablePin1, LOW);
digitalWrite(enablePin2, LOW);
}
Serial.println(motorMove);
Serial.println(motorSpeed);
delay(2);
}