Hello,
I face a problem using 3 DC motors connected mechanically to 3 levers. My code (see below) is correct but I can't find a solution to send the last position of each lever (angular position). In fact, I detect which of the 3 levers has moved and then I want to send a letter corresponding to the motor which has moved (F, G or H) with the last position at the end of the movement of the lever. Currently, I send various positions with a delay of 200ms (the positions during the movement of the lever), could you modify my code to get the letter and the last position of the lever ? Or a strategy to detect that the lever has finished its movement. I have tried different strategies but without success.
// Libraries
#include <AFMotor.h>
// Motor object
AF_DCMotor motor1(1); // Assuming you have connected the motor 1 to motor port 1
AF_DCMotor motor2(2); // Assuming you have connected the motor 2 to motor port 2
AF_DCMotor motor3(3); // Assuming you have connected the motor 3 to motor port 3
// Potentiometer pin
const int potPinM1 = A0;
const int potPinM2 = A1;
const int potPinM3 = A2;
// Variables
//int potValue;
int potValueM1;
int potValueM2;
int potValueM3;
int potValueM4;
int potValueM1_init;
int potValueM2_init;
int potValueM3_init;
int potValueM4_init;
int prevMotor1Position = 10000; //10000 never reached
int prevMotor2Position = 10000; //10000 never reached
int prevMotor3Position = 10000; //10000 never reached
int targetPosition1;
int targetPosition2;
int targetPosition3;
int targetPosition4;
int currentPosition1;
int currentPosition2;
int currentPosition3;
int currentPosition4;
int threshold=0;
int MatLabMode;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set initial target position and current position
targetPosition1 = 40;
targetPosition2 = 40;
targetPosition3 = 40;
potValueM1 = 0;
potValueM2 = 0;
potValueM3 = 0;
potValueM1_init = analogRead(potPinM1);
potValueM2_init = analogRead(potPinM2);
potValueM3_init = analogRead(potPinM3);
// Set up motor driver
motor1.setSpeed(255); // Set a constant maximum speed for the motor 1
motor2.setSpeed(255); // Set a constant maximum speed for the motor 2
motor3.setSpeed(255); // Set a constant maximum speed for the motor 3
// Set up potentiometer pin as input
pinMode(potPinM1,INPUT);
pinMode(potPinM2,INPUT);
pinMode(potPinM3,INPUT);
}
void loop() {
// Read potentiometer value (feedback)
potValueM1 = analogRead(potPinM1);
potValueM2 = analogRead(potPinM2);
potValueM3 = analogRead(potPinM3);
// Map potentiometer value to target position (0-180)
currentPosition1 = map(potValueM1, 0, 1023, 0, 180);
currentPosition2 = map(potValueM2, 0, 1023, 0, 180);
currentPosition3 = map(potValueM3, 0, 1023, 0, 180);
if (Mode == 1 ) // Master Level to move manually the 3 levers
{
targetPosition1 = currentPosition1 ;
targetPosition2 = currentPosition2 ;
targetPosition3 = currentPosition3 ;
}
if (Mode == 2 ) // Lever 1 master
{
targetPosition1 = currentPosition1 ;
}
if (Mode == 3 ) // Lever 2 master
{
targetPosition2 = currentPosition2 ;
}
if (Mode == 4 ) // Lever 3 master
{
targetPosition3 = currentPosition3 ;
}
// Calculate the difference between current and target positions
int positionError1 = targetPosition1 - currentPosition1;
int positionError2 = targetPosition2 - currentPosition2;
int positionError3 = targetPosition3 - currentPosition3;
// Check the difference between the previous and current positions to detect movement
int motor1Difference = abs(currentPosition1 - prevMotor1Position);
int motor2Difference = abs(currentPosition2 - prevMotor2Position);
int motor3Difference = abs(currentPosition3 - prevMotor3Position);
// Determine the motor direction based on the position error
if (positionError1 > threshold) {
motor1.run(FORWARD);
} else if (positionError1 < -threshold) {
motor1.run(BACKWARD);
} else {
motor1.run(RELEASE);
}
if (positionError2 > threshold) {
motor2.run(FORWARD);
} else if (positionError2 < -threshold) {
motor2.run(BACKWARD);
} else {
motor2.run(RELEASE);
}
if (positionError3 > threshold) {
motor3.run(FORWARD);
} else if (positionError3 < -threshold) {
motor3.run(BACKWARD);
} else {
motor3.run(RELEASE);
}
if (Mode == 1) // Master level
{
if ((prevMotor1Position != 10000) && (motor1Difference > 2))
{
Serial.print("F,");
Serial.println(currentPosition1);
delay(200);
}
if ((prevMotor2Position != 10000) && (motor2Difference > 2))
{
Serial.print("G,");
Serial.println(currentPosition2);
delay(200);
}
if ((prevMotor3Position != 10000) && (motor3Difference > 2))
{
Serial.print("H,");
Serial.println(currentPosition3);
delay(200);
}
prevMotor1Position = currentPosition1;
prevMotor2Position = currentPosition2;
prevMotor3Position = currentPosition3;
}
// Add a delay if needed
delay(20); // Adjust the delay time as necessary
}