Hall Effect Sensor Homing

Hello everyone,

I hate to ask for help but here it goes…
I am fairly new with Arduino. I dedicate all of my free time to learning. However, my current project is giving me trouble.

So below is the code I have. I must have re-written it 50 times and decided it’s no longer practical to keep struggling on my own. With a lack of related information online, I am reaching out to you.

I am using a 28byj-48 stepper motor to spin a production line infeed wheel. I have mounted a small magnet on the underside of the wheel to enter the detection field of the hall sensor. The wheel is only 5" in diameter but needs to stop in a precise location. I have the sensor working correctly and the motor working correctly as individual circuits, but when I try and combine them in one code the sensor reading doesn’t change. I think the problem is somewhere in my setup (homing) section. I have tried multiple variations of “while” and “if” statements and this is the closest I can get. Any help would be extremely appreciated! Thanks

#include <Stepper.h>
const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
int StepsRequired;
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
int hallSensorPin = 2; //1 off 0 on        
int sensorState = 0;

void setup() {
  Serial.begin(9600);
  pinMode(hallSensorPin, INPUT); //hall

sensorState = digitalRead(hallSensorPin);
   while (sensorState == HIGH) {
    Serial.print(sensorState);
  StepsRequired = 1;
  steppermotor.setSpeed(200);
  steppermotor.step(StepsRequired);
    }

  StepsRequired = 0;
}

void loop() {
Serial.print(sensorState);
  StepsRequired = 256;
  steppermotor.setSpeed(300);
  steppermotor.step(StepsRequired);
  delay(3000);
}

sensorState never change because you don't update it in loop().
Btw, for your system to be responsive, don't use delay(), use millis() to keep track of passed time.

 sensorState = digitalRead(hallSensorPin);
  while (sensorState == HIGH)
  {
    Serial.print(sensorState);
    StepsRequired = 1;
    steppermotor.setSpeed(200);
    steppermotor.step(StepsRequired);
  }

sensorState never changes when in this while loop so the while loop will never exit. Read the state of the sensor inside the loop

Wow! Works like a charm! Thank you both for taking the time to respond. I ran that scenario over and over in my head and should have known. I have experimented a little with the millis() function but kind of got intimidated by the setup so for now I stuck with the delay. Is there a simple base code or setup that you use to keep track of time? Thanks again and much appreciated!

Is there a simple base code or setup that you use to keep track of time?

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Perfect! Will do thanks again!

UKHeliBob that article was very very helpful! It made perfect sense of an otherwise confusing subject. I was able to replace all of my delay() functions with this new method. Thank you!

I just have one question...
When my 28BYJ-48 is in the off, or what would be called "delay" period, it hums and the driver LED's are illuminated. Is this normal?
I have all of the motor pins set to LOW and/or 0 stepsRequired. Other than that I'm done! Sorry to be a pain.

void loop() {
  digitalWrite(2, HIGH); //GREEN ON SOLID
  lcd.print("RUNNING         ");
   
  currentMillis = millis();  
  if (currentMillis - startMillis >= period) 
  {
    StepsRequired = 256;
  steppermotor.setSpeed(300);
  steppermotor.step(StepsRequired);
    startMillis = currentMillis; 
  }
  else {digitalWrite (8, LOW);
  digitalWrite (9, LOW);
  digitalWrite (10, LOW);
  digitalWrite (11, LOW);}
}

When my 28BYJ-48 is in the off, or what would be called "delay" period, it hums and the driver LED's are illuminated. Is this normal?

Sorry, but I know little or nothing about stepper motors and controlling them