Thread: Problem with reading a photoresistor from an Arduino Uno

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    Problem with reading a photoresistor from an Arduino Uno

    I am relatively new to C++ coding. Recently we were given an assignment to connect an Arduino uno to a photoresistor and code it as a counter. We put 5V to a photoresistor with a wire leading to an analog pin, followed by a 10K ohm resistor going back to ground. The code itself wasn't too tricky, but it miscounts. If you keep your hand over the resistor it continues to count. Any tips to eliminate this problem and also do away with the delay?

    Code:
    /*
     Photoresistor object detector.
    
     Detect when an object blocks light to a photoresistor, and count
     the number of times this happened.
    
     The circuit:
     * Photoresistor voltage divider connected to analog pin 0.
    
     */
    
    // These constants won't change. They're used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog pin the sensor is connected to 
    int sensorValue = 0; // Value of the sensor reading (0 to 1023)
    int count = 0; // Number of times an object blocked the light
    
    void setup() {
     Serial.begin(9600); // initialize serial communications at 9600 bps
    }
    
    void loop() { // read the analog in value.
     sensorValue = analogRead(analogInPin); // Recieve the sensor value
    
     if (sensorValue < 750) {  // 
       count = count + 1; // Increment the count
       Serial.println(count); // Print the count and go to the next line
     }
     delay(100);  // 
    }

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Think about it, every 100ms you check if the sensorValue is less than 750, and if it is so - you increment the counter. Everything works as it should.

    You could however check if the sensor value the last time you checked was also less than 750 and if that was the case - leave the counter alone.

    So, how do you check if something happened in the past?

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    To tell you the truth I am not sure, not saying our instructors have not covered it. However, they do have a history of leaving things out. I attempted using while or for loops but I feel as if I am just taking shots in the dark.

  4. #4
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Think about... hm, alphabet. What's the first letter of it? You probably answered that correctly. Now, how did you know that?
    Think about your mother, what is her name? You probably answered that correctly. How did you know that?

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    Obviously I need to access the memory of the previous if statement that declared "if (sensorValue < 750)" right? How do I go about this?

  6. #6
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    There is no memory of a previous statement to even access like that. What I was alluding to is - you have to store that fact in memory.

    You need to create a variable that will signify if the last time the loop iterated the sensor got enough light deficiency to trigger your condition. I know you can do it - in fact, I don't know if I can explain it further without simply giving you the answer outright. That wouldn't earn you EXP in problem solving though.

    Here, try to insert your own words in the source code. If they don't easily translate to C, then think about how you can split your solution to more explicit steps and repeat until they do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with A star problem in C for arduino
    By cesalyst in forum C Programming
    Replies: 6
    Last Post: 04-02-2016, 01:49 PM
  2. C and Arduino
    By bamwels in forum C Programming
    Replies: 11
    Last Post: 08-21-2013, 09:20 PM
  3. Reading button input on Arduino board
    By jimbolad in forum C Programming
    Replies: 5
    Last Post: 03-08-2013, 01:19 AM
  4. arduino
    By Annonymous in forum Tech Board
    Replies: 7
    Last Post: 05-29-2012, 11:13 PM
  5. Arduino programming
    By Terese in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2010, 01:03 PM

Tags for this Thread