To display the count

In this code.. the C.Ct is displaying in descending order.. I tried out the increment operator too.. If I used the ++ operator then the led is not stopping at the correct count. How can we modify the code..?

#undef MyHW
#ifdef MyHW
const byte PinBut = A1;
#else
const byte PinBut = 6;
#endif

const byte PinLed =13;

byte butState;
int  butCnt;

#define Timeout 500
#define Period  1000

enum { Off = LOW, On = HIGH };

unsigned long msecLst;
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
//connect to lcd in(RS,E,D4,D5,D6,D7);

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);

    lcd.begin(16,2);
    lcd.print("Welcome");
}
void loop () {
    unsigned long msec = millis ();
    if (0 < butCnt && (msec - msecLst) > Timeout)  {
      
        for (int k=0 ; k< butCnt; butCnt--)  {
          
          lcd.setCursor(7,0);
            lcd.print("C.Ct:");
            lcd.setCursor(7,1);
            lcd.print(butCnt);
            digitalWrite (PinLed, On);
            delay (Period);
            digitalWrite (PinLed, Off);
            delay (Period);
            
        }       
    }

    byte but = digitalRead (PinBut);
    if (butState != but) {
        butState = but;
        msecLst  = msec;

        delay (10);     // debounce

        if (LOW == but)  {
            butCnt++;
          lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("T.Ct:");
            lcd.setCursor(0,1);
            lcd.print(butCnt);
          
          
        }
    }
  
}

How can I make the count to display properly in ascending order here.....

1 Like

Please post what you tried and an explanation of what you want the code to do

I have given the code above.. The code is about.. if we press the button 5 times, the led will blink for 5 times... at the same time., In the lcd it should print the count. Here in this program when I press 5 times the button, it is displaying the total count (T.Ct) properly, but the led blinking count (C.Ct) is displaying in descending order(5..4..3..2..1) since I have decremented the butCnt. But I need that to display in incrementing order ex..1..2...3...4...5. I have tried the ++ operator changed the code to print the k (in code) value., if I give that, then the led is not stopping at exact count.. its blinking forever..

I think you want this :

for (int k=0 ; k < butCnt; k++)  {
    ...
    lcd.print(k);

butCnt--;

Really your issue description needs some work, could you restate the issue and thanks for using code tags. +1

yeah! I tried this.. but its not working properly.. led is not stopping at the exact count.. and the lcd is printing the same values again and again

Then cause the LCD value to only update upon a button press instead of on every loop?

How to stop the program(led)here?

Miracle_Kite,

you are working on an informatic project. And what is most needed in an informatic project is information.

This means:

  • post the working sketch. The complete sketch as a code-section
    using this method

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting
  • post the output of the serial monitor as a code-section too.

  • write down an example that shows what result you want to have.

You can waste more time by typing more too short to be clear postings.
Or you can use time to write down a detailed long posting.

best regards Stefan

I would try this:

        for (int k=0 ; k < butCnt; k++)  {
          
          lcd.setCursor(7,0);
            lcd.print("C.Ct:");
            lcd.setCursor(7,1);
            lcd.print(k);

[/quote]

Did you really want "butCnt" to be zero after the blinks? You could keep that 'feature' by adding "butCnt = 0;" after the blink loop. I don't see how your count would ever go above 1.

Thankyou I have given like this....

if(k==butCnt)
{
butCnt=0;
}

tips how to improve your posting style

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