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;
}