英文:
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("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 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("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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论