Candy dispenser possible "while" loop problem.

BACKGROUND:

  1. I am relatively new to Arduino and I consider myself to be the world's second worst programmer.
  2. I believe there may be multiple problems with my code so this reduced portion is the first part.
  3. The project is an automated candy dispenser using an Arduino Nano (knockoff).
  4. Here's how the whole thing is supposed to work:
    Wave hand under "ON" sensor. Motor turns until falling candy triggers one or both "OFF" sensors. If this does not happen within a set amount of time (10 sec.), turn off motor. If two consecutive attempts are made and the OFF sensors are not triggered, turn on red MIL (Malfunction Indicator Lights) and disable motor.
  5. All three sensors are the same TCRT5000 IR sensors. The motor is a geared 12V DC motor so I have a 12 V power source and a 5V regulator for the Nano. A relay is used to isolate the 5V control from the 12V motor. All logic is LOW = ON. I have a schematic if you need to see it.
  6. I prototyped the whole thing with an LED in place of the motor until it worked perfectly, but when I married it with the hardware. No bueno! There were lots of issues. After pulling a few precious hairs out, I decided to just back up and take it one step at a time. I started with the following code:

WHAT I EXPECTED:
Wave hand in front of ON sensor. Motor should turn until something trips one or both of the OFF sensors.

WHAT I GOT:
First wave produced the desired effect. The second wave produced the desired effect. The third and all successive waves produce motor movement for less than 3 seconds, without any candy triggering the OFF sensors.

There are some extra definitions at the beginning that I left in because I wanted to expand the code after I solved the first problem. But clearly I haven't even done that.

So the questions are:
A. Why is the motor stopping when there is no OFF trigger?
B. Why is ok on the first couple of tries, but then stops working?

I hope this meets all of the rules. Thanks in advance. Here's my code:

//Candy Dispenser Control 10/25/20
//Relay test using 3 TCRT5000 IR sensors


//defines which pins are used
const int pinIR_ON = 2;
const int pinIR_OFF1 = 4;
const int pinIR_OFF2 = 7;
const int pinRelay = 8;


//sets initial values
int IR_ON_valueD = HIGH;
int IR_OFF1_valueD = HIGH;
int IR_OFF2_valueD = HIGH;
int period = 10000;         //sets how many milliseconds motor should turn without shutoff
int counter = 0;            //used to count how many tries before motor shuts off
unsigned long duration = 0;






void setup() {
  //sets pins to inputs and outputs
  pinMode(pinIR_ON, INPUT);
  pinMode(pinIR_OFF1, INPUT);
  pinMode(pinIR_OFF2, INPUT);
  pinMode(pinRelay, OUTPUT);
  pinMode(12, OUTPUT);        //pin 12 is the red LED MIL
  digitalWrite(12, HIGH);     //sets initial value of the MIL
}


void loop() {


  if (IR_ON_valueD == LOW)
  {
    while (IR_OFF1_valueD == HIGH && IR_OFF2_valueD == HIGH)
    {
      IR_OFF1_valueD = digitalRead(pinIR_OFF1);
      IR_OFF2_valueD = digitalRead(pinIR_OFF2);
      digitalWrite(pinRelay, LOW);
    }
  }
  else
  {
    digitalWrite(pinRelay, HIGH);
  }


  IR_ON_valueD = digitalRead(pinIR_ON);
  IR_OFF1_valueD = digitalRead(pinIR_OFF1);
  IR_OFF2_valueD = digitalRead(pinIR_OFF2);
}

So something like this?

void loop()
{
  if (digitalRead(pinIR_ON) == LOW)  // Wave hand in front of  ON sensor.
  {
    digitalWrite(pinRelay, LOW); // Motor should turn...
    
    while (digitalRead(pinIR_OFF1) != LOW && digitalRead(pinIR_OFF2) != LOW)
    {
      // ... until something trips one or both of the OFF sensors.
    }


    digitalWrite(pinRelay, HIGH);   // (Motor should then stop.)
  }
}

Wow, such a small difference. I'll give it a try. Thank you for the response.

Hi,
Not sure if;

int IR_ON_valueD = HIGH;
int IR_OFF1_valueD = HIGH;
int IR_OFF2_valueD = HIGH;

Should be;

bool IR_ON_valueD = HIGH;
bool IR_OFF1_valueD = HIGH;
bool IR_OFF2_valueD = HIGH;

Tom... :slight_smile:

johnwasser,

That totally worked. I'm not sure why mine didn't but thank you so much!

On to step 2!