Can't get motor to rotate 3 times then stop - need help

I'm working on a wood bundler for myself. I want to be able to push a Green Button (Button1) and have the DC motor rotate 3 times then stop immediately at the same position it started. Then do that every time Green Button is pressed. I'd also like to be able to push Red Button (Button2) to rotate motor as long as the Red Button is being pushed the stop immediately when released. The Red Button will be used as a "nudge" button or to move motor to a desired location/free rotating as long as the button is pressed. I'm using and Elegoo Uno V3, BTS7960 motor controller, AS5600 magnetic encoder and a 24V Brushed Geared DC bike motor.

The pin outs are as follows:
BTS7960
M+ to Motor +
M- to Motor-
B+ to 24V power supply +
B- to 24V power supply-
VCC to 5v pin - UNO
GRD to GRD pin - UNO
R_EN & L_EN to digital pin 11 - UNO
RPWM to digital pin 9 - UNO
LPWM to digital pin 10 - UNO

AS5600
VCC to 5v - UNO
GRD to GRD - UNO
SCL to analog pin A5 - UNO
SDA to analog pin A4 - UNO

I've tried several different iterations of code, pin outs and configurations. I even tried using AI. The latest iteration of code is below. I need help to get this working. Been working on this for weeks with no solution. Would really appreciate any help to get this working. Thanks!

BUNDLER CODE:

// Include necessary libraries
#include <BTS7960.h>
#include <AS5600.h>
#include <Wire.h>

#define RPWM 9  // Replace these with your actual motor control pin numbers
#define LPWM 10
#define PWM 11
#define BUTTON_PIN 2  // Change this to the actual button pin

const int ENCODER_RESOLUTION = 4096;  // Assuming 12-bit AS5600 encoder
const int TARGET_ROTATIONS = 3;

int buttonState = LOW;
int previousButtonState = LOW;
int currentAngle = 0;
int targetAngle = 0;
bool motorRunning = false;

// Motor control functions (implementation details will depend on your motor driver)
void motor_stop() {
  // Set PWM to 0 or other logic to stop the motor
  analogWrite(PWM, 0);
}

void motor_cw() {
  // Set PWM and direction pins for clockwise rotation
  analogWrite(PWM, 255);  // Adjust duty cycle as needed
  digitalWrite(RPWM, HIGH);
  digitalWrite(LPWM, LOW);
}

void motor_ccw() {
  // Set PWM and direction pins for counter-clockwise rotation
  analogWrite(PWM, 255);  // Adjust duty cycle as needed
  digitalWrite(RPWM, LOW);
  digitalWrite(LPWM, HIGH);
}

int readEncoder() {
  // Implement your encoder reading logic here (replace with actual code)
  // This example assumes a simplified read function that returns the current angle
  // You'll need to replace this with the actual reading code for your encoder
  return /* actual encoder reading code */;
}

int calculateTargetAngle() {
  // Consider encoder wrap-around for multi-turn capability
  targetAngle = (currentAngle + (TARGET_ROTATIONS * ENCODER_RESOLUTION)) % ENCODER_RESOLUTION;
  return targetAngle;
}

void setup() {
  Serial.begin(9600);
  Serial.println("START");
  pinMode(RPWM, OUTPUT);
  pinMode(PWM, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Button with internal pull-up resistor
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == LOW && previousButtonState == HIGH) {
    // Button pressed
    if (!motorRunning) {
      motorRunning = true;
      targetAngle = calculateTargetAngle();
    }
  }

  if (motorRunning) {
    currentAngle = readEncoder();

    if (abs(currentAngle - targetAngle) <= 10) {  // Adjust tolerance as needed
      motor_stop();
    } else if (currentAngle < targetAngle) {
      motor_cw();
    } else {
      motor_ccw();
    }
  }

  previousButtonState = buttonState;
}

How are you telling the Arduino the number of turns the motor made?

Which pin is used for this purpose?

And what is the purpose of this routine?

int readEncoder() {
  // Implement your encoder reading logic here (replace with actual code)
  // This example assumes a simplified read function that returns the current angle
  // You'll need to replace this with the actual reading code for your encoder
  return /* actual encoder reading code */;
}

The operation result is always zero: "
targetAngle = (currentAngle + (TARGET_ROTATIONS * ENCODER_RESOLUTION)) % ENCODER_RESOLUTION;"

AI seems to work better for some than others.

Here you see it glossed over a major detail and until you fill in the blanks it clearly copped to leaving out of its solution, the code will be non-functional.

Perhaps the AI can digest the capabilities and operation and programming for the specific encoder on you moto and write that low level code for you.

That you did not notice this missing piece suggests that you haven't carefully read the AI's output for basic plausibility and correctness.

Code can be read, AI will be more useful if you develop an ability to read code. No good writer of anything is not also widely read, you could start by following threads on these fora that match your interests and curretn abilities.

a7

  • the trick with you're nudge button is recognizing when to stop. seems you'll need to recognize that the code is in a "nudge" state, set when the nudge button is pressed and cleared when the nudge button is released as well as turning off the motor

  • i've found that encoders need to be tracked by an interrupt, even when turning by hand.

  • pressing your green button should set a "wrap" state, compute a target "position" computed by add the count of 3 revolutions to the current encoder position and turn the motor on. the motor should be stopped when in the "wrap" state and the encoder position exceeds the target position as well as clearing the state

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.