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:
- Wave hand in front of ON sensor. Motor starts turning.
- Motor turns for 10 seconds unless something trips one of two OFF sensors (OFF1 and OFF2).
- 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);
}