Timing for Triggering a Solenoid

Hello.

I am relatively new to the Arduino.

I have implemented the schematic of the solenoid driver (i cannot post the link because this is my first post, but it is on the Arduino websie )

by using a doorchime solenoid. I can turn on the solenoid by pressing a key on the keyboard and setting the digital out high. If I want it to hit again, I must set the out to low first. It's fun and was very easy to implement following that schematic.

What I want to try now is trigger the doorbell at regular time intervals. I suppose I could do something similar to the blink sketch, but I want the timing to be as accurate as possible. From some examples, I've seen that using the delay function is pretty reasonable at timing; however, from what I have seen, whenever you use the delay, you are pausing the entire program. Is there a way for me to create a regular triggering of the solenoid without having to pause the program?

The reason why I'm asking is because I want to use the same Arduino for other things like driving a servo. If the program pauses, the servo will pause as well. I would like to have the servo running as smoothly as possible while the solenoid is triggered.

Thank you
Aaron

I think the BlinkWithoutDelay sketch is going to provide the prototype code you're looking for.

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons

Thank you

I basically put the code I was using inside this and it seems to work well.

This is the essential portion:

if (millis() - previousMillis > interval && isActive ) {
previousMillis = millis();

if (pinState == LOW)
pinState = HIGH;
else
pinState = LOW;

digitalWrite(pin,pinState);

}

However, one thing that I believe I will have to worry about is testing how often this might drift. If we imagine that we have two arduinos running this code, I'm pretty sure we'd hear that they might drift out of sync if we use this code.

The millis() function gives you the time since you've started running the program. So perhaps, in order to ensure rhythmic structure, I think that previousMillis must be set to something that is the closest multiple of the repetition interval since the program started.

Yes, there will be drift between two Arduinos unless you take pains to synchronize them. You can either designate one the "master" and the other the "slave" such that the master drives a synchronization signal, then the slave waits for it. Alternatively, you could have a central PC that sends a synchronization signal periodically to any number of Arduinos and they all synchronize by waiting for this signal.

Either way, you're talking about more complexity here and some thought (and perhaps more details on what your desired precision is) will be required.

--
The Quick Shield: breakout all 28 pins to quick-connect terminals

One way to easily sinc the two is by using the external interrupts pin 2 or 3. You could use the external interrupt to tell the slave to trigger the solenoid, that way there is only one timing program (that on the master).

Thank you for the advice. Having one as a reference time keeper does seem like a necessity.

I have a question regarding the drift. Is it due to code, or is it due to hardware?

What if the essential portion of the code were instead something like this:

if (micros() - previousMicros > interval && isActive ) {

previousMicros = previousMicros + interval;
.
.
.
}

While we are not guaranteed to be accurate because we are always technically going to be late ( triggering after the interval time), it seems as though it will always be consistent on one end (precise?), in that the previous time is always increasing by the interval amount whereas in the older code, the previous time was set to whatever the current time was (which most likely would not fall on an interval).

Using this, the arduinos probably would not drift as far out of time, right? Or would the clocks on the arduinos be that different so that it would drift anyway over time?

The drift will be due to different clock frequencies, although the code revision you proposed will definitely help.

If one oscillator is 16 MHz+0.01% (that's 100 parts per million, a common specification for crystals) and the other is 16 MHz then that is enough to cause noticeable drift over time. When one processor counts off 1 second, the other will have counted off 0.9999s. After 10001 seconds (2.78 hours) they will be separated by 1 second.

--
Need a custom shield? Let us design and build one for you.

The main reason I am suggesting external interrupts is you do not need to worry about the time on the slave board. Therefore the master controls the "event" timing The master board triggers your local solenoid and the external interrupt, the slave board "sees" the interrupt change and thus executes the slave interrupt coded function. The loop usually changes the state of a variable the slave then can call to action the triggering of the slave solenoid. In the reference http://arduino.cc/en/Reference/AttachInterrupt you will see it is designed to do what you are looking for. That is, if I understand your needs :slight_smile:

I think I see what you're saying, Snowman. Only the master one is doing any time keeping; the rest are just responding to messages that the master one sends out. I wasn't expecting a solution like that actually. I was thinking that it might actually be an interesting problem to try and make two clocks sync, but this approach really is about only having one clock.

I think I would like to attempt two approaches, one with the interrupt, and the other with actually trying to get the clocks to sync because the music that results could be very different in both scenarios. Especially if one were to expand it to more than 2 arduinos, in the interrupt example you're saying, it seems like a centralized approach with everyone syncing to one arduino, wherase if it was this thing where we try to adjust the clocks, perhaps you might get some phasing effects, but hopefully settle on something that is steady, kindof like how people actually play: no one person is a perfect time keeper, but a group can still settle on something that works.

What does "volatile" mean in the example on the attachInterrupt page?

aaron
http://www.arduino.cc/en/Reference/Volatile
:slight_smile: