Arduino counter

Hi, I am looking for some help, I am looking to use a micro switch to count and display to an LCD, I have the code working but have two problems, 1/ at the start no digit shows up, then counts 1,2,3 how can I get the LCD to show 0 at the start instead of blank and 2/ how can I code to count to 10 and then reset to 0, any help appreciated.

Code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int inputPin = 7;
int val = 0;
int count = 0;

void setup()
{
lcd.begin(16, 4);
pinMode(inputPin, INPUT);
lcd.print("Counter");
}
void loop(){
val = digitalRead(inputPin);

if(val==LOW){

count = count + 1;

lcd.setCursor(0,2);

lcd.print(count);

delay(1000);
}}

completestarter:
how can I get the LCD to show 0 at the start instead of blank

Write a "0" to it in setup().

Thanks for the reply, please excuse my lack of knowledge, but how do I write "0" to it in setup, I have tried analogWrite (7, 0) but still same.

completestarter:
Thanks for the reply, please excuse my lack of knowledge, but how do I write "0" to it in setup, I have tried analogWrite (7, 0) but still same.

The same way you did it in loop(), how else? :o

lcd.setCursor(0,2);
lcd.print(0);
#include <LiquidCrystal.h>

const uint8_t   pinBUTTON   = 7;

const uint8_t   BUTTON_UP   = HIGH;
const uint8_t   BUTTON_DOWN = LOW;

int             counter     = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void loop()
{ 
    if ( BUTTON_DOWN = digitalRead(pinBUTTON) )
    {
        counter = ((counter + 1) % 10);

        lcd.setCursor(0, 2);
        lcd.print(counter);
        delay(1000);
    }
}

void setup()
{
    pinMode(pinBUTTON, INPUT);

    lcd.begin(16, 4);
    lcd.print(counter);
}

Thanks so much for the help, that is now starting at Zero :slight_smile: how do I get it to reset back to 0 after 10 counts ?

completestarter:
Thanks so much for the help, that is now starting at Zero :slight_smile: how do I get it to reset back to 0 after 10 counts ?

Test to see whether is is greater than 10. If it is, set it to 0.https://www.arduino.cc/en/Reference/If

completestarter:
Thanks so much for the help, that is now starting at Zero :slight_smile: how do I get it to reset back to 0 after 10 counts ?

Reply #4

lloyddean:
Reply #4

I don't think the OP is trying your stuff yet. Probably should.

Thanks so much lloyddean, I did try your code but must be missing something as I am getting compiller error, I have included the code line counter = ((counter + 1) % 10); in my code and it works perfect, the problem I have is this counts to 9 and resets to zero, but when I put in to count to 10 then reset at next push it resets to 00 and then increments 10, 20, 30 etc, how do I get around this please

code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int inputPin = 7;
int val = 0;
int count = 0;

void setup()
{
lcd.begin(16, 4);
pinMode(inputPin, INPUT);
lcd.print("Counter");
lcd.setCursor(0,2);
lcd.print(0);
}
void loop(){
val = digitalRead(inputPin);

if(val==LOW){

count = ((count + 1) % 10);

}

lcd.setCursor(0,2);

lcd.print(count);

delay(300);
}

counts to 9 and resets to zero, but when I put in to count to 10 then reset at next push it resets to 00 and then increments 10, 20, 30 etc, how do I get around this please

Print a space after the number to get rid of the zero left when you printed 10.

Thanks UKHeliBob , excuse my lack of knowledge, but where in my code can I do this ?

completestarter:
Thanks UKHeliBob , excuse my lack of knowledge, but where in my code can I do this ?

Don't mean to be rude, but you won't get anywhere being so helpless. Program lines are performed one after the other in sequence. Just walk through it step by step in your head until you get it.

Thanks aarg, figured it out, one final question, is it possible on a 2 x 16 character display to display text "counter" , the increment counter 0-10 and the cumulative count ?

completestarter:
Thanks aarg, figured it out, one final question, is it possible on a 2 x 16 character display to display text "counter" , the increment counter 0-10 and the cumulative count ?

How many characters do you need to print the information ?
How many characters can the LCD display ?
If the former is smaller than the letter then the answer is yes.
You have complete control over which text and data is printed, where it is printed and how often,

@completestarter, I suggest you think of your project as two parts.

In one part a switch is used to increment a variable. In the other part the value in the variable is displayed on the LCD. The only common ground between the two parts is the variable.

The code in loop() could be something like

void loop() {
   readSwitch();
   updateLCD();
}

Have a look at Planning and Implementing a Program.

Even if you think the use of functions is overkill for this small project it is a technique that is useful to learn at an early stage.

...R

You've already used the setCursor() function. What do you think it does? If you're not sure, there is documentation that you can consult.

Thanks everyone for your help, I finally have the code I need :slight_smile:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int inputPin = 7;
int val = 0 ;
int count = 0 ;
int add = 0;
void setup()
{
lcd.begin(16, 4);
pinMode(inputPin, INPUT);
lcd.print("Counter");
lcd.setCursor(0,2);
}
void loop(){
val = digitalRead(inputPin);

if(val==LOW){

count = ((count + 1));

}
if ((count >9)){
count = 0;
}
if (val==LOW){
add = ((add + 1));
lcd.setCursor(1,10);
lcd.print (add);
lcd.setCursor(1,1);
lcd.print("Total Count");
}
lcd.setCursor(0,2);

lcd.print(count );
lcd.print(" ");

delay(300);
}

Your code will count up quite quickly if you hold the switch closed. You have fudged it by including a delay() but that will prevent the count being incremented faster than 3 times per second.

Would it be better if 1 was added to the count each time the switch became pressed rather that when it is pressed ?

By the way. As count can now never exceed 9 think about whether you need to print the space as there will never be a second digit.

Why do you need to test whether the switch is pressed twice in the program ?

How would i make something that starts at 0 then goes up to 20, then back down to 0. But every time It goes up a number it goes slower and slower to the next number and then faster and faster when counting down. It also has to be saying flash then the number and then on intervals of 4 a double flash