I have rewritten this twice, first with easier code. yet, same problem occurs. When finishing uploading this code, LED C and D light up right away, while LED A and B work fine. can anyone spot this tiny bug for me? thank you in advance
and I am sure it's software problem because after removing those codes with LED C and D, and those 2 leds won't light up.
but the software part with 4 parts of similar code that I just copied and pasted and changed a letter and some, so I really have no idea what went wrong here.
int sec=0;
int minu=0;
int hr=0;
unsigned long pstimer=0;
int secLED=1;
int pbuttonA=1;
int pbuttonB=1;
int pbuttonC=1;
int pbuttonD=1;
int ledA=8;
int ledB=9;
int ledC=10;
int ledD=11;
int ledE=12;
int ledAS=0;
int ledBS=0;
int ledCS=0;
int ledDS=0;
void setup() {
pinMode(A4,INPUT_PULLUP);
pinMode(A5,INPUT_PULLUP);
pinMode(A6,INPUT_PULLUP);
pinMode(A7,INPUT_PULLUP);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
//----------------------------------------------------------------------------------
//======= Start Button ==================================================================
int cbuttonA=digitalRead (A4);
int cbuttonB=digitalRead (A5);
int cbuttonC=digitalRead (A6);
int cbuttonD=digitalRead (A7);
//buttonA//
if (cbuttonA != pbuttonA){
if(cbuttonA==LOW){ // buttonA
ledAS=!ledAS;
}
}
pbuttonA=cbuttonA;
//buttonB//
if (cbuttonB != pbuttonB){
if(cbuttonB==LOW){ // buttonB
ledBS=!ledBS;
}
}
else (digitalWrite (9, LOW));
pbuttonB=cbuttonB;
//buttonC//
if (cbuttonC != pbuttonC){
if (cbuttonC==LOW){ // buttonC
ledCS=!ledCS;
}
}
pbuttonC=cbuttonC;
//buttonD//
if (cbuttonD != pbuttonD){
if (cbuttonD==LOW){ // buttonD
ledDS=!ledDS;
}
}
pbuttonD=cbuttonD;
//----------------------------------------------------------------------------------
//======= End button ===============================================================
//----------------------------------------------------------------------------------
//======= start timer ==============================================================
unsigned long timer= millis();
if(timer-pstimer > 1000){
secLED=!secLED;
sec++;
pstimer = timer;
}
//----------------------------------------------------------------------------------
//=======End timer =================================================================
//----------------------------------------------------------------------------------
//======= start Core Code ==========================================================
digitalWrite (ledE, secLED);
digitalWrite (ledA, ledAS);
digitalWrite (ledB, ledBS);
digitalWrite (ledC, ledCS);
digitalWrite (ledD, ledDS);
//----------------------------------------------------------------------------------
//======= End Core Code ============================================================
}
rewritten my code the third times. Same thing happens. LED C and D keeps on.
int sec=0;
int minu=0;
int hr=0;
unsigned long pstimer=0;
int pbuttonA=HIGH;
int pbuttonB=HIGH;
int pbuttonC=HIGH;
int pbuttonD=HIGH;
int ledA=8;
int ledB=9;
int ledC=10;
int ledD=11;
int ledE=12;
int stLEDa=0;
int stLEDb=0;
int stLEDc=0;
int stLEDd=0;
int stLEDe=0;
void setup() {
pinMode(A4,INPUT_PULLUP);
pinMode(A5,INPUT_PULLUP);
pinMode(A6,INPUT_PULLUP);
pinMode(A7,INPUT_PULLUP);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
//----------------------------------------------------------------------------------
//======= Start Button ==================================================================
int cbuttonA=digitalRead (A4);
int cbuttonB=digitalRead (A5);
int cbuttonC=digitalRead (A6);
int cbuttonD=digitalRead (A7);
//buttonA//
if (cbuttonA != pbuttonA){
if(cbuttonA==LOW){ // buttonA
stLEDa=!stLEDa;
}
}
pbuttonA=cbuttonA;
//buttonB//
if (cbuttonB != pbuttonB){
if(cbuttonB==LOW){ // buttonB
stLEDb=!stLEDb;
}
}
else (digitalWrite (9, LOW));
pbuttonB=cbuttonB;
//buttonC//
if (cbuttonC != pbuttonC){
if (cbuttonC==LOW){ // buttonC
stLEDc=!stLEDc;
}
}
pbuttonC=cbuttonC;
//buttonD//
if (cbuttonD != pbuttonD){
if (cbuttonD==LOW){ // buttonD
stLEDd=!stLEDd;
}
}
pbuttonD=cbuttonD;
//----------------------------------------------------------------------------------
//======= End button ===============================================================
//----------------------------------------------------------------------------------
//======= start timer ==============================================================
unsigned long timer= millis();
if(timer-pstimer > 1000){
stLEDe=!stLEDe;
sec++;
pstimer = timer;
}
//----------------------------------------------------------------------------------
//=======End timer =================================================================
//----------------------------------------------------------------------------------
//======= start Core Code ==========================================================
digitalWrite (ledA, stLEDa);
digitalWrite (ledB, stLEDb);
digitalWrite (ledC, stLEDc);
digitalWrite (ledD, stLEDd);
digitalWrite (ledE, stLEDe);
//----------------------------------------------------------------------------------
//======= End Core Code ============================================================
}