[half-Solved?]simple logic wrong result

timer is a switch to turn ON/OFF the timer count down;

 nowtimer=millis();
  if(nowtimer-pretimer>=1000) {
    sec++;    
    fc++;
    if(fc==9)fc=1;
    
    if(timer==1){
      lcd.clear();
      csec--;
      arps++; 
      if(arps==8)arps=0;
    }
    
    pretimer=nowtimer;     
  }

counter is the result once timer is over.

  if(cday==0 && chr==0 && cmin==0 && csec==0 && timer==1){timer=0; counter=1; }

Only one line doesn’t work here. After (counter==1), I want to reset counter back to counter=0. It doesn’t work.

  case 1:
    if(timer==0  && counter==0){
    char y[17]; 
    sprintf(y, " <Timer> [%s]", sp);
    lcd.setCursor (0,1); 
    lcd.print (y); 
     }
     
    if(timer==1 && counter==0){
      
    lcd.setCursor (arps,1); 
    lcd.write (ar);    
       
    char y[17]; 
    sprintf(y, "[%s]", sp);
    lcd.setCursor (9,1); 
    lcd.print (y); 
     }         
     
    if(counter==1){
    char y[17]; 
    sprintf(y, "< Time is Up!! >");
    lcd.setCursor (0,1); 
    lcd.print (y);   
    if(bb.clicks==1) counter=0;                 //<<<<<<<<<<<<<<<<<<<<<<< Only this part doesn't work
    }
    
    if(counter==0) if(bb.clicks==1) timer=!timer; 
    
    break;

I am curious, what is bb.clicks?

It is silly to ask this question without posting the entire code.

If you have really managed to isolate the problem to a single line then it shouldn't tak you more than 5 or 10 minutes of diagnostics to figure out what causes it.

As well as not posting all the code you have not told us how you have come to the conclusion that that line is at fault or what diagnostic tests you have already tried.

...R

Perhaps bb.clicks isn't 1?

Stick a Serial.println(bb.clicks); in there so you can see.

Or, maybe it is setting counter to 0 but you don't know it?

Stick a Serial.println(counter); in there so you can see.

Robin2: It is silly to ask this question without posting the entire code.

If you have really managed to isolate the problem to a single line then it shouldn't tak you more than 5 or 10 minutes of diagnostics to figure out what causes it.

As well as not posting all the code you have not told us how you have come to the conclusion that that line is at fault or what diagnostic tests you have already tried.

...R

that is exactly the entire code with "counter" in it. The rest of the code has nothing to do with counter.

arduinomagbit: that is exactly the entire code with "counter" in it. The rest of the code has nothing to do with counter.

Ah, but you see, nobody can compile and run the snippet you posted.

Want help?- well to paraphrase Perry Cox in Scrubs, "Help us to help you, help us to help you...."

Did you stick my Serial.prints in?

JimboZA: Perhaps bb.clicks isn't 1?

Stick a Serial.println(bb.clicks); in there so you can see.

Or, maybe it is setting counter to 0 but you don't know it?

Stick a Serial.println(counter); in there so you can see.

bb.clicks is just a press button...I have use this code through out my sketch. highly doubted it's the problem. and I am pretty sure, counter is one because when counter is 1, it print out a line on the LCD;

so, I am very confused what is wrong here.

more bb.clicks without any problem

  case 2:
    lcd.setCursor (0,1); 
    lcd.print ("[ Reset ]"); 
    if(bb.clicks==1) {cday=0; chr=0; cmin=0; csec=0; post=1; pspost=0; counter=0; }    
    break;      
    
  case 3:
    lcd.setCursor (4,1); 
    lcd.write (au); 
    if(bb.clicks==1) cday++;    
    break;

so, I am very confused what is wrong here.

What is wrong is that you are being very arrogant and not taking the advice that has been given you.

You present an impossible situation because that is what you have in your mind. Yes it is impossible that code does behave like this. So the soloution is to either post all the code or write a much smaller piece of code that shows the problem.

If you are not prepared to make that effort the. Why should anyone make the effort to help you?

Grumpy_Mike:

so, I am very confused what is wrong here.

What is wrong is that you are being very arrogant and not taking the advice that has been given you.

You present an impossible situation because that is what you have in your mind. Yes it is impossible that code does behave like this.
So the soloution is to either post all the code or write a much smaller piece of code that shows the problem.

If you are not prepared to make that effort the. Why should anyone make the effort to help you?

here is all the code:

byte AU[8] = {
  0b00100,
  0b00100,
  0b01110,
  0b11111,
  0b00100,
  0b01110,
  0b01110,
  0b01010};
  
byte AR[8] = {
	0b01000,
	0b00100,
	0b00110,
	0b11111,
	0b11111,
	0b00110,
	0b00100,
	0b01000};  

byte CK[8] = {
	0b00000,
	0b01110,
	0b10101,
	0b10101,
	0b10111,
	0b10001,
	0b01110,
	0b00000
};  


byte AL[8] = {
	0b00000,
	0b00100,
	0b01110,
	0b01110,
	0b01110,
	0b11111,
	0b00100,
	0b00000
};

#define au 0x01
#define ar 0x02
#define ck 0x03
#define al 0x04

#define led 6

#include "ClickButton.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,10,9,8,7);

//------------------------------------------------------------------------------------------------------

ClickButton ba(4, LOW, CLICKBTN_PULLUP);
ClickButton bb(3, LOW, CLICKBTN_PULLUP);

int menu=1;

int sec=0; 
int minn=0; 
int hr=0; 
int halfDay=0;
int day=0;
char* ap="";
char* face="";
int fc=1;

int csec=0;
int cmin=0;
int chr=0;
int cday=0;
int timer=0;
int counter=0;
char* sp="";
int arps=0;

int asec=0;
int amin=0;
int ahr=0;
int ahalfDay=0; 
int aday=0;
int alarm=0;
int bell=0;
char* of="";
char* aap="";

int post=1;
int pspost=1;

int test=888;

unsigned long nowtimer=0;
unsigned long pretimer=0;

//------------------------------------------------------------------------------------------------------
void setup()
{

  lcd.begin(16,2); 
  lcd.createChar(1, AU);
  lcd.createChar(2, AR);  
  lcd.createChar(3, CK); 
  lcd.createChar(4, AL);  
  pinMode (led, OUTPUT);
  ba.multiclickTime = 50;
  bb.multiclickTime = 50;    
}


//------------------------------------------------------------------------------------------------------
void loop()
{
  ba.Update();
  bb.Update();  

  if(ba.clicks==-1) {menu++; post=1; pspost=0;}    //3 menu
  if(menu==4) menu=1;
  
  if(post-pspost==1){  //clear lcd display everytime position has been changed
      lcd.clear();
      pspost=post;
  }
  
  if(counter ==1 || bell == 1)   digitalWrite (led, HIGH);
  else digitalWrite (led, LOW); 
  
  nowtimer=millis();
  if(nowtimer-pretimer>=1000) {
    sec++;    
    fc++;
    if(fc==9)fc=1;
    
    if(timer==1){
      lcd.clear();
      csec--;
      arps++; 
      if(arps==8)arps=0;
    }
    
    pretimer=nowtimer;     
  }  


  if(sec==60){  minn++;    sec=0;  }
  if(minn==60){   hr++;    minn=0;  }
  if(hr==12){ halfDay++;  hr=0; }
  if(halfDay==2){ day++;  halfDay=0; }
  
  if(csec==-1){cmin--; csec=59;}
  if(cmin==-1){chr--; cmin=59;} 
  if(chr==-1){cday--; chr=23;}
  if(cday==0 && chr==0 && cmin==0 && csec==0 && timer==1){timer=0; counter=1; } 
 
 
  if(asec==60){  asec=0;  }
  if(amin==60){  amin=0;  }
  if(ahr==12) {   ahr=0; }
  if(aday==50) {   aday=0; }
  
 if(sec==asec && minn==amin && hr==ahr && halfDay==ahalfDay && day==aday && alarm==1){alarm=0; bell=1;} 
  
  if(halfDay==0)ap="AM"; else ap="PM";
  if(timer==0)sp="Start"; else sp="Pause";  
  
  if(alarm==0)of="OFF"; else of="ON ";  
  if(ahalfDay==0)aap="AM"; else aap="PM";  
  
  if(fc==1)face="=_=";  
  if(fc==2)face="-.-";  
  if(fc==3)face="*.*";  
  if(fc==4)face="*o*";  
  if(fc==5)face="^_^";  
  if(fc==6)face="^.^";  
  if(fc==7)face="@-@";  
  if(fc==8)face="w.w";    
  //===== menu 1 =============================================================================================

  if(menu==1){

    char x[17];
    sprintf(x,"%c %02d:%02d:%02d:%02d %s", ck,day,hr,minn,sec,ap);  
    lcd.setCursor (0,0); 
    lcd.print (x);  
    
  
  switch (post){
  case 1:
    char y[17]; 
    sprintf(y, " Day:Hr:Min  %s", face);
    lcd.setCursor (0,1); 
    lcd.print (y); 
    break;
    
  case 2:
    lcd.setCursor (0,1); 
    lcd.print ("[ Reset ]"); 
    if(bb.clicks==1){day=0; hr=0; minn=0; sec=0; halfDay=0; post=1; pspost=0;  }    
    break;    
    
  case 3:
    lcd.setCursor (3,1); 
    lcd.write (au); 
    if(bb.clicks==1) day++;    
    break;    
    
  case 4:
    lcd.setCursor (6,1); 
    lcd.write (au); 
    if(bb.clicks==1) hr++;    
    break;    

  case 5:
    lcd.setCursor (9,1); 
    lcd.write (au); 
    if(bb.clicks==1) minn++;    
    break; 

  case 6:
    lcd.setCursor (12,1); 
    lcd.write (au); 
    if(bb.clicks==1) sec=0;    
    break;      
    
  case 7: 
    lcd.setCursor (15,1); 
    lcd.write (au); 
    if(bb.clicks==1) halfDay=!halfDay;    
    break;     
  }
  

  if(ba.clicks==1) post++;
  if(bb.clicks==-1){post=1; pspost=0;}  
  if(post==8){post=1; pspost=0;}  

  }


  //===== menu 2 =============================================================================================
  if(menu==2){

    char x[17];
    sprintf(x,"C%c %02d:%02d:%02d:%02d %c", ar, cday,chr,cmin,csec, ar);  
    lcd.setCursor (0,0); 
    lcd.print (x);       


  switch (post){
  case 1:
    if(timer==0  && counter==0){
    char y[17]; 
    sprintf(y, " <Timer> [%s]", sp);
    lcd.setCursor (0,1); 
    lcd.print (y); 
     }
     
    if(timer==1 && counter==0){
      
    lcd.setCursor (arps,1); 
    lcd.write (ar);    
       
    char y[17]; 
    sprintf(y, "[%s]", sp);
    lcd.setCursor (9,1); 
    lcd.print (y); 
     }         
     
    if(counter==1){
    char y[17]; 
    sprintf(y, "< Time is Up!! >");
    lcd.setCursor (0,1); 
    lcd.print (y);   
    if(bb.clicks==1) {post=1; pspost=0; counter=0;}   
    }
    
    if(counter==0) if(bb.clicks==1) timer=!timer; 
    
    break;   
    
  case 2:
    lcd.setCursor (0,1); 
    lcd.print ("[ Reset ]"); 
    if(bb.clicks==1) {cday=0; chr=0; cmin=0; csec=0; post=1; pspost=0; counter=0; }    
    break;      
    
  case 3:
    lcd.setCursor (4,1); 
    lcd.write (au); 
    if(bb.clicks==1) cday++;    
    break;    
    
  case 4:
    lcd.setCursor (7,1); 
    lcd.write (au); 
    if(bb.clicks==1) chr++;    
    break;    

  case 5:
    lcd.setCursor (10,1); 
    lcd.write (au); 
    if(bb.clicks==1) cmin++;    
    break; 

  case 6:
    lcd.setCursor (13,1); 
    lcd.write (au); 
    if(bb.clicks==1) csec++;    
    break;      
      
  }
  

  if(ba.clicks==1) post++;
  if(bb.clicks==-1) {post=1; pspost=0;} 
  if(post==7){post=1; pspost=0;}  
  }  
  //===== menu 3 =============================================================================================
  
  if(menu==3){

    char x[17];
    sprintf(x,"%c %02d:%02d:%02d:%02d %s", al, aday,ahr,amin,asec,aap);  
    lcd.setCursor (0,0); 
    lcd.print (x);       


  switch (post){
  case 1:
    if(bell==0){
    char y[17]; 
    sprintf(y, "  <Alarm>  [%s]", of);
    lcd.setCursor (0,1); 
    lcd.print (y);}
     
     else {
     char y[17]; 
    sprintf(y, "<Alarm went off>");
    lcd.setCursor (0,1); 
    lcd.print (y);
     }
    
    if(bb.clicks==1) alarm=!alarm;  
    break;

  case 2:
    lcd.setCursor (0,1); 
    lcd.print ("[ Reset ]"); 
    if(bb.clicks==1){aday=0; ahr=0; amin=0; asec=0; ahalfDay=0; post=1; pspost=0; alarm=0; bell=0;}    
    break; 
    
  case 3:
    lcd.setCursor (3,1); 
    lcd.write (au); 
    if(bb.clicks==1) aday++;    
    break;    
    
  case 4:
    lcd.setCursor (6,1); 
    lcd.write (au); 
    if(bb.clicks==1) ahr++;    
    break;    

  case 5:
    lcd.setCursor (9,1); 
    lcd.write (au); 
    if(bb.clicks==1) amin++;    
    break; 

  case 6:
    lcd.setCursor (12,1); 
    lcd.write (au); 
    if(bb.clicks==1) asec++;    
    break;      
    
  case 7: 
    lcd.setCursor (15,1); 
    lcd.write (au); 
    if(bb.clicks==1) ahalfDay=!ahalfDay;    
    break;     
  }
  

  if(ba.clicks==1) post++;
  if(bb.clicks==-1) {post=1; pspost=0;}  
  if(post==8){post=1; pspost=0;}  
  }    
  //===== menu 4 ===========================


}

here is all the code:

But still, as far as I can see, without Serial.prints to see if bb.clicks is 1, when counter is 1, inside case 1 of menu 2.

Do you, or do you not, know for a fact that bb.clicks is 1 under those very specific constraints (counter is 1, inside case 1 of menu 2), and if so, how do you know. 10 seconds work to stick a serial print in there....

JimboZA:

here is all the code:

But still, as far as I can see, without Serial.prints to see if bb.clicks is 1, when counter is 1, inside case 1 of menu 2.

Do you, or do you not, know for a fact that bb.clicks is 1 under those very specific constraints (counter is 1, inside case 1 of menu 2), and if so, how do you know. 10 seconds work to stick a serial print in there…

here is my serial result

counter:0
timer:0
counter:0
timer:0
counter:0
timer:1 ////<<<< timer = 1 when I press the button
counter:1 ////<<<<< counter became 1 right away
timer:0
counter:1
timer:0
counter:1
timer:0
counter:1
timer:0
counter:1
timer:0
counter:1
timer:0
counter:1
timer:0

===========here is when the button is pressed
bb 0
bb 1
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 0
bb 1
bb 0

The line you’re saying doesn’t work, relies on counter being 1 already (inside case 1 of menu 2) if I followed the thread properly. Then it looks for bb.clicks being 1.

Have you got serial.prints right here:

if(counter==1){
    char y[17]; 
    sprintf(y, "< Time is Up!! >");
    lcd.setCursor (0,1); 
    lcd.print (y);   
             Serial.println(counter);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (must be 1)
             Serial.println(bb.clicks);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (hoping for 1)
    if(bb.clicks==1) counter=0;                 //<<<<<<<<<<<<<<<<<<<<<<< Only this part doesn't work
             Serial.println(counter);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (should be 0 now)
    }

JimboZA:
The line you’re saying doesn’t work, relies on counter being 1 already (inside case 1 of menu 2) if I followed the thread properly. Then it looks for bb.clicks being 1.

Have you got serial.prints right here:

if(counter==1){

char y[17];
    sprintf(y, “< Time is Up!! >”);
    lcd.setCursor (0,1);
    lcd.print (y);   
             Serial.println(counter);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (must be 1)
             Serial.println(bb.clicks);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (hoping for 1)
    if(bb.clicks==1) counter=0;                 //<<<<<<<<<<<<<<<<<<<<<<< Only this part doesn’t work
             Serial.println(counter);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (should be 0 now)
    }

here is the result, seems to me that when bb goes to 1, counter goes to 0, but after bb goes back to 0, counter goes back to 1

counter before:1
bb:0
counter after:1

counter before:1
bb:0
counter after:1

counter before:1
bb:0
counter after:1

counter before:1
bb:1
counter after:0

counter before:1
bb:1
counter after:0

counter before:1
bb:1
counter after:0

counter before:1
bb:1
counter after:0

counter before:1
bb:1
counter after:0

counter before:1
bb:0
counter after:1

counter before:1
bb:0
counter after:1

counter before:1
bb:0
counter after:1

counter before:1

if(counter==1){
    char y[17]; 
    sprintf(y, "< Time is Up!! >");
    lcd.setCursor (0,1); 
    lcd.print (y);   
             Serial.print ("counter before:");
             Serial.println(counter);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (must be 1)
             
             Serial.print ("bb:");
             Serial.println(bb.clicks);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (hoping for 1)
    if(bb.clicks==1) counter=0;       //<<<<<<<<<<<<<<<<<<<<<<< Only this part doesn't work
    
             Serial.print ("counter after:");
             Serial.println(counter);  // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (should be 0 now)
             Serial.println ("------");
    }

    if(counter==0)  if(bb.clicks==1) timer=!timer;
    
    break;

seems to me that when bb goes to 1, counter goes to 0, but after bb goes back to 0, counter goes back to 1

So this line…

if(bb.clicks==1) counter=0;                 //<<<<<<<<<<<<<<<<<<<<<<< Only this part doesn't work

… actually does work, as suspected all along.

So now we (read as, you 8) ) need to examine the rest of the flow to see where it goes to set counter to 1 again.

Methinks you need some more serial prints, so that you can see where this convolution of code actually goes.

JimboZA:

seems to me that when bb goes to 1, counter goes to 0, but after bb goes back to 0, counter goes back to 1

So this line…

if(bb.clicks==1) counter=0;                 //<<<<<<<<<<<<<<<<<<<<<<< Only this part doesn't work

… actually does work, as suspected all along.

So now we (read as, you 8) ) need to examine the rest of the flow to see where it goes to set counter to 1 again.

Methinks you need some more serial prints, so that you can see where this convolution of code actually goes.

I think i see where the problem is. It is probably from the noises of the button, and my code is stupid too. thanks for the help anyway. entire code save to my floppy disk doesn’t really help even a tiny bit.

arduinomagbit: I think i see where the problem is. It is probably from the noises of the button, and my code is stupid too. thanks for the help anyway. entire code save to my floppy disk doesn't really help even a tiny bit.

Perhaps you have also learned a little more about the essential art of debugging ..... and about how to get help on the Forum.

...R

that is exactly the entire code with "counter" in it. The rest of the code has nothing to do with counter.

Which part of the question "What is bb.clicks?" didn't you understand ?