Tested circuit and sketch (attached) as shown using 2 relays and one LED.
Runs great, but each time i restart the circuit (turn power on), the 2 relays activate for about 2 seconds, then the programmed sequence runs.
The relays have the quirk of being active when driven LO.
I have run the sketch with combinations of relay outputs being set HIGH and LOW and the NC/NO contacts being changed appropriately.
I have powered up the Mega and the relay board separately and in different order.
No matter what, the relay board does the initial 2 sec activation on it's own.
The LED, driven directly from the Mega, also flashes on.
So I'm assuming it's the sketch that is the issue.
(schematic doesnt show Mega pin use accurately...sketch does)
The problem is that, in full use, 18 servos and 9 LEDs will be used. I can't have all that power draw on startup. In actual use, the Mega will be powered by a fixed 8VDC supply on the Vin pin.
I need to know how to start and stop this sequence without the initial output event.
Advice appreciated...
void setup() {
pinMode(2,OUTPUT); //heat
pinMode(4,OUTPUT); //LED
pinMode(6,OUTPUT); //air
}
// basic test with loop for 1 cannon
void loop() {
digitalWrite(2, LOW); //heat on, stays on until write high
delay(3600); //preheat time
digitalWrite(6, LOW); // air on, stays on until write high
delay(100); //
analogWrite(4, 150); // flash sequence for LED starts
delay(50); //these delays don't seem to effect pins 2 heat or 6 air
analogWrite(4, 0);
delay(15);
analogWrite(4, 150);
delay(75);
analogWrite(4, 0);
delay(15);
analogWrite(4, 150);
delay(50);
analogWrite(4, 0);
delay(15);
analogWrite(4, 150);
delay(50);
analogWrite(4, 0); //LED flash sequence ends
delay(105);
digitalWrite(2,HIGH); // heat goes off
delay(100);
digitalWrite(6,HIGH); // air off after heat goes off - clears barrel
delay(20000); //20 sec to start again.
}
What is circuit you have driving the relays ?
The problem is caused by the state of the digital outputs when the Arduino boots up, and circuit mods may well fix it .
It’s come up before , not sure of the fix, but to save time for an advisor , put up your relay drive circuit .
Setting your outputs to the desired state in setup will help as the second item in loop is a long delay . When you set pinmode as output , the outputs go low as default .
When an arduino boots up, the pins power up as INPUTS which means they float. If your relays are active low, you will want to install pull-up resistors from between your control pins and +5V so they relay powers up HIGH which means it is off.
Also, If you wiring diagram is correct, you do not have any common ground between the arduino and the relay board. You should have all of your grounds connected together (Arduino to ground of 5V regulator and ground of 12V regulator)
If an I/O low activates the relay, set it to high before declaring it as output and vise versa. The code later contains delay(x) which might prolong wrong activities.
digitalWrite(2, LOW); //heat on, stays on until write high
delay(3600); //preheat time
digitalWrite(6, LOW); // air on, stays on until write high
delay(100); //
@DanLRC
Comment out all code except the code running those two relays and make that work as You want. Use Serial.print for test printings like "Now I am here, Now I am there".
All due, I guess, to these relay boards being pre-set active LOW.
Beginning to realizing that there is so much already posted about things Arduino that learning to search is as important as learning to write sketches. Again, thank you for the help.
The INPUT_PULLUP approach worked perfectly. Thank you.
BUT...with these normally --"on" when LOW/ "off" when HIGH"-- relays, I totally confused myself when looking at the whole system of 18 separate relays and the power they would consume to maintain the needed contact status. When to run LOW or HIGH? Use N/O or N/C contacts? Pin, board and relay current limits?
So I made a table that made it clearer for me. It's the attached PDF with explanation. Look right/wrong? Helpful or not? Any additional advice?
With the option I chose, the sketch opens with all relays low, so to prevent them all bouncing "on" at once when the board powers up, I used the INPUT_PULLUP as suggested. Easy solution..thank you.
Arduino pin current (sink) is ~2mA (not 5mA) for common relay boards with optocouplers.
There is an opto LED (~1.2volt), and indicator LED (~1.8volt) and a 1k resistor in series.
The remaining 2volt across the 1k resistor results in ~2mA drive current.
Apart from the ~75mA relay coil current (that should come from a seperate 5volt supply).
Leo..