Хорошо.Короче говоря, я работаю над относительно простым проектом: машиной, которая мигает светодиодом через определенные промежутки времени, в зависимости от того, какая настройка включена, что также покажет, какая настройка выбрана на 4-значном 7-сегментном светодиодном дисплее.В конце концов я планирую также включить кнопку пуска / останова.
В настоящее время схема состоит из 2x 10 тыс. Горшков, 4-значного дисплея и светодиода, подключенного к Arduino, все аппаратное обеспечение работаетхорошо.1 горшок - для выбора режима, 1 горшок - для выбора цикла для запуска в указанном режиме.
Однако работает только ОДИН из трех моих режимов, все циклы в коде (постоянное постоянное мигание при указанномчастота герц) работает как задумано.Два других режима, каждый из которых состоит из 3 отдельных циклов, не работают вообще - светодиодный индикатор гаснет или показывает 2 загорелых случайных индикатора, и светодиодный индикатор перестает мигать.Я предполагаю, что это из-за того, как я написал код / интервалы или счетчик - что я сделал неправильно / как я могу написать это так, чтобы это имело смысл?Код выглядит следующим образом:
int sensorPin = A0;
int ledPin = 1;
float sensorValue = 0;
float sensorValue2 = 0;
float hertz = 0;
int sensorPin3 = A2;
int sensorValue3 = 0;
int mode = 0;
int shortCycle = 0;
int longCycle = 0;
float hertz2 = 0;
int count = 0;
int pMin = 0;
int pMax = 1023;
int ledState = LOW;
unsigned long previousMillis = 0;
//store when led was updated
long interval = 1000;
#include "SevSeg.h"
SevSeg sevseg; //Initiate a seven segment controller object
void setup() {
byte numDigits = 4;
byte digitPins[] = {13, 12, 11, 10};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
sevseg.setBrightness(90);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(sensorPin3, INPUT);
}
void loop() {
sensorValue3 = analogRead(sensorPin3);
mode = map(sensorValue3, pMin, pMax, 1, 3);
if (mode == 1) {
sensorValue = analogRead(sensorPin);
hertz = map(sensorValue, pMin, pMax, 18, 0);
sensorValue2 = ( 1000 / hertz );
if (hertz == 1) {sevseg.setChars(" 1 h");}
else if (hertz == 2) {sevseg.setChars(" 2 h");}
else if (hertz == 3) {sevseg.setChars(" 3 h");}
else if (hertz == 4) {sevseg.setChars(" 4 h");}
else if (hertz == 5) {sevseg.setChars(" 5 H");}
else if (hertz == 6) {sevseg.setChars(" 6 H");}
else if (hertz == 7) {sevseg.setChars(" 7 H");}
else if (hertz == 8) {sevseg.setChars(" 8 H");}
else if (hertz == 9) {sevseg.setChars(" 9 H");}
else if (hertz == 10) {sevseg.setChars("10 H");}
else if (hertz == 11) {sevseg.setChars("11 H");}
else if (hertz == 12) {sevseg.setChars("12 H");}
else if (hertz == 13) {sevseg.setChars("13 H");}
else if (hertz == 14) {sevseg.setChars("14 H");}
else if (hertz == 15) {sevseg.setChars("15 H");}
else if (hertz == 16) {sevseg.setChars("16 H");}
else if (hertz == 17) {sevseg.setChars("17 H");}
else if (hertz == 18) {sevseg.setChars("18 H");}
sevseg.refreshDisplay(); // Must run repeatedly
} else if(mode == 2) {
//put short cycles here
sensorValue = analogRead(sensorPin);
shortCycle = map(sensorValue, pMin, pMax, 0, 4);
sensorValue2 = 1000 / hertz2;
if(sensorValue == 1) {sevseg.setChars("a 1");
if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
if(count >= 4620 and count < 5280){hertz2 == 11;} //1 min
if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
if(count >= 7020){hertz2 == 10;} //5 min = 10020 count
//10 minutes + end (~15 minute session)
sevseg.refreshDisplay(); // Must run repeatedly
}
else if(sensorValue == 2){sevseg.setChars("th 1");
if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
if(count >= 6480 and count< 7020){hertz2 == 9;}//1 min
if(count >= 7020 and count < 7500){hertz2 == 8;} //1 min
if(count >= 7500 and count< 7920){hertz2 == 7;} //1 min
if(count >= 7920 and count< 8280){hertz2 == 6;} //1 min
if(count >= 8280 and count < 8880){hertz2 == 5;} //2 min
if(count >= 8880 and count< 9120){hertz2 == 4;} // 1 min
if(count >= 9120){hertz2 == 5;} // 5 min = 10620
//16 minutes + end (~21 minute session)
}
else if(sensorValue == 3){sevseg.setChars("d 1");
if(count >= 0 and count< 3900){hertz2 == 13;}; //5 min
if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
if(count >= 5280 and count < 6480){hertz2 == 10;} //2 min
if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
if(count >= 7020 and count< 7500){hertz2 == 8;} //1 min
if(count >= 7500 and count< 7920){hertz2 == 7;} //1 min
if(count >= 7920 and count< 8280){hertz2 == 6;} //1 min
if(count >= 8280 and count< 8880){hertz2 == 5;} //2 min
if(count >= 8880 and count< 9600){hertz2 == 4;} // 3 min
if(count >= 9600 and count < 10320){hertz2 == 3;} // 4 min
if(count >= 10320){hertz2 == 2;} // 5 min = 10920
sevseg.refreshDisplay(); // Must run repeatedly
//23 min + end (~28 minute session)
}
} else if(mode == 3) {
//put long cycles here
//fill in times as you calculate
sensorValue = analogRead(sensorPin);
longCycle = map(sensorValue, pMin, pMax, 0, 4);
sensorValue2 = 1000 / hertz2;
if(sensorValue == 1){sevseg.setChars("a 2");
if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
if(count >= 7020 and count< 10020){hertz2 == 10;} //5 min
if(count >= 10020 and count< 11640){hertz2 == 9;} //3 min
if(count >= 11640 and count< 13440){hertz2 == 10;} //3 min
if(count >= 13440 and count< 14760){hertz2 == 11;} //2 min
if(count >= 14760 and count < 16200){hertz2 == 12;} //2 min
if(count >= 16200){hertz2 == 13;} //5 min == 20100
sevseg.refreshDisplay(); // Must run repeatedly
//25 min + end (~30 min session)
}
else if(sensorValue == 2){sevseg.setChars("t 2");
if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
if(count >= 6480 and count < 7020){hertz2 == 9;} //1 min
if(count >= 7020 and count< 7500){hertz2 == 8;} //1 min
if(count >= 7500 and count < 7920){hertz2 == 7;} //1 min
if(count >= 7920 and count< 8280){hertz2 == 6;} //1 min
if(count >= 8280 and count< 8880){hertz2 == 5;} //2 min
if(count >= 8880 and count< 9120){hertz2 == 4;} // 1 min
if(count >= 9120 and count< 10620){hertz2 == 5;} // 5 min
if(count >= 10620 and count< 11340){hertz2 == 4;} //3 min
if(count >= 11340 and count < 12240){hertz2 == 5;} //3 min
if(count >= 12240 and count< 12960){hertz2 == 6;} //2min
if(count >= 12960 and count< 13800){hertz2 == 7;} //2 min
if(count >= 13800 and count< 14280){hertz2 == 8;} //1 min
if(count >= 14280 and count< 14820){hertz2 == 9;}//1 min
if(count >= 14820 and count< 15420){hertz2 == 10;}//1 min
if(count >= 15420 and count< 16080){hertz2 == 11;}//1 min
if(count >= 16080 and count < 16800){hertz2 == 12;} //1 min
if(count >= 16800 ){hertz2 == 13 ;}// 5 min = 20700
sevseg.refreshDisplay(); // Must run repeatedly
//36 min + end (~41 min session)
}
else if(sensorValue == 3){sevseg.setChars("d 2");
if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
if(count >= 4620 and count < 5280){hertz2 == 11;} //1 min
if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
if(count >= 7020 and count< 7500){hertz2 == 8;} //1 min
if(count >= 7500 and count< 7920){hertz2 == 7;}//1 min
if(count >= 7920 and count < 8280){hertz2 == 6;} //1 min
if(count >= 8280 and count < 8880){hertz2 == 5;} //2 min
if(count >= 8880 and count < 9600){hertz2 == 4;} // 3 min
if(count >= 9600 and count < 10320){hertz2 == 3;} // 4 min
if(count >= 10320 and count < 10920){hertz2 == 2;} // 5 min
if(count >= 10920 and count< 11640){hertz2 == 3;} //4 min
if(count >= 11640 and count < 12360){hertz2 == 4;} //3 min
if(count >= 12360 and count< 12960){hertz2 == 5;} //2 min
if(count >= 12960 and count < 13680){hertz2 == 6;} //2 min
if(count >= 13680 and count< 14520){hertz2 == 7;} //2 min
if(count >= 14520 and count < 15000){hertz2 == 8;} //1 min
if(count >= 15000 and count < 15540){hertz2 == 9;} //1 min
if(count >= 15540 and count < 16140){hertz2 == 10;} //1 min
if(count >= 16140 and count < 16800){hertz2 == 11;} //1 min
if(count >= 16800 and count < 17520){hertz2 == 12;}//1 min
if(count >= 17520){hertz2 == 13;} // 5 min = 21420
sevseg.refreshDisplay(); // Must run repeatedly
//45 min + end (~50 min session)
}
}
unsigned long currentMillis = millis();
if((ledState == LOW) && (currentMillis - previousMillis >= sensorValue2))
//makes light blink at the begining of each "beat"
//as opposed to being on for a beat, off for a beat
// may remove "/hertz" at the end, if a better solution is found, becomes unusable after 4 hrtz
{
//saves last time you blinked led
previousMillis = currentMillis;
ledState = HIGH;
count =(count ++);
} else if ((ledState == HIGH) && (currentMillis - previousMillis >= 15))
{
ledState = LOW;
}
digitalWrite(ledPin, ledState);
sensorValue3 = analogRead(sensorPin3);
}