IF statement problem

I am trying to latch a limit switch with using bounce based on code below: The limit switch signal is not recognized by or the if statement is wrong.

Any recommendations?

void loop() {

digitalWrite(DIRR,LOW); digitalWrite(ENAR,HIGH); digitalWrite(PULR,HIGH); delayMicroseconds(50); // pulse length, leave fixed digitalWrite(PULR,LOW);

if (SWITCH_PIN2== LOW) { // Toggle stepper state : stepperState = !stepperState; digitalWrite(DIR,stepperState); delay(50); } digitalWrite(ENA,HIGH); digitalWrite(PUL,HIGH); delayMicroseconds(50); // pulse length, leave fixed digitalWrite(PUL,LOW); delayMicroseconds(200); // interval between pulses, controls speed

}

if (SWITCH_PIN2== LOW)In the absence of complete code (hint) I'm going to guess that that pin number is non-zero.

Here is the entire code

Stepper_with_micro_March21.ino (1.05 KB)

I’d love to help you but I’m posting from my phone - I can’t see your code.

how's this?

define SWITCH_PIN2 2

define SWITCH_PIN3 3

define PUL 7

define DIR 6

define ENA 5

define PULR 10

define DIRR 9

define ENAR 8

boolean stepperState =LOW;

void setup() {

// Setup the button with an internal pull-up : pinMode(SWITCH_PIN2,INPUT_PULLUP); pinMode(SWITCH_PIN3,INPUT_PULLUP);

pinMode (PUL, OUTPUT); pinMode (ENA, OUTPUT); pinMode (DIR, OUTPUT);

pinMode (PULR, OUTPUT); pinMode (ENAR, OUTPUT); pinMode (DIRR, OUTPUT);

digitalWrite(DIR,stepperState); }

void loop() {

digitalWrite(DIRR,LOW); digitalWrite(ENAR,HIGH); digitalWrite(PULR,HIGH); delayMicroseconds(50); // pulse length, leave fixed digitalWrite(PULR,LOW);

if (SWITCH_PIN2== LOW) { // Toggle stepper state : stepperState = !stepperState; digitalWrite(DIR,stepperState); delay(50); } digitalWrite(ENA,HIGH); digitalWrite(PUL,HIGH); delayMicroseconds(50); // pulse length, leave fixed digitalWrite(PUL,LOW); delayMicroseconds(200); // interval between pulses, controls speed

}

Lacking code tags, but as I predicted #define SWITCH_PIN2 2

How is 2 ever going to equal LOW (aka zero)?

Read the pin

thanks

If you look at the button example which Arduino offer with the IDE you can see how you read the state of the pin easily.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

KawasakiZx10r: If you look at the button example which Arduino offer with the IDE you can see how you read the state of the pin easily.

You could also question why buttonState has global scope, and why buttonPin and ledPin need sixteen signed bits.

Or even why the loop function isn't simply writtendigitalWrite(ledPin, digitalRead(buttonPin));

It is written that way because it is an example that is intended to be extended. The one-line version is closed off to further experiments.

MorganS: It is written that way because it is an example that is intended to be extended. The one-line version is closed off to further experiments.

digitalWrite(ledPin, 1 - digitalRead(buttonPin));

8)