打开二极管一段时间,以及在Arduino中执行下一个程序。

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

Switching on the diode for some time, as well as execution of the next program in Arduino

问题

在tinkercad中制作的电路
我使用继电器,因为我只有4根导线连接到LED二极管和这两个开关。

int led = 12; // 红色LED
int s1 = 9; // 开关1
int s2 = 10; // 开关2
int k1 = 3; // 第一个蓝色LED
int k2 = 2; // 第二个蓝色LED
int y1 = 11; // 继电器
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
const int led1Duration = 6000; // 第一个蓝色LED持续时间
const int led2Duration = 12000; // 第二个蓝色LED持续时间

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(k1, OUTPUT);
  pinMode(k2, OUTPUT);
  pinMode(y1, OUTPUT);
  pinMode(s1, INPUT);
  pinMode(s2, INPUT);
}

void loop()
{
  if (digitalRead(s1) == HIGH && digitalRead(s2) == LOW)
  {	
    digitalWrite(k1, HIGH);
    digitalWrite(y1, HIGH);
    startTime1 = millis(); 
  }
  else
  {
    digitalWrite(led, LOW);
  }
  
  if (digitalRead(s2) == HIGH && digitalRead(s1) == LOW)
  {
    digitalWrite(k2, HIGH);
    digitalWrite(y1, HIGH);
    startTime2 = millis(); 
  }
  else
  {
    digitalWrite(led, LOW);
  }
  
  if (digitalRead(k1) == HIGH && (millis() - startTime1 >= led1Duration)) 
  {
    digitalWrite(k1, LOW);
    digitalWrite(y1, LOW);
  }
  if (digitalRead(k2) == HIGH && (millis() - startTime2 >= led2Duration)) 
  {
    digitalWrite(k2, LOW);
    digitalWrite(y1, LOW);
  }
  
  if (digitalRead(k1) == HIGH && digitalRead(k2) == LOW)
  {
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
  }
  if (digitalRead(k2) == HIGH && digitalRead(k1) == LOW)
  {
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
  }
  if (digitalRead(k2) == HIGH && digitalRead(k1) == HIGH)
  {
    digitalWrite(led, HIGH);
  }
}

是否有更简单的方法来做到这一点,因为当我组装它时,开关的响应非常慢,蓝色LED会在一段时间后才会亮起。接下来,我使用了ESP8266,但是3V的继电器无法工作。我该如何使其正常工作?

英文:

The circuit made in tinkercad
I use the relay, because I have only 4 wires to led diode and this two switches.

int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) == HIGH and digitalRead(s2) == LOW)
{	
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis(); 
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(s2) == HIGH and digitalRead(s1) == LOW)
{
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis(); 
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(k1) == HIGH && (millis() - startTime1 >= led1Duration)) 
{
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k2) == HIGH && (millis() - startTime2 >= led2Duration)) 
{
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k1) == HIGH and digitalRead(k2) == LOW)
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == LOW)
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == HIGH)
{
digitalWrite(led, HIGH);
}
}

Is there easier way to do that, cause when I assembled it the switches work very slow that blue led turn on after some time. Next I use the esp2866 but the relay for 3V doesn't turn on. How can I make it to work?

答案1

得分: 0

@Peter 是真的:避免阻塞延迟代码。

这里有一个例子:我稍微简化了你的代码,并添加了else if以使其更可读

更新1

    int led = 12; // 红色LED
    int s1 = 9; // 开关1
    int s2 = 10; // 开关2
    int k1 = 3; // 第一个蓝色LED
    int k2 = 2; // 第二个蓝色LED
    int y1 = 11; // 继电器
    unsigned long startTime1 = 0;
    unsigned long startTime2 = 0;
    unsigned long t200 = 200;
    unsigned long t500 = 500;
    const int led1Duration = 6000; // 第一个蓝色LED亮起时间
    const int led2Duration = 12000; // 第二个蓝色LED亮起时间

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(k1, OUTPUT);
  pinMode(k2, OUTPUT);
  pinMode(y1, OUTPUT);
  pinMode(s1, INPUT);
  pinMode(s2, INPUT);
}

void loop()
{
  if (digitalRead(s1) && !digitalRead(s2)) {
    digitalWrite(k1, HIGH);
    digitalWrite(y1, HIGH);
    startTime1 = millis();
    blinkLed200();
  } else if (!digitalRead(s1) && digitalRead(s2)) {
    digitalWrite(k2, HIGH);
    digitalWrite(y1, HIGH);
    startTime2 = millis();
    blinkLed500();
  }

  if (digitalRead(k1) && (millis() - startTime1 >= led1Duration)) {
    digitalWrite(k1, LOW);
    digitalWrite(y1, LOW);
    digitalWrite(led, LOW);
  }

  if (digitalRead(k2) && (millis() - startTime2 >= led2Duration)) {
    digitalWrite(k2, LOW);
    digitalWrite(y1, LOW);
    digitalWrite(led, LOW);
  }
}

void blinkLed200() {
  if (millis() - t200 > 200) { //每200毫秒闪烁一次
    t200 = millis();
    digitalWrite(led, !digitalRead(led));
  }
}

void blinkLed500() {
  if (millis() - t500 > 500) { //每500毫秒闪烁一次
    t500 = millis();
    digitalWrite(led, !digitalRead(led));
  }
}

这些3V继电器的电阻非常低,我不确定数字输出是否足够强大。个人建议通过晶体管来控制继电器。
英文:

@Peter is true : avoid blocking delay code.

here is an example : I have a little simplified your code and added else if to make it more readable.

update1 :

   int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
unsigned long t200 = 200;
unsigned long t500 = 500;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) && !digitalRead(s2)) {
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
blinkLed200();
} else if (!digitalRead(s1) && digitalRead(s2)) {
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
blinkLed500();
}
if (digitalRead(k1) && (millis() - startTime1 >= led1Duration)) {
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
if (digitalRead(k2) && (millis() - startTime2 >= led2Duration)) {
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
}
void blinkLed200() {
if (millis() - t200 > 200) { //tick every 200mS
t200 = millis();
digitalWrite(led, !digitalRead(led));
}
}
void blinkLed500() {
if (millis() - t500 > 500) { //tick every 500mS
t500 = millis();
digitalWrite(led, !digitalRead(led));
}
}

The 3V relays have a very low resistance and I am not sure that the digital outputs are powerful enough. Personally I always switch relays via a transistor.

huangapple
  • 本文由 发表于 2023年2月18日 00:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487038.html
匿名

发表评论

匿名网友

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

确定