Having problems with a button

Sorry to bother you again Larry but is there a way for me to reset the whole thing? Like when I press the button for the 5th time everything goes back to the start as if I plugged the arduino uno card just now? Maybe with this way even tho I pressed the button for 12 times it will show me 20 rather than 220 because it will reset the pushButtoncounter ?
I don't know if it would work tho. I am just thinking

First things first.

Let's get your LCD formatting straight.

Go thru your sketch and add some comments similar to these last lines seen in setup, i.e. add column comments as seen below, through the whole sketch.

  lcd.setCursor(0, 1);
  //                   111111
  //         0123456789012345
  //         xx
  lcd.print(buttonPushCounter);

  lcd.setCursor(2, 1);
  //                   111111
  //         0123456789012345
  //         xx/
  lcd.print("/");

  lcd.setCursor(10, 1);
  //                   111111
  //         0123456789012345
  //         xx/       =
  lcd.print("=");

I will add them right now thank you so much for the quick response. In the meantime maybe the problem is, there is buttonPushCounter and butCnt should I add a line of code about them

are you not clearing the display before updating it?
what happens if you previously displayed 100 and now display 2?

I have lcd.clear in the setup part. I did add one before inside of the *loop , this part of the code:

 if(ButCntMax <= ++butCnt)
                butCnt = 0;
                lcd.clear();  //<<<<<<<---------
                 lcd.setCursor(0,1);
                lcd.print(butCnt);

with this code the moment I pressed the button the whole lcd was cleared except the first digit, when I pressed the button multiple times it showed like this 1,2,3,4,0,1,2,3,4,0,1,2.... when I activated the sensor then other numbers like time and the result of the operation appeared. And the other problem is, I mentioned this in my previous posts too when I pressed it for 12 times I want it to show me 20 but instead it showed me 220. Now with lcd.clear it shows me 2 and the calculation displaying on the lcd is 2 / (time between 2 sensors) = ................. BUT it calculated the outcome like this 220/(time between 2 sensors)=............

It was like this 2 / 1(second)= 220

What is this all about ?

  if (bPress)
  {
    bPress = false;
  }

Useless code from before that I forget to delete.
....I deleted it now

Larry, I am working with arduino based projects for just a couple of months and I am certain that you more experienced but I really do think the problem might have something to do with buttonPushCounter and butCnt I feel like a piece of code missing and its about them but I cant put my finger on it :thinking: But its just a guess

Where is buttonPushCounter every reset ?

      buttonPushCounter = buttonPushCounter + 10;
. . .

what do you mean by that

  buttonPushCounter = buttonPushCounter + 10;

This will keep incrementing by 10 each time we execute this line of code.

For example only, you might want to consider something like this.

 buttonPushCounter = buttonPushCounter + 10;
 if(buttonPushCounter > 50)  //do not go over a certain value that you pick
 {
   buttonPushCounter = 0;
 }
1 Like
if(buttonPushCounter>50)
                 buttonPushCounter= 0;
                 lcd.setCursor(0,1);
                 lcd.print(buttonPushCounter);

I did it like this and it worked. Thank you so much for your help and for giving me your time. :blush:

( I didn't add curly brackets but it worked)

Always add braces, do not take short cuts !

{ }

if(buttonPushCounter>50)
{
  buttonPushCounter= 0;
}

For accurate documentation, always format your LCD print lines of code like this:

      lcd.setCursor(0, 1);
      //                   111111
      //         0123456789012345
      //         xx/ yy    =
      lcd.print("  /       =     ");

      lcd.setCursor(0, 1);
      lcd.print(buttonPushCounter);

Spend some time in learning how to use a State Machine in your sketches.

Avoid delay() at all costs.

Does doing that make much of a difference?
I usually do it like this:

lcd.setCursor(2,1);
lcd.print(/);
lcd.serCursor(8,1);
lcd.print(=);

I will check State Machine as soon as possible, thanks again

I want to comment on how you are trying to write code:

Well a "couple of months" could be 2 hours per month for 4 month which would be 8 hours of writing code

"couple of months" could be 20 hours per month for 5 month which would be 100 hours of writing code.

If it is more than 20-30 hours you must have done it in a very ineffective way with using a lot of guessings and trying around different things without really learning it and modifying the code based on guessings in an unsystematic way.

If you would have done it by following a tutorial about programming and did modify the code in a systematic way with a lot of debugging you would not ask such questions and you would be able to solve such problems yourself.

not using curly braces in the below code-section means

And beeing unaware of this fact shows that you have not learned very important basics.

Not knowing these very important basics makes you struggle again and again for hours which IMHO you would better invest into learning these very important basics.

Knowing these basics saves you dozens of hours fiddling around trying this, trying that based on guessings.

best regards Stefan

This is the format I suggest you use, it documents your code and helps you see
what the screen will look like:

lcd.setCursor(2,1);
//                   111111
//         0123456789012345
//           / 
lcd.print("/");

lcd.serCursor(8,1);
//                   111111
//         0123456789012345
//           /     =
lcd.print("=");

This would be a way of adding static text and erasing dynamic variables.

lcd.setCursor(0,1);
//                   111111
//         0123456789012345
//           /     =
lcd.print("  /     =       ");

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.