Logic help with Hall sensor

I decided to try and add a hall sensor to my project so I don’t have to worry about missed steps but can’t seem to figure the code out.
What I’m trying to do is have the motor turn a certain number of times past the Hall sensor and then stop.

I’m using an Allegro A3144EUA that activates LOW.
(The turn-on threshold is 35-450 Gauss, the minimum hysteresis is 20 Gauss, and the operating voltage range is 4.5 - 24 volts.).

What happens is the code will just run the steps I put in on stapler.move() without regard to the times it has passed the hall sensor and if I uncomment the delay(250) to give the sensor time to read the low or high then the motor takes short steps until it reaches whatever value I put into stapler.move().

I borrowed the code from a counting program I found online and it did the counting and I’ve been trying to incorporate it into my sketch without luck. I will post the counter code and what I have so far and any help with a direction would be appreciated.
thanks

Here is the code I’m trying to incorporate

int positionSensor = A0; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(positionSensor, INPUT_PULLUP); 
Serial.begin(9600);
}

void loop(){
val = digitalRead(positionSensor); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
currentState = 1;
}
else {
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
delay(250);
}

Here is my attempt

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 5, 6);

const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis = LOW;

//VALUES FOR TURNING STAPLER 4 TURNS(using position Sensor)
int hallcounter = LOW;
int hallcurrentState = LOW;
int hallpreviousState = LOW;
int positionSensor = A0;

void setup() {

  pinMode(button, INPUT_PULLUP);
  pinMode(positionSensor, INPUT_PULLUP);
  stapler.setMaxSpeed(1000);
  stapler.setAcceleration(6000);
  Serial.begin(115200);

}

void loop() {

 hallcurrentState = digitalRead(positionSensor);
 
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = currentMillis;
    old_val = val;
  }
  if (buttonstate == 1) {
    stapler.move(400);
    if (hallcounter >= 4); {                  //keep checking till it hits 4
      stapler.stop();
    }
  }
  if (hallcurrentState != hallpreviousState) {
    if (hallcurrentState == 1) {
      hallcounter = hallcounter + 1;
      Serial.println(hallcounter);
    }
  }
  hallpreviousState = hallcurrentState;
   if (hallcounter >= 4) {
     buttonstate = LOW;
     hallcounter = 0;
     hallcurrentState = 0;
  }
  stapler.run();
}
    if (hallcurrentState == HIGH)

Does the value of hallcurrentState ever change ?

    hallval = digitalRead(positionSensor);      //keep checking till it hits 4You never use the value of hallval in the program

int hallcounter = LOW;

You appear to be using this variable to count how many times you've seen the hall effect sensor. Why does LOW make sense as an initial value? Why not false?

Hi,

Have you written some code that just checks that the Arduino is responding to the Hall Effect Output?

What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

UKHeliBob:

    if (hallcurrentState == HIGH)

Does the value of hallcurrentState ever change ?

Yes, when the magnet leaves the Hall sensor it goes HIGH and then LOW when it goes by the Hall sensor.

    hallval = digitalRead(positionSensor);      //keep checking till it hits 4You never use the value of hallval in the program

I see where I left that out at the top, with all my attempts I must have deleted it. I have it a little farther down to keep checking the value but I will put it up above.

PaulS:

int hallcounter = LOW;

You appear to be using this variable to count how many times you’ve seen the hall effect sensor. Why does LOW make sense as an initial value? Why not false?

I used low because the initial code used low and it worked so I didn’t want to mess with it. Would false make it run any differently than low?

TomGeorge:
Hi,

Have you written some code that just checks that the Arduino is responding to the Hall Effect Output?

What model Arduino are you using?

Thanks… Tom… :slight_smile:

The first block of code I pasted above counts the times the magnet hits the sensor and shows the count in the serial window.

I see where I left that out at the top, with all my attempts I must have deleted it. I have it a little farther down to keep checking the value but I will put it up above.

Sorry, but I have no idea what that means.

In the code you posted you read the value of the sensor into hallval then do nothing with it. Elsewhere in the code you use hallcurrentState and check whether its value has changed but is not changed in the code.

I used low because the initial code used low and it worked so I didn't want to mess with it. Would false make it run any differently than low?

Give me an example of how you count pretty girls walking by.

0, 1, 2, 3, ...

or

LOW, true, INPUT_PULLUP, 3, ...

If you are counting things, none means 0, not LOW, not false.

UKHeliBob:
Sorry, but I have no idea what that means.

In the code you posted you read the value of the sensor into hallval then do nothing with it. Elsewhere in the code you use hallcurrentState and check whether its value has changed but is not changed in the code.

I think I get your meaning, I need to read the sensor and then initialize the count?

Like this,

hallval = digitalRead(positionSensor);
  if (hallval == LOW) {
    hallcurrentState = 1;
  }

PaulS:
Give me an example of how you count pretty girls walking by.

0, 1, 2, 3, ...

or

LOW, true, INPUT_PULLUP, 3, ...

If you are counting things, none means 0, not LOW, not false.

I follow, I'm thinking button on off instead of counting.

I think I get your meaning, I need to read the sensor and then initialize the count?

That is not what I meant.

You should read the sensor and compare its state with the previous reading
If it has changed and is now LOW add 1 to the count

As it is now you read the sensor into hallval, ignore the reading then check to see whether another variable, hallcurrentState , that never changes has changed.

I follow, I'm thinking button on off instead of counting.

Then why is the variable called hallcounter?

PaulS:
Then why is the variable called hallcounter?

I meant that I shouldn’t be thinking on off.

I should be thinking counting and thats why it is hallcounter.

UKHeliBob:
That is not what I meant.

You should read the sensor and compare its state with the previous reading
If it has changed and is now LOW add 1 to the count

As it is now you read the sensor into hallval, ignore the reading then check to see whether another variable, hallcurrentState , that never changes has changed.

I'm reading the sensor to establish the hallcurrentState as I understand the counting code that I found on the web. I realize I don't use it anywhere else but the code doesn't count without using the code below which I removed by mistake from my original attempt when I was trying everything I could think of to make it work.

hallval = digitalRead(positionSensor);
  if (hallval == LOW) {
    hallcurrentState = 0;
  }
  else {
    hallcurrentState = 1;
  }

I don't see why you need 2 variables

Is not hallcurrentState always the same as hallval ?

I should be thinking counting and thats why it is hallcounter.

So, counting does not involve HIGH, LOW, true or false. Counting starts at ZERO. There's even a literal value that you can use to express that.

ribbonman:
Now I see what you mean, I have modified the code to remove the extra variable.

Thank you.