0

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;
   }
}
  • Make sensorStatus static and give it a default value? – Majenko Jul 8 at 15:54
  • It does seem to stop it from taking garbage value and making it not work without input from sensor by setting it as 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. – Moeed Rehman Masood Jul 8 at 16:07
  • So change the sensorStatus back to HIGH only when a button is pressed? – Majenko Jul 8 at 16:10
  • I set sensorStatus to HIGH right after the digitalWrite(relayControl1_1, HIGH); in the else part of the buttonStatus 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 adding delay(2000); digitalWrite(relayControl1_1, LOW); after it and it works exactly how I want. Thanks a lot for the help, kind stranger. – Moeed Rehman Masood Jul 8 at 16:42
  • @Majenko can you post your comment as an answer so the OP can accept it? As it is now, I had to read the question and 4 comments to figure out that the problem has already been solved. – Duncan C Jul 8 at 17:00
1

Thanks to the help from @Majenko♦ I was able to fix the issue.

1 - int sensorStatus; should have been set to static int sensorStatus = HIGH; to make the program remember the value of the sensor.

2 - sensorStatus = HIGH; should have been right after digitalWrite(relayControl1_1, HIGH); in the buttonStatus conditional else part to make the program set the sensorStatus back to HIGH correctly.

3 - Unrelated to the question posted, doing the above steps made the program run the motors continuously unless they were stopped by an obstacle from the IR sensor. This was fixed by adding delay(2000); digitalWrite(relayControl1_1, LOW); after the sensorStatus = HIGH; mentioned in the 2nd step.

Source Code after fixing:

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() {

  static int sensorStatus = HIGH;

  if (digitalRead(inSensor) == LOW) {
    sensorStatus = LOW;
  }

  if(sensorStatus == LOW) {
      buttonStatus1 = digitalRead(inButton1);
  
      if(buttonStatus1 == HIGH) {
        digitalWrite(relayControl1_1, LOW);
      } else {
        digitalWrite(relayControl1_1, HIGH);
        sensorStatus = HIGH;
        delay(2000);
        digitalWrite(relayControl1_1, LOW);
      }
    
      buttonStatus2 = digitalRead(inButton2);
    
      if(buttonStatus2 == HIGH) {
        digitalWrite(relayControl2_1, LOW);
      } else {
        digitalWrite(relayControl2_1, HIGH);
        sensorStatus = HIGH;
        delay(2000);
        digitalWrite(relayControl2_1, LOW);
      }
   }
}
| improve this answer | |
  • couple of observations ... it appears that your code does not work as described ... you said obstacle passes in front of the IR sensor ... your code never checks for obstacle passing sensor, it only checks for obstacle blocking sensor ....... the program line digitalWrite(relayControl1_1, LOW); gets called regardless of the state of buttonStatus1 ... put digitalWrite(relayControl1_1, LOW); after the if block .... same kind of thing in the other if block – jsotola Jul 9 at 1:28
  • the code would be clearer if sensorStatus was boolean named coin or paid – jsotola Jul 9 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.