Hi, everybody
I am working on a project related to a motor and a button.
The algorithm looks like this:
-
When the power is connected, all motors turn back and stop when the limit switch is pressed.
-
There is a regular button with which you can switch cells
-
There is a touch button, with which you can perform an action inside the cell
-
If you press the touch button, the motor turns forward at a given number of revolutions and stops
-
If you press the motor again, turn back according to the specified number of revolutions.
The algorithm works well up to point 5. The problem is that the motor turns well forward, but it's hard to turn back.
I thought it was a motor failure, but no, I checked, turned back and forth everything is fine, but there is an error in the algorithm.
Can you please help in correcting the algorithm code
Code:
// Define the pins used to control the Maxon 344515 engine and the touch button
const int motorPin2 = 2; // New motor pin 2
const int motorPin3 = 3; // New motor pin 3
const int motorPin4 = 4;
const int motorPin5 = 5;
const int motorPin6 = 10;
const int motorPin7 = 11;
const int motorPin8 = A0;
const int motorPin9 = A1;
const int touchPin6 = 6;
const int stepsPerRotation = 200; // Change this value depending on the motor specifications
int numRotations = 10; // Change this value to specify the number of rotations
int motorSpeed = 100; // Change this value to specify the speed of the motor
bool forwardRotation = true;
const int LIMIT_SWITCH_PIN = 7;
int currentFunction = 1;
int touchButtonState = LOW;
int touchButtonPrevState = LOW;
int normalButtonState = LOW;
int normalButtonPrevState = LOW;
int functionCount = 4;
void setup() {
Serial.begin(9600);
pinMode(motorPin2, OUTPUT); // New motor pin 2
pinMode(motorPin3, OUTPUT); // New motor pin 3
pinMode(motorPin4, OUTPUT);
pinMode(motorPin5, OUTPUT);
pinMode(motorPin6, OUTPUT); // New motor pin 2
pinMode(motorPin7, OUTPUT); // New motor pin 3
pinMode(motorPin8, OUTPUT);
pinMode(motorPin9, OUTPUT);
pinMode(8, INPUT_PULLUP); // Normal button
pinMode(touchPin6, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
motorBackward();
motorBackward1();
motorBackward2();
motorBackward3();
while (digitalRead(LIMIT_SWITCH_PIN) == HIGH) {}
motorStop();
motorStop1();
motorStop2();
motorStop3();
}
void motorForward() {
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin2, HIGH); // New motor pin 2
digitalWrite(motorPin3, LOW); // New motor pin 3
}
void motorBackward() {
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin2, LOW); // New motor pin 2
digitalWrite(motorPin3, HIGH); // New motor pin 3
}
void motorStop() {
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin2, LOW); // New motor pin 2
digitalWrite(motorPin3, LOW); // New motor pin 3
}
void motorForward1() {
digitalWrite(motorPin2, HIGH); // New motor pin 2
digitalWrite(motorPin3, LOW); // New motor pin 3
}
void motorBackward1() {
digitalWrite(motorPin2, LOW); // New motor pin 2
digitalWrite(motorPin3, HIGH); // New motor pin 3
}
void motorStop1() {
digitalWrite(motorPin2, LOW); // New motor pin 2
digitalWrite(motorPin3, LOW); // New motor pin 3
}
void motorForward2() {
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin5, LOW);
}
void motorBackward2() {
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin5, HIGH);
}
void motorStop2() {
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin5, LOW);
}
void motorForward3() {
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, HIGH); // New motor pin 2
digitalWrite(motorPin9, LOW); // New motor pin 3
}
void motorBackward3() {
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin8, LOW); // New motor pin 2
digitalWrite(motorPin9, HIGH); // New motor pin 3
}
void motorStop3() {
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, LOW); // New motor pin 2
digitalWrite(motorPin9, LOW); // New motor pin 3
}
void loop() {
touchButtonState = digitalRead(6);
if (touchButtonState == HIGH && touchButtonPrevState == LOW) {
switch (currentFunction) {
case 1:
function1();
break;
case 2:
function2();
break;
case 3:
function3();
break;
case 4:
function4();
break;
}
}
touchButtonPrevState = touchButtonState;
normalButtonState = digitalRead(8);
if (normalButtonState == HIGH && normalButtonPrevState == LOW) {
currentFunction++;
if (currentFunction > functionCount) {
currentFunction = 1;
}
Serial.print("Switched to function ");
Serial.println(currentFunction);
}
normalButtonPrevState = normalButtonState;
delay(50);
}
void function1() {
Serial.print("function1");
int numSteps = numRotations * stepsPerRotation;
if (forwardRotation) {
motorForward();
} else {
motorBackward();
}
for (int i = 0; i < numSteps; i++) {
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin2, HIGH); // New motor pin 2
delayMicroseconds(500);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin2, LOW); // New motor pin 2
delayMicroseconds(500);
}
motorStop();
Serial.print("Number of revolutions: ");
Serial.println(numRotations);
forwardRotation = !forwardRotation;
}
void function2() {
Serial.print("function2");
int numSteps = numRotations * stepsPerRotation;
if (forwardRotation) {
motorForward1();
} else {
motorBackward1();
}
for (int i = 0; i < numSteps; i++) {
digitalWrite(motorPin2, HIGH); // New motor pin 2
delayMicroseconds(500);
digitalWrite(motorPin2, LOW); // New motor pin 2
delayMicroseconds(500);
}
motorStop1();
Serial.print("Number of revolutions: ");
Serial.println(numRotations);
forwardRotation = !forwardRotation;
}
void function3() {
Serial.print("function3");
int numSteps = numRotations * stepsPerRotation;
if (forwardRotation) {
motorForward2();
} else {
motorBackward2();
}
for (int i = 0; i < numSteps; i++) {
digitalWrite(motorPin4, HIGH);
delayMicroseconds(500);
digitalWrite(motorPin4, LOW);
delayMicroseconds(500);
}
motorStop2();
Serial.print("Number of revolutions: ");
Serial.println(numRotations);
forwardRotation = !forwardRotation;
}
void function4() {
Serial.print("function4");
int numSteps = numRotations * stepsPerRotation;
if (forwardRotation) {
motorForward3();
} else {
motorBackward3();
}
for (int i = 0; i < numSteps; i++) {
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin8, HIGH); // New motor pin 2
delayMicroseconds(500);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin8, LOW); // New motor pin 2
delayMicroseconds(500);
}
motorStop3();
Serial.print("Number of revolutions: ");
Serial.println(numRotations);
forwardRotation = !forwardRotation;
}