I am trying to make a counter for the amount of times that my fish swims by the front of his tank and I can figure out the code problems that I have been having. The code has been compiling but it keeps on putting out random letters and symbols to the LCD instead of the numbers. (I am new to coding c++)
Here is the code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define trigPin 13
#define echoPin 12
const int led = 13;
int tcrt;
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup(){
// set up the LCD's number of columns and rows:
lcd.init();
lcd.backlight();
// initialize the serial communications:
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
tcrt = analogRead(A0);
Serial.println(tcrt);
lcd.home();
lcd.print(tcrt);
delay(0); // for testing only, wait 1 s before updating LCD with next number
analogWrite(led, tcrt/4);
}
}
Welcome to the forum, and congratulations on using code tags with your first post.
I am trying to make a counter for the amount of times that my fish swims by the front of his tank
Can you indeed sense the presence of the fish on the other side of the glass? I would have thought that the primary reflection from the glass or attenuation through the glass would be a problem, but if its working who can argue with success.
Now to the code. You should always use auto format found in the Tools pull down menu, or Ctrl + T from the keyboard. It helps when trouble shooting matching brackets problems.
First, can you run the library example for the lcd and see its proper operation? I actually can not confirm and display issues when I run your code with my setup.
Second, having the led and the trigger both on pin 13 appears to work, but it is not a good idea to use pin 13 as a general io pin. Additionally, is pin 13 a pwm pin on your Arduino?
The biggest issue that I see with your code(but it may not be the cause of the lcd issues) is that you never set previousState = currentState, and you will get multiple counts if an object is not moving but close enough to the sensor. You can do it here
if (currentState != previousState) {
previousState = currentState;
if (currentState == 1) {
counter = counter + 1;
Serial.println(counter);
}
Finally, what is connected to A0 and what does the value of the reading indicate ?
Ok so here is the updated code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define trigPin 13
#define echoPin 12
const int led = 13;
int tcrt;
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup(){
// set up the LCD's number of columns and rows:
lcd.init();
lcd.backlight();
// initialize the serial communications:
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
previousState = currentState;
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
Serial.println(tcrt);
lcd.home();
lcd.print(tcrt);
delay(0); // for testing only, wait 1 s before updating LCD with next number
analogWrite(led, tcrt/4);
}
}
I now am getting zeros after the number so now it wont print onto the LCD
And also here is the i2c library example that I am going off of:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
}
void loop()
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}
I now am getting zeros
tcrt is initialized as zero, and never takes on any other value.
#define trigPin 13
#define echoPin 12
const int led = 13;
You are using pin 13 for 2 different purposes