I want void loop to go through once

Hi all, I am currently in a project where I am having a limit switch activate code for a DC motor. Once the limit switch is activated, I want the code to go through a sequence once. The problem I'm having is for my project, my limit switch will be permanently pressed until I physically lift what is triggering the limit switch. Any thoughts on how to have the code in the void loop () occur once until the state of the limit switch is changed, where then the loop will reset?

Do You mind posting the code?
You can put a dead stop any ware You want
.

while(1);

so you want to detect when the limit switch GETS activated (state change), not when it IS activated.

a boolean could also be used to remember that you've done. the sequence and should not do it again

#define RPWM 3
#define LPWM 6
#define REN 4
#define LEN 7

const int buttonPin = 12;

int out1;
int out2;

void setup() {
  pinMode(buttonPin, INPUT);

  pinMode(RPWM, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(LEN, OUTPUT);
  pinMode(REN, OUTPUT);
  digitalWrite(REN, HIGH);
  digitalWrite(LEN, HIGH);

}


void loop() {



  while (digitalRead(12) == HIGH)
  {
    delay(500);
    digitalWrite(LEN, HIGH);
    digitalWrite(LPWM, HIGH);
    delay(4000);
    digitalWrite(LEN, LOW);
    digitalWrite(LPWM, LOW);
    delay(500);
    digitalWrite(13, HIGH);

    break;

  }

  if (digitalRead(12) == LOW) {
    return;

  }
}

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)


do you have an external pull-up or pull-down on the button?

Sorry for the janky code too I've been constantly changing the code to try to troubleshoot the issue.

do you have an external pull-up or pull-down on the button?

can you clarify why you have a while but with a break as the last line? that's basically a if()

So the intention for my code is for when the limit switched is pressed, the motor will spin for a set span of time. Because the limit switched will be continuously pressed, I am having a problem where the motor will spin pretty much infinitely. I just need the motor to spin for that set span of time, then stay off until the switch is pressed again. I hope I am conveying this clearly please feel free to let me know if I need to explain anything further. Thank you

I originally had only "if" statements but I switched to "while" statements in an attempt to try to solve the problem.

do you have an external pull-up or pull-down on the button? (third time...)

what are LEN and REN ? (left and right enable?)
share a schema of your setup

How is your switch wired? You configured the pin as INPUT and the way you have it coded implies you have a pull-down resistor. Is that correct?

Assuming HIGH means the switch is activated then you need to look for a LOW to HIGH change and not the state of the input.

yes

Yes to what...

come on, it's a pain if we need to extract every piece of information from you...

  • post a clear schema (hand drawn is OK) of your set up
  • explain what's connected where
  • explain what gets the button to be pressed and unpressed

its a pull-down on the button. The LEN and REN are my enable pins for the motor (im hooked up to a BTS7960 driver)

so something like this then (with an active wait) ?

// BTS7960  pins
const byte  RPWM = 3;
const byte  LPWM = 6;
const byte  REN  = 4;
const byte  LEN  = 7;

// Limit Switch
const byte buttonPin = 12;

void setup() {
  pinMode(buttonPin, INPUT); // external pull down
  pinMode(RPWM, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(LEN, OUTPUT);
  pinMode(REN, OUTPUT);
  digitalWrite(REN, HIGH); 
  digitalWrite(LEN, HIGH); 
}

void loop() {
  // wait for the limit switch to be activated
  while (digitalRead(12) == LOW); 

  // do your stuff once
  delay(500);
  digitalWrite(LEN, HIGH);
  digitalWrite(LPWM, HIGH);
  delay(4000);
  digitalWrite(LEN, LOW);
  digitalWrite(LPWM, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  
// wait for the limit switch to be reset
  while (digitalRead(12) == HIGH); 
  delay(20); // poor's man anti-bounce
}

you don't use REN?
if you disable LEN, do you need to set the PWM to 0 too ?
13 was not set as an output, yet you do digitalWrite(13, HIGH);... that's not a good idea (activates the pull-up? ). is that just the LED built in or do you have something connected there?

REN isn’t necessary if I use LEN. I just had it there just in case I wanted to switch the direction of my motor.

As long as the enable pin (LEN) is disabled the motor is unable to spin.

Pin 13 is an output to an LED.

Once again I’m sorry for the jankyness of this code and everything going on inside of this I’m sort of new to coding to this extent

Also what you have written in the code is exactly what I was looking for

It seems one could do that with switch case or with an if()

so add pinMode(ledPin, OUTPUT); to the setup and define const byte ledPin = 13; as a global constant

as you've defined a constant for the pin 12, use it ➜ digitalRead(buttonPin)

I'm not sure about how you get the motor moving (no PWM set in the setup) or after you put LEN to low but I'll let you figure this out

Thank you this pointed me in a much better direction than what I would’ve ended up doing