Need help in finding a tiny bug - Nano

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 ============================================================

















}

You have symbolic names for your LEDs:

int ledA=8;
int ledB=9;
int ledC=10;
int ledD=11;
int ledE=12;

How about using them?

  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);

...

  else (digitalWrite (9, LOW));
  if(timer-pstimer > 1000){
    secLED=!secLED;
    sec++;
    pstimer = timer;
  }

How does that loop ever exit?

Not sure int is the right type for ledAS through ledDS and secLED. Iwould probably have used bool.

However, that wouldn't explain why 2 out of the 4 LEDs wook, and 2 don't. Are you sure you have LED C and LED D wired from the pins to GND?

Write a sketch that does nothing but turn one LED on. Do that for each LED. Once you know they all work, you can move on to doing something useful with them.

What loop?

There is nothing I can see that is wrong with your code - it works (with a few pin number tweaks) on my Uno. Ergo it is most probably either your wiring or a duff Nano.

majenko:
What loop?

Oops, I misread that as a "while". Sorry.

PeterH:
Write a sketch that does nothing but turn one LED on. Do that for each LED. Once you know they all work, you can move on to doing something useful with them.

Did it. All my LEDs wire correctly. how about this bug?

int ledA=8;
int ledB=9;
int ledC=10;
int ledD=11;
int ledE=12;

void setup() {  

pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);

}

void loop() {

digitalWrite (ledE, LOW);
digitalWrite (ledA, HIGH);
delay (500);
digitalWrite (ledA, LOW);
digitalWrite (ledB, HIGH);
delay (500);
digitalWrite (ledB, LOW);
digitalWrite (ledC, HIGH);
delay (500);
digitalWrite (ledC, LOW);
digitalWrite (ledD, HIGH);
delay (500);
digitalWrite (ledD, LOW);
digitalWrite (ledE, HIGH);
delay (500);


}

The LEDs were numbers before. I use symbolic names for them to make it easier for me to follow on my board. A to E, from left to right. I don't think there is any difference. I have written a simple code to test all LEDs and these symbolic names. They all work.

I am not sure what you really mean by right type, but I am definitely sure all my LEDs wire correctly.

lar3ry:
Not sure int is the right type for ledAS through ledDS and secLED. Iwould probably have used bool.

However, that wouldn't explain why 2 out of the 4 LEDs wook, and 2 don't. Are you sure you have LED C and LED D wired from the pins to GND?

majenko:

[quote author=Nick Gammon link=topic=204192.msg1503595#msg1503595 date=1386800621]

  if(timer-pstimer > 1000){

secLED=!secLED;
   sec++;
   pstimer = timer;
 }




How does that loop ever exit?

What loop?

There is nothing I can see that is wrong with your code - it works (with a few pin number tweaks) on my Uno. Ergo it is most probably either your wiring or a duff Nano.
[/quote]

maybe because I have a damaged nano board??

majenko:

[quote author=Nick Gammon link=topic=204192.msg1503595#msg1503595 date=1386800621]

  if(timer-pstimer > 1000){

secLED=!secLED;
   sec++;
   pstimer = timer;
 }




How does that loop ever exit?

What loop?

There is nothing I can see that is wrong with your code - it works (with a few pin number tweaks) on my Uno. Ergo it is most probably either your wiring or a duff Nano.
[/quote]

I just use serial to test my board. and it works fine.

int pinA=A6;
int pinB=A7;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins input:
  pinMode(pinA, INPUT); 
  pinMode(pinB, INPUT); 

}

void loop() {
  // if there's any serial available, read it:
  if (Serial.available() > 0) {
      int testA=analogRead (pinA);
      int testB=analogRead (pinB); 

      // print the pin
      Serial.print("PINA:");
      Serial.println(testA);
      delay (500);
      Serial.print("PINB:");      
      Serial.println(testB);
      delay (500);      
    }
  }
PINB:277
PINA:137
PINB:0
PINA:89
PINB:0
PINA:105
PINB:0
PINA:96
PINB:0
PINA:112
PINB:68
PINA:0
PINB:85
PINA:0
PINB:87
PINA:0
PINB:92
PINA:0
PINB:91
PINA:0
PINB:89
PINA:0
PINB:94
PINA:0
PINB:89
PINA:0
PINB:92
PINA:0
PINB:93
PINA:0
PINB:89
PINA:86
PINB:51
PINA:87
PINB:0
PINA:91
PINB:0
PINA:106
PINB:0
PINA:83
PINB:0
PINA:0
PINB:62
PINA:0
PINB:85
PINA:168
PINB:175
PINA:253

Maybe this little caveat on the Nano web page might shed some light on the issue:

Analog pins 6 and 7 cannot be used as digital pins.

majenko:
Maybe this little caveat on the Nano web page might shed some light on the issue:

Analog pins 6 and 7 cannot be used as digital pins.

.....that ..... thank you