[SOLVED] reading LDR value with analogRead

program is suppose to generate random #, blink LED the same # of times as the # generated and sound a buzzer every time the LED comes on using a photoresistor.
Currently the analog pin is not reading the value of the light resistor when it changes. I have been trying to figure this out for 3 days and I 'am ready to smash it :weary: please help if you can.

[code]
//generate random number. Blink and ring buzzer the same amount of times as the random number//
int LED = 12;                       //LED is pin 12.
int BUZ = 5;                        //BUZ is pin 5. 
long number;                        //set "number" as number storage.

void setup() {               //put your setup code here, to run once:
  Serial.begin(9600);        //set data speed.
  pinMode (LED,OUTPUT);      //pin 12 is output.
  pinMode (BUZ,OUTPUT);      //pin 5 is output.
  pinMode (A1,INPUT);
  randomSeed(analogRead(0));  //generate random number on analog pin A0.
  number = random(3,10);      //generate random number between 3-9 range.
  Serial.print("number=");    //Print phrase "number=" in serial monitor.
  Serial.println(number);     //Print random number after above phrase and start new line in serial monitor.
  }

void loop() {                            
int PHOTO = analogRead(A1);                 //Marks PHOTO as reading from A1 pin.
int PHOTOval = map(PHOTO, 0, 1023, 0, 255); //Maps scope and value for PHOTO and PHOTOval.
for (int i = 0; i <= number; i++){          //for this equation add 1 to "i" and run code below.
    digitalWrite (LED, HIGH);               //Turns on LED.
    analogRead(PHOTO);                      //Read A1 pin.
    Serial.println(PHOTOval);               //Print Value of pin A1.
    delay(500);                             //Wait .5 seconds.
    digitalWrite (LED, LOW);                //Turn LED off.
    analogRead(PHOTO);                      //Read A1 pin.
    Serial.println(PHOTOval);               //Print value of pin A1
    delay(500);                             //Wait .5 seconds.
    
  if (i == number){            //If "i" = # generated run code below.
    i = 0;                     //reset "i" to 0.
    number = random(3,10);     //Generate random number between 3-9.
   Serial.print("number=");    //Print phrase"number=" in serial monitor.
   Serial.println(number);     //Print random # generated after "number="and start new line in serial monitor.
    delay(2000);               //Wait 2 seconds.
  }
  }
  }

[/code]

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

Take a view here

@paulpaulson
I tried that in a previous code with this but it the LED would not blink and if it did it was not in time with the amount of blinks it needed. I finally have it working and the issue I have now is that the resistor only seems to read the value once. at least that is what shows in the monitor.

@LarryD
Because I am a new user it wont let me upload the picture I have of the schematic. I do know that the circuit is correct though.

Hello
welI I guess you have ignored my advice, haven't you?

@paulpaulson
I have not ignored your advice. I have already looked into that option and if you must know I am currently looking into it now and running into the same issue as before. If this is the route I should take that would be great but I need more hints or help in order to progress further into this particular solution.

You have this line two time in your program:

analogRead(PHOTO);

These lines do nothing and there are two thing s wrong with it.

One, you’re reading an invalid address, it should probably be A1, whatever analog pin you have your sensor wired to.

Two, you don’t assign the return value to a variable so there is no action taken by the program. It’s to be:

exampleVariable = analogRead(A1):

And then you do something with “exampleVariable” in your program.

@WattsThat
ok so I took away the useless "analogRead(PHOTO);"
and kept what was needed. I currently only want to read the value pin (A1)
but it is still only reading and printing the same number.
do I still have something wrong?

int PHOTO = analogRead(A1);   
int PHOTOval = map(PHOTO, 0, 1023, 0, 255); 
for (int i = 0; i <= number; i++){   
    digitalWrite (LED, HIGH);             
    Serial.println(PHOTOval); 
    delay(500);     
    digitalWrite (LED, LOW);           
    Serial.println(PHOTOval);      
    delay(500); 

How is the sensor wired ?

@LarryD
I have 5v going into the photo resistor connected to ground.
Between the ground and the photoresistor I have it connected to analog pin 1.

@LarryD
This is the current set up I have of the circuit. The pin connected to the LED is actually 12 but I wrote it wrong, my bad.


@StefanL38
Thanks, Funny enough I just hit trust level 1 a few minutes ago and did not know what good that did me lol.

Your LDR has to be a part of a voltage-divider

it is always a good idea to do a five-minute search with google

search with keyword "arduino" and add some more keywords that are relevant

https://www.google.de/search?q=arduino+using+LDR

best regards Stefan

@StefanL38
thanks for the help I did forget to add a resistor to create the voltage divider, but the value is still not being read. Sadly I have spent longer than 5 minutes googling terms. I am just having finding or even understanding the issue.

image

@LarryD
I just added the resistor in so that it does that and the value is still not being read by the A1 pin.
I also just checked it with my multi meter so that I know the power is fluctuating. Its just not being read.

There are always two parts:

  1. the hardware
  2. the software

you can search for a software-error infinitely if it is a hardware-bug.

So you have to narrow down the problem through minimising the system
In your case this means:

take the most tiniest program that does nothing more than print the value of analogRead to the serial-monitor to check if the hardware is working as expected.

This means loading a different code into the arduino-IDE and uploading it.
Uh this takes extra-time :frowning: I want to proceed fast !
Well - how much time did you spend trying this trying that?

well invested time to check the hardware with a small testprogram that is wel known for working good

after that: learn how to debug
Uh this takes extra-time :frowning: I want to proceed fast !
Well - how much time did you spend trying this trying that?

In the long run it turns out that doing things rather slowly is well invested time
to finish your project faster.

best regards Stefan

We don’t know what “printing the same number” means. The random number? The analog value? You can copy the text from the output window and post it as code. That can help us (sometimes).

The random number will never change. It is in the setup() routine which is only executes at program start.

When you change a program you’re asking for help with, please post the entire, current program in your response, leaving each iteration in its original place so we can follow any evolution.