Halloween candy dispenser. Hardware or code problem?

I've tried many things so to keep the post short(er) I'll just give the latest. I'm not even sure I'm posting in the right place.
BACKGROUND: I'm the world's second worst programmer and relatively new to arduino. I'm making an automatic candy dispenser for Halloween. About a month ago I got the software working with an LED in place of a motor/relay combo and everything worked perfectly. Then I modified a device I already had to dispense the candy. About a week ago I married the electrical and the mechanical and it didn't work. I've been working on getting this functional all week and with help from this forum, I thought I had it, but no. After trying many things, basically the problem is the same. The code works with LEDs but not with the motor.

HOW IT'S SUPPOSED TO WORK:

  1. Wave hand in front of ON sensor. Motor starts turning.
  2. Motor turns for 10 seconds unless something trips one of two OFF sensors (OFF1 and OFF2).
  3. If it times out twice consecutively (i.e. no OFF sensors tripped), turn on the LEDs and disable the device.

HOW IT'S ACTUALLY WORKING:

I don't know how its actually working.

So post the well formatted code in code tags, provide a schematic, and describe what actually happens.

I'm guessing if it worked without a motor and now does not work with a motor that you might be trying to run the motor from a GPIO pin or from the MCU itself, thus the want for a wiring diagram.

I've tried many things so to keep the post short(er) I'll just give the latest. I'm not even sure I'm posting in the right place.

BACKGROUND: I'm the world's second worst programmer and relatively new to arduino. I'm making an automatic candy dispenser for Halloween. About a month ago I got the software working with an LED in place of a motor/relay combo and everything worked perfectly. Then I modified a device I already had to dispense the candy. About 10 days ago I married the electrical and the mechanical and it didn't work. I've been working on getting this functional all week and with help from this forum, I thought I had it, but no. After trying many things, basically the problem is the same. The code works with LEDs but not with the motor.

HOW IT'S SUPPOSED TO WORK:

  1. Wave hand in front of ON sensor. Motor starts turning.
  2. Motor turns for 10 seconds unless something trips one of two OFF sensors (OFF1 and OFF2).
  3. If it times out twice consecutively (i.e. no OFF sensors tripped), turn on the LEDs and disable the device.

When it failed, I took a step back and started with step 1. above. That seems to work. Then to step 2, which is where I am now.

HOW STEP 2 IS ACTUALLY WORKING:
When I use the code below and change the word "pinRelay" to "pinLED" thereby causing the output to use D12 to light the LEDs (instead of D8 for the motor relay)everything works as it should, meaning the lights stay on for about 9 seconds and goes out unless I trigger either OFF sensor. But when I use "pinRelay" and I trigger the ON sensor, it turns the motor forever unless I trigger an OFF sensor, no time out.

Because the code seems to work with LEDs but not the motor, I assume this is a hardware problem. I figured there must be a difference between grounding an LED to turn it on and grounding the relay to turn it on. I thought maybe the pin CAN'T go HIGH to turn off the motor. But it the motor goes off when I trip the OFF sensors. I've tried many things, but can't seem to get it to time out. Hopefully, I can successfully attach the schematic.

NOTES on hardware. The relay only draws 17mA, so at first I was just placing the coil of the relay across the D8 pin (all logic is LOW, meaning everything goes ON when brought LOW), but I read somewhere here that all inductances should be removed from the small signal and a coil has inductance so I resorted to the transistor powering the relay powering the motor method, which seems silly to me. Operation was unchanged. The 12V is provided by a benchtop power supply.

Sorry for the long post but believe it or not, this is much shorter than it could be. Only one day left till Halloween. I hope someone can help.

//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;
const int pinLED = 12;


//sets initial values
int ON = 1;
int OFF1 = 1;
int OFF2 = 1;
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 startTime = 0;
unsigned long currentTime = 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(pinLED, OUTPUT);        //pin 12 is the red LED MIL
  digitalWrite(pinLED, HIGH);     //sets initial value of the MIL
  digitalWrite(pinRelay, HIGH);
  digitalWrite(pinIR_ON, HIGH);
  Serial.begin(9600);
}


void loop()
{
  if (digitalRead(pinIR_ON) == LOW)
  {
    digitalWrite(pinRelay, LOW);
    startTime = millis();
    while (millis() - startTime < period)
    {
      counter++;
      Serial.println(counter);
      digitalWrite(pinRelay, LOW);
      if (digitalRead(pinIR_OFF1) == LOW || digitalRead(pinIR_OFF2) == LOW)
      {
        digitalWrite(pinRelay, HIGH);
        break;
      }
    }
    digitalWrite(pinRelay, HIGH);
  }
  digitalWrite(pinRelay, HIGH);
}

What do you see for serial output?

Please see similarly titled version. I accidentally submitted this prematurely and when I tried to edit, the code tags were gone and basically chaos ensued. Sorry.

Good question. I put a counter in the while loop because I figured it might not be entering it and it does start counting and then encounters an error and just stops.

Topics merged

I have no idea why you started a second topic on the same subject

Do not do it again

Because I accidentally submitted the first one as I said when I reported it to the moderator.

I tried to edit the original, but did not see code tags in that one and needed to submit. Sorry your majesty.

profozone:
Good question. I put a counter in the while loop because I figured it might not be entering it and it does start counting and then encounters an error and just stops.

I wonder if you have a power issue resetting your Arduino. I would print something in setup to check.

I wouldn't be surprised. For a while when I had the power supply off, but I plugged into the arduino with the USB cable, I could see the meter on the power supply go up a bit. Also, when I ran the code with the supply on and the USB plugged in to use the serial monitor, I noticed that when it wanted to keep driving the motor and I turned off the supply, the motor was still trying to be driven off of the USB somehow. That is why I put the diode D2 into the circuit. I thought it might be more appropriate after the 7805 regulator, but then I would have only had 4.5V due to the drop in the diode.

Anyway, since then, I haven't had that problem.

How do I check the power supply by printing something in the setup section? Sorry. I don't really get that.

If the Arduino is getting reset it will run setup again so printing something in there will let you see that it is happening.

Ah, I see. Thank you. I'll give that a try.

Ok, it only went through the setup once. So that seems normal.

I should mention that I tried driving it off of the transistor alone. Data sheet seemed to imply it might handle it, (max 200mA continuous) the motor only draws 98-120mA without jamming. The motor didn't turn on and the transistor got hot. It seemed to work better without it so I put it back to it's original configuration without the transistor.

Well I don't see a way to add a 'new' schematic. So D8 connects to the -coil of the relay and the +coil goes straight to 5 volts.

Performance is seemingly random. I triggered the ON sensor and the motor kicked on for a second. I triggered it again and it ran forever and now the OFF sensors don't shut if off. Think I should try a new Nano?

You have a diode to suppress the effects of the collapsing field on the relay. I would have expected to see one on the motor too.

Come to think about it, I would not want the motor sharing a common ground with the Arduino either.

Well I don't see a way to add a 'new' schematic. So D8 connects to the -coil of the relay and the +coil goes straight to 5 volts.

Performance is seemingly random. I triggered the ON sensor and the motor kicked on for a second. I triggered it again and it ran forever and now the OFF sensors don't shut if off. Think I should try a new Nano?

If you go back to using the led, does the code work properly?

If you are having arduino, sensor or logic problems, then those need to be sorted out before getting back to the motorized dispensor.

Is you are indeed back to a working configuration, then for trouble shooting, I recommend you separate operating the relay from operating the motor. If you use an led instead of the motor on the secondary side of the relay can you turn it on and off with the relay?

wildbill:
Come to think about it, I would not want the motor sharing a common ground with the Arduino either.

That's what I was thinking, but I have 12V at the location and need 5V for the arduino, so. . . voltage regulator. Regarding the diode, I figured the motor was isolated by the relay.
I could separate the motor ground and power the nano with batteries. It only has to function for a few hours, right?

profozone:
That's what I was thinking, but I have 12V at the location and need 5V for the arduino, so. . . voltage regulator. Regarding the diode, I figured the motor was isolated by the relay.
I could separate the motor ground and power the nano with batteries. It only has to function for a few hours, right?

Certainly worth a try.

cattledog:
If you go back to using the led, does the code work properly?

If you are having arduino, sensor or logic problems, then those need to be sorted out before getting back to the motorized dispensor.

Is you are indeed back to a working configuration, then for trouble shooting, I recommend you separate operating the relay from operating the motor. If you use an led instead of the motor on the secondary side of the relay can you turn it on and off with the relay?

I didn't go back to the LEDs, but I can try that.
That's a great idea (subbing LED for just the motor). I'll try that.
My guess is that Bill is on the right track with the grounds though, since I have seen evidence of the USB power and the 12V mixing.
My set-up is starting to look shabby with all of the changes.

wildbill:
Come to think about it, I would not want the motor sharing a common ground with the Arduino either.

Yes, you do. Except for exceptional circumstances, you always want a common ground.

Read This.