LEDs having erratic behaviour despite simple code

Hey everyone, so my boss told me to pick a task to do and complete it so the current goal is to - Have a stepper motor hit an limit switch and that limit switch to light up 6 LEDs and then to turn off when the switch is released.

This works, for about 3-4 clicks then the LEDS have some random behaviour, sometimes they will flash really quickly, not turn on at all or will light up and stay lighted up until the switch is pressed again. Could anyone show me what i've done wrong so I can make this code more stable? That would be amazing. Here is my code:

#include <Stepper.h>

// Stepper Settings
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);

//LED Settings
int LED[6] = { 3, 4, 5, 6, 7, 12 };
volatile bool ledState = false;

//LIMIT SWITCH
volatile int limitSwitchPin = 2;


void setup()
{
    pinMode(LED[0], OUTPUT);
    pinMode(LED[1], OUTPUT);
    pinMode(LED[2], OUTPUT);
    pinMode(LED[3], OUTPUT);
    pinMode(LED[4], OUTPUT);
    pinMode(LED[5], OUTPUT);
    // pinMode(ledState, OUTPUT);
    pinMode(limitSwitchPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(2), updateLEDS, FALLING);
    Serial.begin(9600);
}

void loop()
{
    clockWise();
    //updateLEDS();
}

void clockWise()
//Turn the stepper motor to hit the limit switch
{
    StepsRequired = -STEPS_PER_OUT_REV / 2;
    steppermotor.setSpeed(1000);
    steppermotor.step(StepsRequired);
    //Serial.println("clockWise");
}

void updateLEDS()
{
    if (digitalRead(limitSwitchPin) == LOW) {
        ledState = !ledState;
        digitalWrite(LED[0], ledState);
        digitalWrite(LED[1], ledState);
        digitalWrite(LED[2], ledState);
        digitalWrite(LED[3], ledState);
        digitalWrite(LED[4], ledState);
        digitalWrite(LED[5], ledState);
    }
}
   /* else {

        if (digitalRead(limitSwitchPin) == LOW) {
            ledState = !ledState;
            digitalWrite(LED[0], ledState);
            digitalWrite(LED[1], ledState);
            digitalWrite(LED[2], ledState);
            digitalWrite(LED[3], ledState);
            digitalWrite(LED[4], ledState);
            digitalWrite(LED[5], ledState);
        }
    } */
}

You may wonder, "Why is he using interupts for such a simple task?" Well, for some reason the stepper motor blocks the code of anything else that is happening in the background so I had no choice other than to use an interrupt.

Sorry if any of this code looks really bad or the answer is simple, this is my first time coding and everything I have learnt is from trying to make this project.

Why is there an else with the same condition in the ISR?

Have you considered a for loop?

After a falling edge interrupt, what do you think the state of the pin will be?

Have you considered bounce?

After a quick look, I can see that your last else block is outside your ISR function. The bracket count is not good also.

Btw you can run through your array with a for loop, or even put all 6 Leds on the same digital output to avoid using too much output (only if all 6 leds are always at the same state).

Sorry that should have been commented out, I just tried that to see if anything would change, No as I dont know exactly how id put this code in a for loop aha.

My objective was to:
**MAKE A STEPPER MOTOR HIT A LIMIT SWITCH THAT POWERS 6 LEDS THAT TURN ON AND TURNS OFF WHEN THE SWITCH IS RELEASED **

So, this is my first every project that I've been set on and all the code. The motor spins and the switch works but the LEDS will only work for a couple of presses and then they will only flicker or they would stay lit up until the switch has been pressed again.

I've tried a few different things like adding an 'else' onto my 'if' statement and I tried compressing all of my LED arrays into one column so it'd make the interrupt shorter but then it messes up the brightness.

Here is the code:

#include <Stepper.h>

// Stepper Settings
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);

//LED Settings
int LED[6] = { 3, 4, 5, 6, 7, 12 };
volatile bool ledState = false;

//LIMIT SWITCH
volatile int limitSwitchPin = 2;

void setup()
{
    pinMode(LED[0], OUTPUT);
    pinMode(LED[1], OUTPUT);
    pinMode(LED[2], OUTPUT);
    pinMode(LED[3], OUTPUT);
    pinMode(LED[4], OUTPUT);
    pinMode(LED[5], OUTPUT);
    // pinMode(ledState, OUTPUT);
    pinMode(limitSwitchPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(2), updateLEDS, FALLING);
    // Serial.begin(9600);
}

void loop()
{
    clockWise();
    //updateLEDS();
}

void clockWise()
//Turn the stepper motor to hit the limit switch
{
    StepsRequired = -STEPS_PER_OUT_REV / 2;
    steppermotor.setSpeed(1000);
    steppermotor.step(StepsRequired);
    //Serial.println("clockWise");
}

void updateLEDS()
{
    if (digitalRead(limitSwitchPin) == LOW) {
        ledState = !ledState;
        digitalWrite(LED[0], ledState);
        digitalWrite(LED[1], ledState);
        digitalWrite(LED[2], ledState);
        digitalWrite(LED[3], ledState);
        digitalWrite(LED[4], ledState);
        digitalWrite(LED[5], ledState);
        delay(500);
    }
}

Why do you have a delay in the ISR?
Why not a for loop?
What about switch bounce?

(I thought I'd just responded to a very similar topic just a while ago)

In general, you should not use magic numbers. The I/O pins love to have a functional name.

1- I was trying to see if it would debounce the switch.
2- I dont know how i'd construct a for loop for this to be honest aha
3-How would I bounce the switch without using a delay? Millis?

Strange coincidence aha

Sorry but what're magic numbers ahaha, new to all this.

They're a bit like the digits at the end of your username.
Is it 04 or 06?

1 Like

Number 0 to number 5 and others are so called magic numbers.

I thought this is how you call back the pins in my LEDS array? Like, pinMode[0], OUTPUT); would call back pin 3 for my LED?

This guy is using multiple user names!
kaden04 kaden06 quite obvious

Time to call police moderator ?

Why are you Scamming Forum policy and confusing all of us

Duplicate topics merged.

The duplicate account @kaden06 permanently suspended due to being used in a harmful manner.

@kaden04 has been given a temporary suspension for lying about the duplicate accounts.

That's great

You can try to use the AccelStepper.h library , it does not block the execution of the code, you could give it a go.

In his world, the led pins have to be named. And if they each had a different function, the array name alone would be less clear and somewhere there is a HOLY STANDARD for the Robed Ones to observe...

But in fact when all the leds do the same thing, pin number names clarify nothing at all and force more work on the coder, an exercise in uselessness when someone else has to track down what doesn't matter over opinions far outside of the lesson at hand.

I smell a manager.