如何修复我的LED不响应限位开关,并阻止它循环执行if else语句?

huangapple go评论80阅读模式
英文:

How to fix my LEDS not responding to the limit switch and stop it from cycling through the if else statement?

问题

I am making a script for an arduino board that will use a stepper motor to hit a limit switch which will then light up some LEDS (right now in only have 1 LED set), the code seems sound to me, it should be lighting up when the limit switch is pressed and then turn off when it is released but it is not doing this at all? Also, its reading my if else statements like normal lines of code and goes through all without looking at the conditions. Could someone read over my code and spot what I've done wrong?

Here is the code:

void updateLEDS() {
limitSwitchPin.loop();
if(limitSwitchPin.getState() == HIGH) {
 if(limitSwitchPin.isPressed()); 
  digitalWrite(LED_2, HIGH);
  Serial.println("LED 1");
  delay(500);
 }
else(limitSwitchPin.getState() == LOW); {
  if(limitSwitchPin.isReleased())
   digitalWrite(LED_2, LOW);
   Serial.println("led low 2");
  delay(500);
}

Entire Code:

//Libaries
#include <ezButton.h>
#include <Stepper.h>

//Constants
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 Motor Pre-Setup
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

//LED PRE-Setup
int LED_2 = 5;

//Switch Pre-Setup
ezButton limitSwitchPin(7);

//Previous Variables 
unsigned long previousLED_2Millis = 0;
unsigned long  currentMillis = 0;

//==========================

void setup() {
  // put your setup code here, to run once:
int currentMillis = millis();
int ledState = digitalRead(LED_2);
pinMode(LED_2, OUTPUT);
pinMode(7, INPUT);
limitSwitchPin.setDebounceTime(100);

Serial.begin(9600);
}
//==========================

void loop() {
  // put your main code here, to run repeatedly:

//clockWise();
updateLEDS();
}

//==========================

void clockWise() {
StepsRequired = - STEPS_PER_OUT_REV / 2;
steppermotor.setSpeed(1000);
steppermotor.step(StepsRequired);
Serial.println("clockWise 4");
};

void updateLEDS() {
limitSwitchPin.loop();
if(limitSwitchPin.getState() == HIGH) {
 if(limitSwitchPin.isPressed()); 
  digitalWrite(LED_2, HIGH);
  Serial.println("LED 1");
  delay(500);
 }
else(limitSwitchPin.getState() == LOW); {
  if(limitSwitchPin.isReleased())
   digitalWrite(LED_2, LOW);
   Serial.println("led low 2");
  delay(500);
}
//}
};

I have tried everything in my power as a novice coder, changing the code around seeing if it would work and adding a delay so it could maybe fix the fact that it's skipping over the if-else conditions, it didn't help.

英文:

I am making a script for an arduino board that will use a stepper motor to hit a limit switch which will then light up some LEDS (right now in only have 1 LED set), the code seems sound to me, it should be lighting up when the limit switch is pressed and then turn off when it is released but it is not doing this at all? Also, its reading my if else statements like normal lines of code and goes through all without looking at the conditions. Could someone read over my code and spot what I've done wrong?

Here is the code:

void updateLEDS() {
limitSwitchPin.loop();
if(limitSwitchPin.getState() == HIGH) {
 if(limitSwitchPin.isPressed()); 
  digitalWrite(LED_2, HIGH);
  Serial.println(&quot;LED 1&quot;);
  delay(500);
 }
else(limitSwitchPin.getState() == LOW); {
  if(limitSwitchPin.isReleased())
   digitalWrite(LED_2, LOW);
   Serial.println(&quot;led low 2&quot;);
  delay(500);

Entire Code:

//Libaries
#include &lt;ezButton.h&gt;
#include &lt;Stepper.h&gt;

//Constants
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 Motor Pre-Setup
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

//LED PRE-Setup
int LED_2 = 5;

//Switch Pre-Setup
ezButton limitSwitchPin(7);



//Previous Variables 
unsigned long previousLED_2Millis = 0;
unsigned long  currentMillis = 0;

//==========================

void setup() {
  // put your setup code here, to run once:
int currentMillis = millis();
int ledState = digitalRead(LED_2);
pinMode(LED_2, OUTPUT);
pinMode(7, INPUT);
limitSwitchPin.setDebounceTime(100);

Serial.begin(9600);
}
//==========================

void loop() {
  // put your main code here, to run repeatedly:

//clockWise();
updateLEDS();
}

//==========================

void clockWise() {
StepsRequired = - STEPS_PER_OUT_REV / 2;
steppermotor.setSpeed(1000);
steppermotor.step(StepsRequired);
Serial.println(&quot;clockWise 4&quot;);
};


void updateLEDS() {
limitSwitchPin.loop();
if(limitSwitchPin.getState() == HIGH) {
 if(limitSwitchPin.isPressed()); 
  digitalWrite(LED_2, HIGH);
  Serial.println(&quot;LED 1&quot;);
  delay(500);
 }
else(limitSwitchPin.getState() == LOW); {
  if(limitSwitchPin.isReleased())
   digitalWrite(LED_2, LOW);
   Serial.println(&quot;led low 2&quot;);
  delay(500);
}
//}
};

I have tried everything in my powder as a novice coder, changing the code around seeing if it would work and adding a delay so it could maybe fix the fact that its skipping over the if else conditions, it dint help.

答案1

得分: 1

I don't know what your intention is, but the indentation does not appear to match the braces. In general it is good practice to always use {} and proper indentation for if and else statements, even if it is not necessary. This is to avoid issues later.

Also: don't use if (condition) ; {...}, because the code in braces will always be executed. You must leave out the ;.

Finally: an if (input == HIGH) does not need an else if (input == LOW), just 'else' will do (ignoring the fact that you apparently forgot the second if).

So I guess your code could be:

void updateLEDS() {
    limitSwitchPin.loop();
    if(limitSwitchPin.getState() == HIGH) {
        if(limitSwitchPin.isPressed()) {
            digitalWrite(LED_2, HIGH);
            Serial.println("LED 1");
            delay(500);
        }
    } else {
        if(limitSwitchPin.isReleased()) {
            digitalWrite(LED_2, LOW);
            Serial.println("led low 2");
            delay(500);
        }
    }
}

Again, not sure if those are your intentions, but formatting it this way should help you solve your problem.

I am ignoring some other issues, like the .getState(), .isPressed(), and isReleased methods that appear to be duplicated, but the presence of the delay(500) suggests that this duplication was intended to debounce the switches before the code got mangled...

英文:

I don't know what your intention is, but the indentation does not appear to match the braces.
In general it is good practice to always use {} and proper indentation for if and else statements, even if it is not necessary.
This is to avoid issues later.

Also: don't use if (condition) ; {...}, because the code in braces will always be executed. You must leave out the ;.

Finally: an if (input == HIGH) does not need an else if (input == LOW), just 'else' will do (ignoring the fact that you apparently forgot the second if).

So I guess your code could be:

void updateLEDS() {
    limitSwitchPin.loop();
    if(limitSwitchPin.getState() == HIGH) {
        if(limitSwitchPin.isPressed()) {
            digitalWrite(LED_2, HIGH);
            Serial.println(&quot;LED 1&quot;);
            delay(500);
        }
    } else {
        if(limitSwitchPin.isReleased()) {
            digitalWrite(LED_2, LOW);
            Serial.println(&quot;led low 2&quot;);
            delay(500);
        }
    }
}

Again, not sure if those are your intentions, but formatting it this way should help you solve your problem.

I am ignoring some other issues, like the .getState(), .isPressed(), and isReleased methods that appear to be duplicated, but the presence of the delay(500) suggests that this duplication was intended to debounce the switches before the code got mangled...

huangapple
  • 本文由 发表于 2023年3月15日 20:02:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定