This is my first time working on Arduino for my project and I ran into an issue. I'm trying to make a very simple Vending Machine. Coin or any obstacle passes in front of the IR sensor and based on that, the user can either press button 1 or 2 and based on that motor 1 or 2 would run respectively.
The overall logic works exactly how I want it to, the issue I'm facing is that the variable (sensorStatus
) storing the IR sensor value isn't working correctly. Whether anything passes in front of it or not, it lets me press the buttons anyways and the motor starts working.
void loop() {
int sensorStatus;
//this is where im having the issue
if (digitalRead(inSensor) == LOW) sensorStatus = LOW;
if(sensorStatus == LOW) {
buttonStatus1 = digitalRead(inButton1);
if(buttonStatus1 == HIGH) {
digitalWrite(relayControl1_1, LOW);
} else {
digitalWrite(relayControl1_1, HIGH);
}
buttonStatus2 = digitalRead(inButton2);
if(buttonStatus2 == HIGH) {
digitalWrite(relayControl2_1, LOW);
} else {
digitalWrite(relayControl2_1, HIGH);
}
sensorStatus = HIGH;
}
}
If I don't use this logic and store the value of the sensor directly into the variable (sensorStatus
), the whole program seems to be working till I have an obstacle in front of the sensor continuously. I don't want this to happen, I want it to work in such a way that once an obstacle passes in front of the sensor it keeps the value (even after the obstacle is gone) until a button is pressed and after that, it gets rid of the value to repeat the process once again. It seems to be a code logic issue but I can't seem to figure out the logic behind it.
My Complete Source Code:
const int relayControl1_1 = 13; // relay 1 of 1 (input 1)
const int relayControl1_2 = 12; // relay 1 of 2 (input 2)
const int relayControl2_1 = 11; // relay 2 of 1 (input 3)
const int relayControl2_2 = 10; // relay 2 of 2 (input 4)
const int inButton1 = 9; //input button 1
const int inButton2 = 8; //input button 2
int buttonStatus1 = 0;
int buttonStatus2 = 0;
const int inSensor = 7;
void setup() {
Serial.begin(9600);
pinMode(relayControl1_1, OUTPUT);
pinMode(relayControl1_2, OUTPUT);
pinMode(relayControl2_1, OUTPUT);
pinMode(relayControl2_2, OUTPUT);
pinMode(inButton1, INPUT_PULLUP);
pinMode(inButton2, INPUT_PULLUP);
pinMode(inSensor, INPUT_PULLUP);
}
void loop() {
int sensorStatus;
//this is where im having the issue
if (digitalRead(inSensor) == LOW) sensorStatus = LOW;
if(sensorStatus == LOW) {
buttonStatus1 = digitalRead(inButton1);
if(buttonStatus1 == HIGH) {
digitalWrite(relayControl1_1, LOW);
} else {
digitalWrite(relayControl1_1, HIGH);
}
buttonStatus2 = digitalRead(inButton2);
if(buttonStatus2 == HIGH) {
digitalWrite(relayControl2_1, LOW);
} else {
digitalWrite(relayControl2_1, HIGH);
}
sensorStatus = HIGH;
}
}
static
and give it a default value?static int sensorStatus = HIGH;
but it stops it from how I want it to work. I want it to be able to store the value until I press the buttons for the motor even if the obstacle from the sensor is gone. Setting it as static ends up making it only work as long as I have an obstacle in front of the sensor, once the obstacle is gone it seems to lose the value.sensorStatus
to HIGH right after thedigitalWrite(relayControl1_1, HIGH);
in the else part of thebuttonStatus
conditional statements. It seems to work this way but the motor keeps on working until I interrupt it by putting an obstacle in front of the IR sensor. I fixed it by addingdelay(2000); digitalWrite(relayControl1_1, LOW);
after it and it works exactly how I want. Thanks a lot for the help, kind stranger.