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