i need help with coding a photoresistor and LCD

Hello …

so I did my best and redo the wiring and the LCD worked with me perfectly with the "hello world "code,
but I don’t need this code i tried to take many codes to make my idea work …
which is pointing a laser on the photoresistor and if an object came and cut it it will count +1 and I will also use another photo resistor because i’m using two roads .
second which is i don’t know how to code it after i count it i want to sum the first one alone and the second one alone and see the less the display it on the LCD screen.

i’m totaly new to Arduino ,so i really need help with the coding because i’m new to it.

here is the code that i used :

#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
int n = 1;
LiquidCrystal_I2C       lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

int ledPin = 13;
int lightPin = 0;  //define a pin for Photo resistor
int threshold = 85;  //intensity of light that triggers the count
int i = 0;
int dirpin = 2;
int steppin = 3;
int j;

void setup(){
    lcd.begin (20,4,LCD_5x8DOTS);
    lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // init the backlight
    //Serial.begin(9600);  //Begin serial communcation
    pinMode(ledPin, OUTPUT);
    pinMode(dirpin, OUTPUT);
    pinMode(steppin, OUTPUT);

    digitalWrite(dirpin, LOW);     // Set the direction.


void loop(){

    if(analogRead(lightPin) > threshold ){
        if(digitalRead(ledPin) == LOW){
        digitalWrite(13, HIGH);
        digitalWrite(steppin, LOW); 
        digitalWrite(steppin, HIGH);
        digitalWrite(ledPin, LOW);
    lcd.home ();
    lcd.setCursor ( 9, 1 );    // go home     

Thank you for your time :’(

Please redraw the circuit so that it resembles normal circuit diagrams. IE, without wires going all over the place.

Another problem you may have is that LDRs are not normally used with lasers. The laser has a small spot on the LDR so will only make a small change if the LDR material is even sensitive to the light from the laser.


First thing, I would put a dropping resistor on the analog pins that the photoresistors are connected to. A 10k 1/4 watt resistor that goes from each pin to ground. This will help stabilize the signals you get from the sensors. This will change your threshold value but it will be much more stable. Without the resistors it may trip all on it’s own at a threshold of 85.

Second thing is that weedpharma is right - lasers by their nature are one frequency of light, and that frequency may not be detectable (or not very well detectable) by the photo resistor - check the sensors data sheet and see if the the frequency falls within its spectral sensitivity. If the problem is that the beam is too small be to be seen by the sensor, you may have to put some diffusion material or a diffraction grating over the sensor to spread that beam out at bit. Or get a opto detector built for laser light. In a pinch an LED is also a light detector (they love small laser beam points) But you have to get one that will detect the frequency of the laser

Its a little tough to tell what you are trying to do, there is a call to test LEDPin (13) but there is nothing connected to it in the diagram. Plus right after reading that pin you turn around and write to it without redefining it as an output.

You have the right idea there with the counter (i) being incremented after each lightPin detect. You just have to do that for both sensors and use another counter (I see j being defined but not being used so I assume…)

At every loop you can display these on the screen and subtract them to find the difference and display that number.

One gotcha - is this vehicles you are tracking? If a tire breaks the beam going 60mph the sensor and code will only have 7ms or less to trip. That might not be enough time as the code might be processing the wrong part of the loop and not listening for the trip. Or you might “hear” the first tire but not the second, which would throw your counters off. Its little better if you can raise the sensor and track the whole car but that’s still only 150ms long.

Because you are new to arduino I don’t think an Interrupt would be worth getting into (its the answer but it can be complicated to code) However, you could build a latch circuit that would stay active for a half second or so. So once the car passes, even though the sensor cannot see the tire any more, the latch circuit is still telling the arduino that its tripped.

The flaw in your sketch is that you count when the pin is low, not when it becomes low. So if a vehicle parks in front of your beam the counter will increment for ever. You need to embrace the thinking of State Change Detection.

Good point Jimbo - You could put a time-out on the detection so that if an obstacle blocks the sensor for too long it doesn't count it as a repeated hit. Or you could set a flag that you have to check and/or reset before the next increment. If the flag is still set, skip the increment.