I am not able to reset the pulse count to zero after the condition has been met.
Here is my code.
[#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);//RS,EN,D4,D5,D6,D7
const byte sensorPin = 2;
volatile int count = 0;
void setup() {
pinMode(sensorPin, INPUT);
attachInterrupt(0, pulsecount, RISING);
for(int k=14;k<20;k++)
{
pinMode(k,OUTPUT);//pins 14-19 are enabled as output
}
lcd.begin(16, 2);//initializing LCD
}
void loop() {
lcd.setCursor(0,0);
lcd.print("PulseCount:");
lcd.setCursor(11,0);
lcd.print(count);
if(count == 50)
{
count = 0;
}
}
void pulsecount()
{
count = count + 1;
}
]
So what does happen to the value of count ?
What is providing the interrupt signal ?
Which Arduino board are you using ?
I am making use of the Arduino Uno. Interrupt is provided an IR sensor at digital pin 2. For example if the count should go only till 10 & then reset to zero (here I have considered 50), after reaching 10, it shows 00 on my LCD instead of showing a single zero(0) and does not count from 1 but keeps counting like 10,20,30,40... The second zero in 00 does not go away.
The count is resetting to zero, but if you have previously printed say 10 to the LCD, when you print 0 after resetting the value the 0 from the second digit of 10 is still on the screen.
Simple fix. Print a single space immediately after printing count.
I am still not able to solve that problem. Do you mind posting the correction with the code?
Thanks
GautamD:
I am still not able to solve that problem. Do you mind posting the correction with the code?
Thanks
like this:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);//RS,EN,D4,D5,D6,D7
const byte sensorPin = 2;
volatile int count = 0;
void setup() {
pinMode(sensorPin, INPUT);
attachInterrupt(0, pulsecount, RISING);
for (int k = 14; k < 20; k++)
{
pinMode(k, OUTPUT); //pins 14-19 are enabled as output
}
lcd.begin(16, 2);//initializing LCD
}
void loop()
{
static int lastCount = -1;
if (count != lastCount)
{
char displayText[17] = "";
snprintf(displayText, sizeof(displayText), "PulseCount:%2d", count);
lcd.setCursor(0, 0);
lcd.print(displayText);
lastCount = count;
if (count >= 50)
count = 0;
}
}
void pulsecount()
{
count = count + 1;
}
The corrected code worked well when the count was 10 not for any other count. I want to implement a 5 digit counter that resets after a particular count set by the user. Would you help me with a more generalised code that could count atleast upto 4 digits correctly and reset properly and count again.
Thanks.
Unfortunately it didnt work for a high count like 1000, the problem of zero at the end most digit still persists.
GautamD:
Unfortunately it didnt work for a high count like 1000, the problem of zero at the end most digit still persists.
well... you didn't ask for a high count like 1000.
you can set the width of the display changing the Format Specifier:
snprintf(displayText, sizeof(displayText), "PulseCount:%5d", count); // now allows numeric width of 5
So on RESET will all the 5 digits reset to zero? That is what I want to make the counter do.
GautamD:
So on RESET will all the 5 digits reset to zero?
what does that mean?
what did you try?
what happened when you tried?
Well right now I tried a count of 2000 and it worked perfectly. The change you suggested in the format specifier made it happen. Anyways my count is not gonna exceed 4 digits but I have taken 5 digits just in case I require on additional digit. Thanks alot for the help. 
GautamD
I understand that English may not be your first language, but you need to express your initial problem more clearly - and react appropriately when intelligent help is offered.
Bulldog has been very helpful, but it seems you've done little more than cutting and pasting his suggestions
Try understanding or Googling the functions he used for your own development!
Yes I will make sure that next time when I post some query I would have done my homework. Plus I am new here and my programming knowledge is very little. I will be working on it. Thanks for the valuable suggestions and help.
Regards
Gautam