Incremental Counter

Hi guys!

I'm trying to make a simple counter that, when it reaches the desired limit, trigger the next part of the code.

This is the code that I'm using, I'm using accelstepper library.

#include "AccelStepper.h"

AccelStepper stepper (1, 9 , 8);

#define home_switch 4
#define inductiveSensorpin7 7
long initial_homing = -1;
int x;



void setup() {


   
   pinMode(home_switch, INPUT_PULLUP);
   
   delay(150);  

  stepper.setCurrentPosition(0); 
  stepper.setMaxSpeed(1.0);      
  stepper.setAcceleration(1500.0); 
  initial_homing=1;
  
  while (digitalRead(home_switch)) {    
    stepper.moveTo(initial_homing);  
    initial_homing++;  
    stepper.run(); 
    delay(5);
}


  stepper.setCurrentPosition(0); 
  stepper.setMaxSpeed(1.0);      
  stepper.setAcceleration(1500.0); 
  initial_homing=1;

  while (!digitalRead(home_switch)) { 
    stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
    delay(5);
  }
  
  stepper.setCurrentPosition(0);
  x == 0;
  stepper.setMaxSpeed(30.0);      
  stepper.setAcceleration(40.0);
  stepper.setSpeed(20);  

}
 
void loop()
{  
  if (digitalRead(inductiveSensorpin7) == HIGH) {
    x++;
  }  

  if (x == 9) {
    stepper.runSpeed();
  }


  if (digitalRead(home_switch) == HIGH) {
    stepper.stop();
    x=0;
  }
}

As you can see, in the loop section of the code i have the following instructions, but I'm not having the desired behavior:

void loop()
{  
  if (digitalRead(inductiveSensorpin7) == HIGH) {
    x++;
  }  

  if (x == 9) {
    stepper.runSpeed();
  }


  if (digitalRead(home_switch) == HIGH) {
    stepper.stop();
    x=0;
  }
}

TL;DR : I need to run the comand "stepper.runSpeed()" when the inductiveSensorpin7 is activated x number of times.

Thank you in advance!

Have you tried printing the value of x ?

It will be incremented each time through loop() not each time the sensor is triggered

You need to detect when the sensor becomes triggered not when it is triggered

Okay. would it work if I put the code like this?

  if (digitalRead(inductiveSensorpin7)) {
    x++;
  }

manchap23:
Okay. would it work if I put the code like this?

  if (digitalRead(inductiveSensorpin7)) {

x++;
  }

No

Look at the StateChangeDetection example in the IDE

Sure! ill check it right now

I changed the code in function of the example of StateChangeDetection:

#include "AccelStepper.h"

AccelStepper stepper (1, 9 , 8);

#define home_switch 4
#define buttonState 7
long initial_homing = -1;
int x;
int sensorPushCounter = 0;
int sensorState = 0;
int lastsensorState = 0;


void setup() {


   
   pinMode(home_switch, INPUT_PULLUP);
   
   delay(150);  

  stepper.setCurrentPosition(0); 
  stepper.setMaxSpeed(1.0);      
  stepper.setAcceleration(1500.0); 
  initial_homing=1;
  
  while (digitalRead(home_switch)) {    
    stepper.moveTo(initial_homing);  
    initial_homing++;  
    stepper.run(); 
    delay(5);
}


  stepper.setCurrentPosition(0); 
  stepper.setMaxSpeed(1.0);      
  stepper.setAcceleration(1500.0); 
  initial_homing=1;

  while (!digitalRead(home_switch)) { 
    stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
    delay(5);
  }
  
  stepper.setCurrentPosition(0);
  x == 0;
  stepper.setMaxSpeed(30.0);      
  stepper.setAcceleration(40.0);
  stepper.setSpeed(20);  

}
 
void loop()
{       
  sensorState = digitalRead(buttonState);

  if (sensorState != lastsensorState){
    if (buttonState == HIGH){
      sensorPushCounter++;
    }
  }


  if (sensorPushCounter % 9 == 0) {
    stepper.runSpeed();
  } else {
    stepper.stop();
  }


  if (digitalRead(home_switch) == HIGH) {
    stepper.stop();
    sensorPushCounter = 0;
    }
}

Now the stepper runs like crazy without stopping. It doesn't respond to the sensor or to the home_switch.

Any idea of why??

After you test for sensorState != lastsensorState you need to set lastsensorState to the current sensorState.

  x == 0;

Pointless. Why are you comparing the two values when you do nothing either way?