Code not entering in state conditions

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

  1. When AM ==0, stepper should not run.
  2. When AC == 1, stepper should remain at zero position or return to zero position.
  3. 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);
  }
}

Below is debug log

18:18:37.584 -> AC: 1
18:18:37.584 -> AM: 1
18:18:37.584 -> Current State: 0
18:18:37.633 -> Unknown State: 0
18:18:37.633 -> In Paint Arm
18:18:37.633 -> AC: 1
18:18:37.633 -> AM: 1
18:18:37.633 -> Current State: 0
18:18:37.683 -> Unknown State: 0
18:18:37.683 -> In Paint Arm
18:18:37.709 -> AC: 1
18:18:37.709 -> AM: 1
18:18:37.709 -> Current State: 0
18:18:37.759 -> Unknown State: 0
18:18:37.759 -> In Paint Arm
18:18:37.787 -> AC: 1
18:18:37.787 -> AM: 1
18:18:37.787 -> Current State: 0
18:18:37.820 -> Unknown State: 0
18:18:37.820 -> In Paint Arm
18:18:37.820 -> AC: 1
18:18:37.820 -> AM: 1

So what happens instead?
What did you expect/ try to accomplish?

Maybe the mystery array A could be given a better self explaining name?

a. Please find mystery array log. Changed Name to Incoming_Array
b. I want to identify why is it not entering switch state in void Paint_Arm when conditions are met.

19:28:25.327 -> Unknown State: 0
19:28:25.344 -> In Paint Arm
19:28:25.344 -> AC: 1
19:28:25.344 -> AM: 1
19:28:25.344 -> Incoming Values : | 517 | 520 | 981 | 100 | 594 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
19:28:25.406 -> Current State: 0
19:28:25.452 -> Unknown State: 0
19:28:25.452 -> In Paint Arm
19:28:25.496 -> AC: 1
19:28:25.496 -> AM: 1
19:28:25.496 -> Incoming Values : | 517 | 520 | 981 | 100 | 594 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
19:28:25.675 -> Current State: 0
19:28:25.707 -> Unknown State: 0
19:28:25.707 -> In Paint Arm
19:28:25.733 -> AC: 1
19:28:25.733 -> AM: 1

It does enter switch state, but state is equal to 0 (IDLE). There is no match with that label, so the default is printing.
So somewhere before you should set state appropriately...

Thanks. Help me. How do i achieve this?

Somewhere you need to insert:
state = IDLE1;
Or
state = P_Step2;
Or
state = P_Step3;

Since I do not know what you want to accomplish in the end, I cannot tell where you need to insert this.
Probably somewhere in an if () {} or in a case:

Thanks for guidance. It's done.

Please post the code as it is now fixed for your fellow travelers.

TIA

a7

Inserted condition to switch state IN void Paint_Arm

 switch (state) {
    case IDLE: {
      if (PA == 0){
        state = IDLE1;
      }
      break;
    }

Great that you got it fixed!

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