Hello guys,
does anyone here have an idea how to improve the following code so that it fulfils the condition. Up to now I got so far and actually hoped to find a remedy with the new limits of the sensor on the gear wheel (A3). Namely, the stepper motor should always move back and forth between these boundaries so that it always remains in the 180 degree range.
To do this, it should take the output voltage of the sensor which is directly connected to it via a 1 to 1 gear. Using the rotation potentiometer on A0, I have already been able to determine exactly these limits (0.42 degrees at the start of the scale and 2.42 degrees at the end of the scale) using a laser on the A3 rotation potentiometer and also calculate the steps in between, which are exactly 1020. It has also shown that the whole mechanism works.
I suspect that I have an error in my thinking, in that the sensor is the actual clock generator for the motor, but at the same time the motor is always one step ahead as it actuates it. And this is also the current result, the motor turns in any direction, sometimes quickly, sometimes slowly. Sometimes it drives out of the limits or stays somewhere in the radius between any two.
If anyone has a new approach for me or could tell me where my error could be, I would be more than happy and grateful.
const int motorPin1 = 8;
const int motorPin2 = 9;
const int motorPin3 = 10;
const int motorPin4 = 11;
const int sensorPin = A0;
const int stepsPerRevolution = 4096;
int stepNumber = 0;
const float minVoltage = 0.42;
const float maxVoltage = 2.41;
const int totalSteps = 1020;
void setup() {
Serial.begin(9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop() {
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
float adjustedMinVoltage = 0.99;
float adjustedMaxVoltage = 1.09;
int targetStep = map(voltage, adjustedMinVoltage, adjustedMaxVoltage, 0, totalSteps);
int stepsToMove = targetStep - stepNumber;
Serial.print("Sensor Value: "); Serial.println(sensorValue);
Serial.print("Calculated Voltage: "); Serial.println(voltage);
Serial.print("Target Step: "); Serial.println(targetStep);
Serial.print("Current Step: "); Serial.println(stepNumber);
Serial.print("Steps to Move: "); Serial.println(stepsToMove);
if (stepsToMove != 0) {
int direction = stepsToMove > 0 ? 1 : -1;
stepMotor(direction);
stepNumber += direction;
delay(10);
}
}
void stepMotor(int step) {
switch (stepNumber % 4) {
case 0:
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
break;
case 1:
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
break;
case 2:
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
break;
case 3:
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
break;
}
}