I am trying to interface a proximity sensor as input to the arduino and i want the controller to count whenever sensor gives an output and display the counter on a 16x2 lcd.
I cannot construct a code for this, if anyone could please urgently try to help me that would a great favor.
Thanks, Zaid.
I cannot construct a code for this
You have our permission to do so. What's stopping you?
Can you read the proximity sensor? Can you count the number of times the sensor has changed state? Can you print anything on the LCD? When is our homework due? Why haven't we got it done yet?
No i am unable to construct a simple counter program, it displays 0 or sometimes nothing on LCD. i am so pissed up
I have successfully interfaced the lcd with arduino and i have tried many programs with it but when it comes to counter it just doesn't work
I need to get it done in a day
I need to get it done in a day
Well, you need to post some code, then. Use the Reply icon, not the stupid quick reply field, and use the Code icon to create code tags and post your code between them.
#include <LiquidCrystal.h>
#define inpin 7
#include <Wire.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
int count=0;
pinMode(inpin, INPUT); // Set pin 2 for input
}
void loop()
{
int inVal = digitalRead(inpin); // sets function for read of pin 2
if(inVal!=0)
{
int count=0;
int b=count++;
lcd.print(b);
delay(500);
}}
int a = 5;
int b = a++;
would it surprise you to find out that a == 6 and b==5 now?
I'm not sure how the code is supposed to create any countdown. But it does seem like resetting the count to 0 every pass through the loop doesn't make much sense at all.
You need something like this:
int count = 60; // countdown from 60
void setup(){
Serial.begin(9600);
}
void loop(){
if(count == 0){
Serial.println("Boom");
while(1); // Hang the program. Countdown over.
}
Serial.println(count--); //Print the count and then subtract one
delay(1000);
}
Please read Nick Gammon's first two posts at the top of this Forum. He lays down the guidelines which makes it easier for us to help you...like using code tags, as Paul suggested.
Also, this makes very little sense:
if(inVal!=0)
{
int count=0;
int b=count++;
lcd.print(b);
delay(500);
}
Every time inVal is not equal to zero, you define count and set it to zero. b is a total waste of memory, but it will equal 1, which you display. The next time inVal isn't zero, you go through the same process. Please explain how b can ever be more than 1.
b will be 0. Putting the ++ after the variable name causes it to be evaluated and then incremented. If he had used ++count then b would be 1 and so would count. But in this case by the time he does the print count is 1 and b is 0.
void loop()
{
int inVal = digitalRead(inpin); // sets function for read DIGITAL value (int inVal) of pin 2
if(inVal!=0) // which can be WHAT ?
{
//and than you reset INTEGER count
int count=0;
//and than you set another INTEGER to incremented value of such integer count
int b=count++;
// and print it
lcd.print(b);
Now ask someone to explain to you what "urgent / urgently " means than analyze your code logic before you write another line of code.
Pay attention what are the values of your variables NOW before you print.
It is not that hard, simple math.
Cheers Vaclav
@Delta_G: Yep, it is post-incremented so it's hard pressed to see how it can display 1 even though it is 1 after the if statement block.