c++ 如何同时使用步进电机和LED?

9lowa7mx  于 2023-03-25  发布在  其他
关注(0)|答案(1)|浏览(93)

我试图使步进电机击中一个限位开关,然后点亮6个LED。个别这些都工作得很好,但当执行所有在同一时间只有电机的工作和LED只会 Flink 一次,当我甚至没有接触到开关。任何想法,为什么会发生这种情况?
下面是代码:

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

// LED PRE-Setup

int LED[6] = { 2, 5, 4, 6, 12, 13 };

int brightness = 0;
int fadeamount = 5;

// Constants
const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
const int blinktime = 500;
int LED_2Interval = 500;
int StepsRequired;
int ledState = LOW;

// Stepper Motor Pre-Setup
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

// Switch Pre-Setup
int limitSwitchPin = 7;
// Previous Variables
unsigned long previousLED_2Millis = 0;
unsigned long currentMillis = 0;

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

void setup()
{
    // put your setup code here, to run once:
    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);

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

void loop()
{
    // put your main code here, to run repeatedly:
    //clockWise();
    updateLEDS();
    switchLEDS();
}

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

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()
//Read the current state of the limit switch to turn the LEDS on or off.
{
    if (digitalRead(limitSwitchPin) == HIGH) {
        ledState = HIGH;
       /* analogWrite(LED, brightness);
        brightness = brightness + fadeamount; ----- Work in progress for a fade feature 
        for (int i = 0; i < 225; i++)

        -fadeamount; */
        Serial.println("LED ON");
    }
    else {
        ledState = LOW;
        Serial.println("LED OFF");
        delay(100);
    }

    if (digitalRead(limitSwitchPin) == HIGH)
        analogWrite(LED, brightness);
    brightness = brightness + fadeamount;
    for (int i = 0; i < 225; i++)
        ;
    -fadeamount;
};
void switchLEDS()
//Turns the LEDS on and off
{

    digitalWrite(LED[0], ledState);
    digitalWrite(LED[1], ledState);
    digitalWrite(LED[2], ledState);
    digitalWrite(LED[3], ledState);
    digitalWrite(LED[4], ledState);
    digitalWrite(LED[5], ledState);
};

最初,这是一个更短的代码,但我在另一篇文章中读到,如果我把它分成两个单独的函数“clockWise”和“updateLEDS”,这两个代码可以同时运行,但这并不起作用。

dgtucam1

dgtucam11#

问题可能在于更新LED的方式()函数,检查限位开关状态的if语句重复两次,模拟Write(LED,brightness)和brightness for loop没有包含在第一个if语句中,因此LED可能会在没有任何延迟的情况下重复打开和关闭。不清楚为什么使用带有空语句的for循环并通过fadeamount递减亮度。
下面是updateLEDS()函数的修改版本:

void updateLEDS() {
  if (digitalRead(limitSwitchPin) == HIGH) {
    ledState = HIGH;
    analogWrite(LED[0], brightness);
    analogWrite(LED[1], brightness);
    analogWrite(LED[2], brightness);
    analogWrite(LED[3], brightness);
    analogWrite(LED[4], brightness);
    analogWrite(LED[5], brightness);
    brightness = brightness + fadeamount;
    if (brightness >= 255 || brightness <= 0) {
      fadeamount = -fadeamount;
    }
  } else {
    ledState = LOW;
    digitalWrite(LED[0], ledState);
    digitalWrite(LED[1], ledState);
    digitalWrite(LED[2], ledState);
    digitalWrite(LED[3], ledState);
    digitalWrite(LED[4], ledState);
    digitalWrite(LED[5], ledState);
    brightness = 0;
    fadeamount = 5;
  }
}

当按下限位开关时,所有LED都将以相同的亮度打开,并逐渐增加/降低LED的亮度。当松开限位开关时,所有LED都将立即关闭。您可以调整fadeamount变量以更改亮度变化的速率。
此外,确保在updateLEDS()函数的if语句中调用switchLEDS()函数,以根据限位开关的状态打开/关闭LED。

相关问题