Dear Forum,
Here below is entire code. Entire code is working fine except void Paint_Arm(). I added various debugging to identify the problem but not able to identify. I am also giving Serial Monitor results to analyze.
Here idea is
- When AM ==0, stepper should not run.
- When AC == 1, stepper should remain at zero position or return to zero position.
- When AC == 0, stepper should rotate 180 degree. It has 30:1 gearbox hence accordingly adjusted.
#include <AccelStepper.h>
#include <nRF24L01.h>
#include <RF24.h>
#define BAUDRATE 115200
RF24 radio(9, 10); // CE, CSN
const byte Surf1_address[6] = { '1', 'N', '0', '1', '0' };
#define pul 7
#define dir 6
AccelStepper stepper(1, pul, dir); // 6 pulse and 7 direction
const int stepPin = 7;
const int dirPin = 6;
int x, jL, jR, ang, drive_sp, AM, AC, FR, arm_sp, ES, AL, AR, Aup, Alo, TRun, PA, DI, steps;
int n = 15;
int a[16];
int A[16];
long initial_homing = -1; // being used to home Stepper at startup
unsigned long lastRecvTime = 0;
int currentPosition = 0;
int angle = 0;
const int Min_Angle = 35;
const int Max_Angle = 80;
const int Paint_Min_Angle = 0;
const int long Paint_Max_Angle = 180UL;
int Min_Arm_Sp = 100;
int Max_Arm_Sp = 1000;
const int long StepsperRevolution = 400UL;
#define Relay_Blast 3 // Relay for Blast Relay operation
float anglePerStep = 0.9; // 400 steps per revolution
int numstep;
const int TIMEOUT_DURATION = 3000;
int stepper_state;
enum State { IDLE,
Step1,
Step2,
Step3,
Step4,
IDLE1,
P_Step1,
P_Step2,
P_Step3 } state = IDLE;
void setup() {
Serial.begin(9600);
resetData();
Radio();
initStepper();
pinMode(Relay_Blast, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH);
digitalWrite(Relay_Blast, LOW);
}
void loop() {
handleReceivedData();
unsigned long now = millis();
if (now - lastRecvTime > TIMEOUT_DURATION) {
Serial.println("Signal lost");
resetData(); // Signal lost, Reset
}
// p_array();
}
void handleReceivedData() {
Data_recieve(a, n);
AC = A[5];
PA = A[14];
if (PA == 1 && AC == 1) {
homing();
}
if (PA == 1) {
Arm();
}
if (PA == 0) {
Paint_Arm();
Serial.println("In Paint Arm");
}
}
void homing() {
// Serial.println("Homing");
AC = A[5];
AL = A[9];
AR = A[10];
stepper.setMaxSpeed(3000);
stepper.setSpeed(2000);
stepper.setAcceleration(10000); // Set Acceleration
if (AC == 1 && AL == 0) {
Serial.println("Right");
stepper.moveTo(-initial_homing); // Set the position to move to
initial_homing++;
delay(0);
stepper.run();
}
stepper.setCurrentPosition(0);
if (AC == 1 && AR == 0) {
Serial.println("Left");
stepper.setCurrentPosition(0);
stepper.moveTo(+initial_homing); // Set the position to move to
initial_homing++;
delay(0);
stepper.run();
}
stepper.setCurrentPosition(0);
}
void Paint_Arm() {
// Receive data and check for timeout
Data_recieve(a, n);
unsigned long now = millis();
if (now - lastRecvTime > TIMEOUT_DURATION) {
Serial.println("Signal lost");
resetData(); // Signal lost, Reset
}
// Update state variables
AC = A[5];
AM = A[7];
// Initialize stepper motor settings
stepper.setMaxSpeed(2000.0);
stepper.setSpeed(2000.0);
stepper.setAcceleration(1000);
Serial.print("AC: ");
Serial.println(AC);
Serial.print("AM: ");
Serial.println(AM);
// Move Data_recieve(a, n) outside the switch statement
Data_recieve(a, n);
Serial.print("Current State: ");
Serial.println(state);
switch (state) {
case IDLE1:
Serial.println("Entering IDLE1");
// State machine
if (AM == 0) {
Serial.println("AM ====> 0");
stepper.stop();
state = IDLE1;
Serial.println("Transitioning to IDLE1 from IDLE1");
}
if (AM == 1) {
stepper.setCurrentPosition(0);
state = P_Step2;
Serial.println("Transitioning to P_Step2 from IDLE1");
}
break;
case P_Step2:
Serial.println("Entering P_Step2");
// Receive data for Step2
AC = A[5];
AM = A[7];
stepper.setMaxSpeed(2000.0);
stepper.setSpeed(2000.0);
stepper.setAcceleration(1000);
if (AC == 0) {
// Move to Target Position1
int targetPosition1 = stepper.currentPosition() + 30 * Paint_Max_Angle;
Serial.println("Moving to Target Position1 in P_Step2: " + String(targetPosition1));
stepper.moveTo(targetPosition1);
stepper.runToPosition();
delay(100);
Serial.println("Paint Cycle 1 Complete");
state = P_Step3;
Serial.println("Transitioning to P_Step3 from P_Step2");
}
break;
case P_Step3:
Serial.println("Entering P_Step3");
if (AC == 1) {
// Move to Step3 to reach 0 Position
Serial.println("Stepper Position: " + String(stepper.currentPosition()));
// Move to center (zero) position
Serial.println((stepper.currentPosition()));
Serial.println("Moving to Center Position in P_Step3: 0");
stepper.moveTo(0); // Move to position 0
stepper.runToPosition();
// Set state back to IDLE1
state = IDLE1;
Serial.println("Transitioning to IDLE1 from P_Step3");
}
break;
default:
Serial.print("Unknown State: ");
Serial.println(state);
}
}