ASK AN EDUCATOR! “How can I control a security gate with my Arduino?”

Freddie asks:

I have a question about Arduino DC Motor Control:

To make the motor run forward and backwards is not a problem.
Also I am not concerned about the speed of the motor.
The H Bridge configuration is also not a problem.

But, to time the motor on and off seems not to be a regular discussion on the net.

I need to open and close a security gate simply by using a push button (like those tiny square ones always used with arduino diagrams and tutorials).
If the button is pressed momentarily, the motor needs to run one direction for e.g. five seconds and stop in its tracks and must wait for my next button press.
If I press the button again, the motor must reverse direction to its default position (same timing span ) and stop in its tracks again. So, it is purely timed on and off in either direction.

On the net are lots of code available for the control of the H bridge including the button state. But with all of those codes the motor directions are not timed. They simply forward or reverse continuously.

With my limited knowledge of coding in C (Arduino), I some how feel the delay instruction is not the way to resolve the issue (or can it be possible?), but rather to make use of the timer of the Arduino. Either options put me in the dark.

Can you advise?

Absolutely!

Rather then trying to time the Arduino to successfully open and close you gate, I would recommend using limit switches such as these. Use two, one to let the Arduino know that the gate is open, and the other to let it know the gate is closed. Then use a weatherproof pushbutton to tell the Arduino to “turn the motor CCW until the limit switch trips, indicating the gate is open” or “turn the motor CW until other limit switch trips, indicating the gate is closed.”

The other problem I see is that your security gate might run on AC rather then DC. In order for your Arduino to control the direction of an AC motor, you will need more complicated circuitry and it is considerably more dangerous. An option would be to use a AC Reversing Solid State Relay as it can be directly controlled by the microcontroller. Unless you are a trained electrician, I would NOT recommend interfacing your Arduino to an AC source.

Below is a rough schematic for your circuit and a bit of code that should get you started!

Code:

const int activate = 2; //set up our button constants
const int gateOpen = 3;
const int gateClosed = 4;
const int bridgeOneA = 5;
const int bridgeTwoA = 6;
const int enable = 7;
const int statLED = 13;
unsigned long currentTime = 0;
boolean gateState = false; //false = closed true = open
void setup() {
  Serial.begin(9600);
  pinMode(activate, INPUT); //set up I/O
  pinMode(gateOpen, INPUT);
  pinMode(gateClosed, INPUT);
  pinMode(bridgeOneA, OUTPUT);
  pinMode(bridgeTwoA, OUTPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable, LOW); //make sure H-Bridge is off
  pinMode(statLED, OUTPUT); //setup our status LED
}
void loop() {
  if (digitalRead(activate) == HIGH && gateState == false) { //check to see if the button is pressed and the gate is closed
    digitalWrite(enable, HIGH); //enable h-bridge
    digitalWrite(bridgeOneA, HIGH); //configure for CW rotation
    digitalWrite(bridgeTwoA, LOW);
    while(1){ //run motor until switch is tripped
      if (digitalRead(gateOpen) == LOW) { //check switch state
        gateState = true;
        digitalWrite(statLED, LOW); //turn off LED
        digitalWrite(enable, LOW); //disable h-bridge
        digitalWrite(bridgeOneA, LOW); //reset h-bridge config
        break;
      }
      if (millis() > currentTime + 500) { //flash status LED once
        digitalWrite(statLED, HIGH);
        delay(500);
        currentTime = millis();
      }
      else {
        digitalWrite(statLED, LOW);
      }
    }
  }
  if (digitalRead(activate) == HIGH && gateState == true) { //check to see if the button is pressed and the gate is open
    digitalWrite(enable, HIGH);
    digitalWrite(bridgeOneA, LOW); //configure for CCW rotation
    digitalWrite(bridgeTwoA, HIGH);
    while(1){
      if (digitalRead(gateOpen) == LOW) {
        gateState = false;
        digitalWrite(statLED, LOW);
        digitalWrite(enable, LOW);
        digitalWrite(bridgeTwoA, LOW);
        break;
      }
      if (millis() > currentTime + 500) { //flash status LED once
        digitalWrite(statLED, HIGH);
        delay(500);
        currentTime = millis();
      }
      else {
        digitalWrite(statLED, LOW);
      }
    }
  }
}

I haven’t tried out the code, but it should get you started in the right direction. The idea is that when the button is pressed, the Arduino configures and activates the h-bridge, moving the gate until the “open” limit switch trips and stops the motor. Then when the button is pressed again, the Arduino configures and activates the h-bridge, moving the gate until the “closed” limit switch trips and stops the motor again. There is a also small bit of code that flashes the LED during the time the motor is in motion using the mills() counter.

I hope this has helped to steer you in the right direction and good luck with your project!

Don’t forget, everyone is invited to ask a question!

Click here!

“Ask an Educator” questions are answered by Adam Kemp, a high school teacher who has been teaching courses in Energy Systems, Systems Engineering, Robotics and Prototyping since 2005.



NUD Collection

Pt 997
NUD Collection. If you’re working on some anachronistic electronics (or simply are tired of gray/beige/black) these cords are really pretty.

Filed under: wires-cables-etc — by adafruit, posted at 9:31 am


owl project

Signal 02
owl project ::.

Decisions, decisions. Who needs ’em? Why should you have to choose what to make with your iLog? Collect sounds right out of the air, listen to the sound of the light all around you. The iLog touch display has stereo ambient light sensors that automatically detect “Amplitude Modulation” and the brightness of the of ambient light in your surroundings.

This log synth is half Buddha Machine, Half Bleep Labs, Half Wood

Filed under: art — by adafruit, posted at 9:29 am


NEW PRODUCTS (video)


NEW PRODUCTS (video).

Filed under: ask-an-engineer — by adafruit, posted at 8:29 am


ASK AN ENGINEER 4-28-2012 (video)


ASK AN ENGINEER 4-28-2012 (video).

Filed under: ask-an-engineer — by adafruit, posted at 8:28 am


525lbs of weights welded to Shuttle…

Shuttles-Med

Robert writes in…

They welded 525lbs of free-weights to Enterprise to get the the center of gravity right…

Filed under: random — by adafruit, posted at 8:16 am


Girls Who Code

Www.Girlswhoco
girlswhocode.

Founded in February 2012, Girls Who Code is working to educate, inspire, and equip underserved girls aged 13-17 with the skills and resources to pursue careers in science, technology, engineering and math (STEM).

 The Girls Who Code program is an eight-week summer program in New York City designed to introduce high school girls to basic software development skills and is accompanied by yearlong outreach initiatives, mentorship programs, and internship opportunities to realize each participant’s potential.

Filed under: educators — by adafruit, posted at 12:00 am


XBee Configuration for Macintosh

Index-26
XBee Configuration for Macintosh via Rob!

A new configuration utility for Digi International’s XBee radios is available for Macintosh, Windows and Linux platforms from Moltosenso in Italy, and it’s free. According to their web site,Moltosenso Network Manager™ IRON enables the following tasks:

• full support to any API Operation mode (with and without escaped characters)
• get/set of the parameters of Digi International® modules plugged to the PC, both in API and AT mode
• get/set of the parameters of Digi International® modules remotely addressable
• an effective graphic test for RSSI parameter, especially tailored for XBEE™ modules
• firmware upload (local and – where available – remote) for many supported Digi International® modules.

Filed under: xbee — by adafruit, posted April 29, 2012 at 12:00 am


The Greatest Machine That Never Was

The computer was invented in the 30s: not the 1930s, but the 1830s. British mathematician Charles Babbage designed and prototyped a fully functional mechanical computer he called the Analytical Engine, but it was never completed. Now a team in Britain plans to build the machine for display at London’s Science Museum before the 2030s come around.

Filed under: ald — by adafruit, posted at 12:00 am


Inside the Steinway Factory

Pt 986
Inside the Steinway Factory – Photographs – NYTimes.com.

Filed under: art — by adafruit, posted at 12:00 am


Robotic spider weaves web

A three-week old robot at the MIT Media Lab is weaving a cocoon-like structure with a little programming help from humans. Eventually it will be autonomous.

Filed under: robotics — by adafruit, posted at 12:00 am


Interview with The Open Source CEO: Jim Whitehurst

Pt 985
The Open Source CEO: Jim Whitehurst @ TechCrunch.

I next asked Whitehurst “How does the open source culture affect Red Hat internally as a corporation?” Specifically, I wanted to know how have “the tenets of open source” affected management processes? This was, in many respects, the most interesting part of my conversation with him.

“The traditional hierarchical org structure was developed to control, and steward fixed assets,” Whitehurst told me. The traditional hierarchies do not tend to innovate. “When information is your primary product, hierarchy isn’t the best way to drive.”

To illustrate how the open source culture influences everything at Red Hat, Whitehurst told me a story about an early experience as the company’s CEO. When he started, the company had — to his mind — a rather weak mission statement. Coming from a traditional management background, Whitehurst’s first reaction was to do what CEOs all around the world do: gather the executive team, have an off-site retreat with a paid facilitator, and develop a new mission statement. This new statement would then be broadcast to the employees.
“Hold on, sparky,” was effectively what his executive team told him. “You need internal buy-in,” he was told. An internal collaboration site was established — mailing list, wiki, blog — to discuss the mission statement with staffers who were passionate about that issue. Once the infrastructure was put in place, the discussion was entirely self-organized, and about 15 to 20 people really dug in. “Iterate, iterate, iterate” is how Whitehurst described the process.

Filed under: open source hardware — by adafruit, posted at 12:00 am


SHOW-AND-TELL (video)

Pt 995

SHOW-AND-TELL (video).



TONIGHT! The weekly show-and-tell is SATURDAY NIGHT 4/28/12 at 9:30pm ET!

Show And Tell-1

The weekly show-and-tell is SATURDAY NIGHT 4/28/12 at 9:30pm ET!

Pt 810

NEW INSTRUCTIONS ON HOW TO BE ON THE SHOW AND TELL. If you are on Google+ and want to join, post a message/comment on Limor’s post on Google+ and say you want to show off a project and she will add you the “Show and Tell” circle. Then just look for the hangout announcement on the very same page later for your invite. There’s an 8 to 10 (at the same time) people limit per hangout, so if it’s full try later or just pop by next week same time. Some weeks are packed!

At 9:30pm ET you will see a link to the hang out. Just keep your mics muted until we call on you and have your project ready.

For those who just want to watch, you’ll be able to watch it live on Google+ ‘s broadcast feature on Ladyada’s page AND it will be recorded and uploaded to YouTube automatically (1st test is up). The Muppets and the Dalai Lama are up if you want to see other types of hangout examples.

Pt 309

Google is allowing some accounts to broadcast and record – Ladyada is trying this out, with your help :)

Previously we broadcasted the hangouts on Google to Ustream using their Producer tool to capture the hangout. We’re still using Ustream for “ASK AN ENGINEER”.

So if anything goes wrong, it’s all very new, but that’s also part of the fun :)

SEE you soon!

Filed under: ask-an-engineer — by adafruit, posted at 11:31 am


ASK AN ENGINEER 10PM ET – 4/28/12 – TONIGHT!

Ask Main

ASK AN ENGINEER 10PM ET – 4/28/12 – TONIGHT!

What is “Ask an engineer”? From the electronics enthusiast to the professional community – “Ask an Engineer” has a little bit of everything for everyone. If you’re a beginner, or a seasoned engineer – stop in and see what we’re up to! We have demos of projects and products we’re working on, we answer your engineering and electronics questions and we have a trivia question + give away each week. Mosfet the cat stops by too. Previous chats can be viewed at http://www.adafruit.com/ask

Chat details!

And don’t forget, 30 minutes before the show we’re doing our weekly show-and-tell. If you are on Google+ and want to join, just add/follow +Limor Fried’s (Ladyada) page and post a comment so you can be added to the show and tell circle. At 9:30pm ET you will see a link to the hang out. Just keep your mics muted until we call on you and have your project ready.

For those who just want to watch, you’ll be able to watch it live on Ustream here and we usually have a recorded version posted later.

Filed under: ask-an-engineer — by adafruit, posted at 11:30 am


www.flickr.com
adafruit's items Go to adafruit's photostream
www.flickr.com
items in Adafruits More in Adafruits pool