Limit switches and prioritization of certain inputs

Hi!

I’m new to arduino and would like some help with my current project.

Me and some friends are building a solartracker that will optically track the sun and right now i think i have taken on a bit more than i can chew.

I do not know how to do the following thing. We have installed a couple of buttons that is intended to work as limit switches. Is there away to program the Arduino to always “watch” and prioritize their input over everything else? Even if the tracking data tells the motor to keep spinning i want the limit switch to override this. With my current code the motor keeps spinning even when the limit switches are hit.

This code does not involve the tracking but the concept should be valid anyways?

#include <Stepper.h>

int SPMU = 32;
int rightbutton = 10;
int leftbutton = 11;
int rightval = 0;
int leftval = 0;
Stepper myStepper(SPMU, 2,3,4,5);

 

void setup()

{

myStepper.setSpeed(500);
pinMode(rightbutton, INPUT);
pinMode(leftbutton, INPUT);
Serial.begin(9600);

}

void loop() {

rightval = digitalRead(rightbutton);
leftval = digitalRead(leftbutton);


if (rightval == HIGH) { //if the button is depressed the motor should reverse it's direction.
   myStepper.step(20480);
  }
else if (leftval == HIGH) {
  myStepper.step(-20480); //if the button is depressed the motor should reverse it's direction.
  }

else 
{
  myStepper.step(20480); //If none of the buttons are pushed proceed as usual.
}}

From the documentation

Stepper: step(steps)

Description

Turns the motor a specific number of steps, at a speed determined by the most recent call to setSpeed(). This function is blocking; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch. For example, if you set the speed to, say, 1 RPM and called step(100) on a 100-step motor, this function would take a full minute to run. For better control, keep the speed high and only go a few steps with each call to step().

So no you can’t take that approach. You need to take small steps and loop to check the buttons again

You were right, a bit stupid of me to not realize that.

Thanks!