自己码了个10路继电器代码,单轻触开关控制
1,按下松手,继电器开闭交替,10路无限循环
2,再按下松手,继电器停在一个随机位置,保持常开
自己写了个Oldtime和mils,发现用轻触,有问题,会有个按钮时间的问题,假象的不灵敏
求大神帮忙改改
1,去抖
2,瞬间实现停和继续
代码奉上
int buttonPin = 2;
boolean Relstate = false;
boolean OldStatus, FlashEnable, FlashBit;
long OldTime;
int Relay2 = 3;
int Relay3 = 4;
int Relay4 = 5;
int Relay5 = 6;
int Relay6 = 7;
int Relay7 = 8;
int Relay8 = 9;
int Relay9 = 10;
int Relay10 = 11;
int Relay11 = 12;
int num;
void setup()
{
for (int i = 3; i < 13; i++) {
pinMode(i, OUTPUT);
}
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop ()
{
if (digitalRead(buttonPin) & !OldStatus) {
FlashEnable = !FlashEnable;
}
OldStatus = digitalRead(buttonPin);
if (millis() - OldTime > 200) {
FlashBit = !FlashBit;
OldTime = millis();
}
if (FlashEnable) {
if (FlashBit) {
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, HIGH);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, HIGH);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, HIGH);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, HIGH);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, HIGH);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, HIGH);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, HIGH);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, HIGH);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
num = random(3, 13);
delay(100);
}
}
else
digitalWrite(num, HIGH);
delay(100);
Relstate = !Relstate;
}
1,按下松手,继电器开闭交替,10路无限循环
2,再按下松手,继电器停在一个随机位置,保持常开
自己写了个Oldtime和mils,发现用轻触,有问题,会有个按钮时间的问题,假象的不灵敏
求大神帮忙改改
1,去抖
2,瞬间实现停和继续
代码奉上
int buttonPin = 2;
boolean Relstate = false;
boolean OldStatus, FlashEnable, FlashBit;
long OldTime;
int Relay2 = 3;
int Relay3 = 4;
int Relay4 = 5;
int Relay5 = 6;
int Relay6 = 7;
int Relay7 = 8;
int Relay8 = 9;
int Relay9 = 10;
int Relay10 = 11;
int Relay11 = 12;
int num;
void setup()
{
for (int i = 3; i < 13; i++) {
pinMode(i, OUTPUT);
}
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop ()
{
if (digitalRead(buttonPin) & !OldStatus) {
FlashEnable = !FlashEnable;
}
OldStatus = digitalRead(buttonPin);
if (millis() - OldTime > 200) {
FlashBit = !FlashBit;
OldTime = millis();
}
if (FlashEnable) {
if (FlashBit) {
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, HIGH);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, HIGH);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, HIGH);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, HIGH);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, HIGH);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, HIGH);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, HIGH);
digitalWrite(Relay11, LOW);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, HIGH);
delay(100);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay5, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay7, LOW);
digitalWrite(Relay8, LOW);
digitalWrite(Relay9, LOW);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, LOW);
delay(100);
num = random(3, 13);
delay(100);
}
}
else
digitalWrite(num, HIGH);
delay(100);
Relstate = !Relstate;
}