count by one

Hi I am after a way to increase a count by only 1 when conditions are met.
I am currently using Count++ but this will keep increasing to value of Count as the program goes through the loop while ever the conditions in the if statement are true while I only wish to increase Count by 1 each time the conditions are met.

#define led 13
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a =0;


void setup() {
  pinMode(led, OUTPUT);
  pinMode(7,INPUT);
  lcd.begin(6, 2);
  lcd.print("Final Project");
  lcd.setCursor(16, 2);
  lcd.print(a);
  
 
}

void loop() {
  
  int count = digitalRead(7);
  
   lcd.setCursor(16, 3);
    lcd.print(a);
  
  
  if (count == LOW) 
    (a ++);  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    lcd.setCursor(6, 2); //for (a=0; a<10; a++)
    lcd.print(a);
       delay(500);
  
      { if (a  >=   5) {
         a = 0;  
        
          lcd.clear();
           lcd.begin(0, 2);
  
         lcd.print("Chain lubrication");
         digitalWrite(led, HIGH);
         delay(3000);
         digitalWrite(led, LOW);
         delay(200);
            lcd.clear();
           lcd.begin(16, 2);
            lcd.print("restart");
             delay(1000);
          lcd.clear();
           lcd.begin(16, 2);
            lcd.print("Final Project");
a = 0; 
       } }
}

Look at the state change example in the IDE.

Kudos for using code tags,but please use the IDE’s auto format tool before posting.

 lcd.begin(6, 2);
 lcd.begin(0, 2);

What is that supposed to be doing?

You only need one begin() function, usually only in setup(), to initialize the display.
It should be:

 lcd.begin(16, 2);
lcd.setCursor(16, 2);

lcd.setCursor(6, 2);

You have a 2 line LCD. You are writing to the third line. Lines are numbered from 0 (0, 1)

groundFungus Thank you very much! Does anyone know how to arrange the matter of the count that will count by one each time?

Did you see reply #1?

The state change detection example.

Yes and I still can not figure out how to do the count each time. Once the sensor detects the numbers are counted quickly and do not wait for the next encounter with the sensorOnce the sensor detects the numbers are counted quickly and do not wait for the next identification with the sensor

Post your code as it looks now and maybe we can help you with it

#define led 13
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a =0;


void setup() {
  pinMode(led, OUTPUT);
  pinMode(7,INPUT);
  lcd.begin(6, 2);
  lcd.print("Final Project");
  lcd.setCursor(16, 3);
  lcd.print(a);
  
 
}

void loop() {
  
  int count = digitalRead(7);
  
   lcd.setCursor(16, 3);
    lcd.print(a);
  
  
  if (count == LOW) 
    (a ++);  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    lcd.setCursor(6, 3); 
    lcd.print(a);
       delay(500);
  
      { if (a  >=   5) {
         a = 0;  
        
          lcd.clear();
        
  
         lcd.print("Chain lubrication");
         digitalWrite(led, HIGH);
         delay(3000);
         digitalWrite(led, LOW);
         delay(200);
            lcd.clear();
        
            lcd.print("restart");
             delay(1000);
          lcd.clear();
        
            lcd.print("Final Project");
a = 0; 
       } }
}

Did you look at the state change detection example. It actually shows how to count button presses. Did you at least try to write the code using the state change example? The "trick" is to only update the count when the pin changes from high to low, not when it is high or low. The state change will detect the transition.

lcd.begin(6, 2);

Really, you have a 6 character wide 2 line display?

lcd.setCursor(16, 3);
 lcd.setCursor(6, 3);

A 16x2 display has character positions (columns) numbered 0 to 15 and lines (rows) numbered 0 and 1.

You really need to study the LiquidCrystal reference.

The code in reply #8 looks very like the code in the original post.

You can find an example in the following link, which might be helpful for you (the poster).

Try this..

if (count == LOW) { // If you get a low..
a++; // Bump up your a var by one.
while(digitalRead(7)==LOW); // Hold 'till the button is released.
}

-jim lee

yonilavi:
Yes and I still can not figure out how to do the count each time. Once the sensor detects the numbers are counted quickly and do not wait for the next encounter with the sensorOnce the sensor detects the numbers are counted quickly and do not wait for the next identification with the sensor

You don’t want to increase the count when the sensor is detected.
You want to increase it when it has changed from being not detected to being detected.

jimLee:
Try this..

if (count == LOW) { // If you get a low..
a++; // Bump up your a var by one.
while(digitalRead(7)==LOW); // Hold 'till the button is released.
}

-jim lee

That won't work reliably without a debounce delay.

aarg:
That won’t work reliably without a debounce delay.

Fine…

Try this then.

if (count == LOW) { // If you get a low…
delay(50);
a++; // Bump up your a var by one.
while(digitalRead(7)==LOW); // Hold 'till the button is released.
delay(50);
}

-jim lee