Programming step motor

I have a humidity sensor, which when reaches x humidity has to open a hatch, and when the humidity falls below x again has to close the hatch again. I dont know which statement to use in this scenario.


if it is in a loop, wouldnt that just make the step motor spin every time the code is looped?

It depends on how you use it.

The best way to learn programming is to try stuff.

If you haven't got the motors yet, just use prints to say "I've switched the motor on" or "I've switched the motor off"

Stepper Motor Basics

Simple Stepper Code


Something like this - you need to remember the state of the hatch, and to implement functions to open and close the hatch and read the humidity sensor:

bool hatch_is_open = false ;   // represent the state
float setpoint = 50.0  // target humidity

#define delta 2.0   // need some hysteresis to prevent over-rapid on-off toggling
void loop ()
  if (read_humidity () > setpoint +delta && !hatch_is_open)
    open_hatch () ;
  else if (read_humidity () < setpoint - delta && hatch_is_open)
    close_hatch () ;