IF statement - triggered by a photosensor issues.

Hello thanks for the help.

Issue: Photosensor needs to wait for the desired value to trigger an IF statement after pushing a button. However the photosensor is storing the value at the moment when the button is pushed, the value seems to be locked/stored after the button push, and does not read the live value after pushing the button.

-Pushing the button while the sensor is covered gets the desired result (needs to wait for the value to trigger)
-Pushing the button with the sensor not covered will not trigger the IF statement, covering the photosensor or not does nothing after button push to trigger the IF statement.

-- I would like the button to be pushed and then it wait for the desired value from the photosensor to trigger the rest of the code. After the button push it may be 0.5 seconds to 30 seconds before the photosensor gets the desired value to move on with the rest of the code within the IF statement.
Basically push 'go' button - 'push motor' runs, waits for desired photosensor value to move on.

The rest of the code works as desired. Just an issue with line 90

Based on Arduino Mega
photosensor 5V: Light Sensor Module | Makerfabs
Canada, 120V, using arduino IDE

I am also able to read the photosensor live values as desired by serial monitor and basic code to read that pin only.

Thanks so much for any input on this, been trying for ever to try and solve this issue.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>

/////////Pin Assignment////////////

#define return_button  2 //return button
#define go_button  3 //go button
#define photosensor A2           //Photoresistor 
#define temp_sensor  59      // Sensor temp
#define left_pot A1           // left potentiometer
#define right_pot  A0      // right potentiometer
#define pot  A3              // potentiometer located in actuator
#define cooler  58            // cooler

#define bottom_motor_in1 4
#define bottom_motor_in2 5
#define bottom_motor_ctrl 9
#define push_motor_in1 7
#define push_motor_in2 8
#define push_motor_ctrl 10
#define motor_shield_enable 6
#define laser A12               //laser



float temperature_val = 0;      //Variable to store the temperature in
int temperature_lowerLimit = 23;      //define the lower threshold for the temperature
int temperature_upperLimit = 24;      //define the upper threshold for the temperature
int counter = 0;
boolean break_flag = false;

OneWire oneWirePin(temp_sensor);
DallasTemperature sensors(&oneWirePin);
Servo flapper;                //servo

void setup() {

  // motor shield info: makerfabs OAS0000HB
  // Bottom motor controll pin initiate;
  Serial.begin(9600);
  pinMode(go_button, INPUT);
  pinMode(return_button, INPUT);
  pinMode(bottom_motor_in1, OUTPUT);
  pinMode(bottom_motor_in2, OUTPUT);
  pinMode(bottom_motor_ctrl, OUTPUT); // Speed control

  // Push motor controll pin initiate;
  pinMode(push_motor_in1, OUTPUT);
  pinMode(push_motor_in2, OUTPUT);
  pinMode(push_motor_ctrl, OUTPUT);  // Speed control

  //Enable the Motor Shield output;
  pinMode(motor_shield_enable, OUTPUT);
  digitalWrite(motor_shield_enable, HIGH);

  pinMode(laser, OUTPUT); //laser
  flapper.attach (46);                      //flapper(servo) pin 46
  pinMode(photosensor, INPUT);

  //cooler
  pinMode(cooler, OUTPUT);
}


void loop() {

  flapper.write (5);                                            //flapper close ****to be at this position after reset.

  //go button
  if (digitalRead(go_button) == LOW) {                          //***GO button pushed
    {
      break_flag = false;
      Serial.println("Go button pressed");
      if (break_flag == false)

        digitalWrite(laser, LOW);                                 //laser on
      flapper.write (65);                                       // degrees flapper open
      delay(50);                                                //delay to allow led on and flapper to move,
      sensor_checker();
      Serial.println("flapper opened");
      digitalWrite(motor_shield_enable, HIGH);                    //push motor down
      analogWrite(push_motor_ctrl, analogRead(right_pot) / 4);    //speed via POT
      digitalWrite(push_motor_in1, HIGH);
      digitalWrite(push_motor_in2, LOW);
      Serial.println("Push motor runs");


      sensor_checker();
      if (analogRead(photosensor) > 200) {                                  // 400>=450       if the sensor goes above 200  (lit reads: 115-118, Blocked is 297-307...
        Serial.println("photosensor tripped");
        digitalWrite(laser, HIGH);                                 //laser off after photosensor is tripped.
        Serial.println("bottom motor on");
        analogWrite(bottom_motor_ctrl, analogRead(left_pot) / 4);       //bottom motor on
        digitalWrite(bottom_motor_in2, HIGH);
        digitalWrite(bottom_motor_in1, LOW);
        delay(1200);
        sensor_checker();
        delay(1000);
        sensor_checker();
        delay(1000);
        flapper.write (5);                                     //flapper close
        Serial.println("flapper close");
        Serial.println("push mtr up");
        digitalWrite(motor_shield_enable, HIGH);
        analogWrite(push_motor_ctrl, 255);                          //push motor up @ full speed
        digitalWrite(push_motor_in1 , LOW);
        digitalWrite(push_motor_in2, HIGH);
        delay(250);
        sensor_checker();
        analogWrite(bottom_motor_ctrl, 0);                              //bottom motor off
        digitalWrite(bottom_motor_in2, LOW);
        digitalWrite(bottom_motor_in1, LOW);
        delay(300);
        sensor_checker();
        digitalWrite(motor_shield_enable, LOW);                                 //shield off
        analogWrite(push_motor_ctrl, 0);                                      //push motor off
        digitalWrite(push_motor_in1, LOW);
        digitalWrite(push_motor_in2, LOW);

        flapper.write (5);                                        //degrees flapper close


        //potentiometer sensor function
        if (analogRead(pot) > 900)                                  // if the potentiometer goes below 600 (BOTTOM THEN RETURN TO TOP)...
        {
          digitalWrite(motor_shield_enable, HIGH);
          analogWrite(push_motor_ctrl, 255);                        //push motor up full speed
          digitalWrite(push_motor_in1, LOW);
          digitalWrite(push_motor_in2, HIGH);
          break_flag = true;
        }
      }
      sensor_checker();
    }
  }


  //Return button
  if (digitalRead(return_button) == LOW) {                              //*****Return button pushed
    Serial.println("Return button pressed");
    digitalWrite(motor_shield_enable , HIGH);
    analogWrite(push_motor_ctrl, 255);                                  //push motor up full speed
    digitalWrite(push_motor_in1, LOW);
    digitalWrite(push_motor_in2, HIGH);
    break_flag = true;
  }



  sensor_checker();
}







void sensor_checker() {
  //cooler/temperature function
  sensors.requestTemperatures();
  temperature_val = sensors.getTempCByIndex(0);

  if (temperature_val <= temperature_lowerLimit) {                //lower limit
    digitalWrite(cooler, LOW);
  }
  else if (temperature_val >= temperature_upperLimit) {             //upperlimit
    digitalWrite(cooler, HIGH);

  }
}

Also tried this variation of code with the same result, this time Code lines 26, 87, 89, have same result as the previous code.

int photosensor_val = 0; //line 26
photosensor_val = analogRead(photosensor); //line 87
if (photosensor_val > 200) { //line 89

What do you see when you print the value(s) being tested in the if statements before you test them ?

Are they what you expect ?

I would suggesting writing a small, test sketch using the same pins are your sketch that does nothing but report the values of the photo sensor. Run it and vary the amount of light present. Does the sketch respond as expected?

pinMode(go_button, INPUT);
pinMode(return_button, INPUT);
if (digitalRead(go_button) == LOW) {                          //***GO button pushed

Do you have 10k pullup resistors on pins 2 and 3? If not they will "float" when buttons are not pressed and might cause false LOWS.

blh64:
I would suggesting writing a small, test sketch using the same pins are your sketch that does nothing but report the values of the photo sensor. Run it and vary the amount of light present. Does the sketch respond as expected?

Yes, have a small sketch and worked as expected, responded perfectly.

UKHeliBob:
What do you see when you print the value(s) being tested in the if statements before you test them ?

Are they what you expect ?

Yes values are as expected, i am seeing a value of 80 when open to a window, and a value of 410 when blocked.

Hello, yes Pins 2,3 have resistors and do not give false readings.

JCA34F:

pinMode(go_button, INPUT);

pinMode(return_button, INPUT);





if (digitalRead(go_button) == LOW) {                          //***GO button pushed



Do you have 10k pullup resistors on pins 2 and 3? If not they will "float" when buttons are not pressed and might cause false LOWS.

I believe you have an incorrect line referring to your photosensor. You define the pin initially as A2, and then further on, you have this line.

" pinMode(photosensor, INPUT); "

I believe you want to use an analogRead(photosensor) to find your information, can you try to comment the line above out of the code?

"// pinMode(photosensor, INPUT);"

JBornstein2016:
I believe you have an incorrect line referring to your photosensor. You define the pin initially as A2, and then further on, you have this line.

It isn't incorrect, just superfluous

TheMemberFormerlyKnownAsAWOL:
It isn't incorrect, just superfluous

Former AWOL, I believe the way the code is written currently, is that the analog pin is defined as digital, and therefore would be functioning like a digitalpin. Meaning only HIGH and LOW state. If he has a digitalPin and he is trying to read analog data, wouldn't the issue be that definition?

According to Arduino Reference:

"Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc."

Pins default to inputs, so remming out the pinMode which makes it input has no effect, since it's going to be an input as default.

A digitalRead() of an Ax pin will give a high or low; an analogRead() thereof will give 0-1023.

JBornstein2016:
Former AWOL, I believe the way the code is written currently, is that the analog pin is defined as digital, and therefore would be functioning like a digitalpin. Meaning only HIGH and LOW state. If he has a digitalPin and he is trying to read analog data, wouldn't the issue be that definition?

According to Arduino Reference:
pinMode() - Arduino Reference

"Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc."

You have the source of analogRead; why not see what it does?

Thank you,

-So by writing the line, "pinMode(photosensor, INPUT);" it then would read as a digital pin?

-Deleting that same line would make it read as an analog pin?

Thank you so much.

No, it's an input by default, so the pinMode() as an input is redundant.

If you do a digitalRead() you get a digital reading; do an analogRead() and you get an analog reading.

I am looking to read it as a analog pin, as it seems to be doing, unblocked is 80, blocked reading of 410.

Why is it storing the value at the time of the button push, rather than waiting to get the proper value, (value >200, and up to 30 seconds after pushing the button) to get the desired value.

It seems to be storing/locking the value when 'go' button pushed than using the live value. How can it read live values all the time after pushing the button?

What does your latest code look like? Every time you do analogRead(), you get the "live" value so your description of the problem is difficult to understand.

Your code also looks a bit like you are trying to do everything with one pass through loop(). It would be better to structure it more as a state machine.

State 1: waiting for "go" button pressed
State 2: running "push" motors
State 3: waiting for photosensor value to be appropriate
State 4: turning off motors
State 5: go back to state 1

or something similar to that

Also, you have a lot of delay()s in your code preceeded/followed by sensor_check(). You could combine those to things such that sensor_check() will continuously check until the amount of time has elapsed

//...
  sensor_checker_delay(2000);
//...




void sensor_checker_delay(const unsigned long wait) {
  //cooler/temperature function
  unsigned long start = millis();
  while ( millis() - start < wait ) {
    sensors.requestTemperatures();
    temperature_val = sensors.getTempCByIndex(0);

    if (temperature_val <= temperature_lowerLimit) {                //lower limit
      digitalWrite(cooler, LOW);
    }
    else if (temperature_val >= temperature_upperLimit) {             //upperlimit
      digitalWrite(cooler, HIGH);
    }
    delay(25);
  }
}