Introduction: Manual Controlled Toll Gate Using Push Button

Picture of Manual Controlled Toll Gate Using Push Button

Hey Makers!

In this Instructable we will simulate an Toll Booth Gate by using a manual Push Button to open and close the gate.

How Does It Work?

When the vehicle comes in front of the gate, the push button is pressed & the gate is opened, this is done by sending a command via microcontroller to the servo to open the gate 90 Degree.

So, we will let's begin.

Step 1: Components Needed

Picture of Components Needed

All That You Need.

Hardware Required

  • NodeMCU
  • Servo Motor
  • Push Button
  • Bread Board
  • Micro USB cable
  • Connecting Wires

Software Required

  • Arduino IDE (with ESP8266 Library installed)

Craft Items

  • Chart ( any colour )
  • Sketch Pen ( any color )
  • Empty Box

Step 2: Building Process

Picture of Building Process

The construction part of the craft is been displayed clearly in the images. You can choose any colour (optional). You add your creative ideas to make the Instructable look even more awesome.

Step 3: Circuit Connection

Picture of Circuit Connection

Follow each image for the circuit connections, connections are very simple.

This Instructable is part of a series of Instructables with the NodeMCU, my previous Instructables shows you how to Interfacing servo motor and push button with NodeMCU.

So make sure, you check those circuit connections first before trying out this.

Interfacing Servo Motor with NodeMCU

Control LED Using PushButton with NodeMCU

Step 4: Programming Stage

Picture of Programming Stage
#include <Servo.h>

Servo servo;

const int button = 16;

int temp = 0;

void setup() {

Serial.begin(9600);
pinMode(button, INPUT); servo.attach(2); //D4 servo.write(0); delay(1000); }

void loop() {

temp = digitalRead(button);
if (temp == HIGH) { servo.write(90); Serial.println(" Toll Opened Proceed "); delay(2000); } else { servo.write(0); delay(1000); Serial.println(" Pay to Open Toll "); }

}

Download the "Toll_Gate.ino" file and open it up in the Arduino IDE.

Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload. You can tinker with it if you like based on the application, or just use it as it is.

Step 5: OUTPUT

Picture of OUTPUT

That's all makers!

I hope you liked this, Stay Tuned for more Projects!

Thank you.

Comments

frarugi87 (author)2017-08-04

This is just a start, but... I think your program can be much better.

IMHO there are two major flaws with your program.

1) you will need to keep the button pressed for on average 500ms (at most 1s) to have it opened, since the control is performed every second

2) I think that the servo moves very quickly (usually servos move in a very short time)

To solve point 1, you can simply reduce the "delay(1000)" line to, for instance, "delay(50)". After all, when it is closed you do not have to wait.

To solve point 2, I'd suggest a complete rewriting of the program. My personal ideal implementation is the one of a finite state machine with 4 states:

- CLOSED: when entering, print "Pay toll". Exit conditions: when button is pressed go to opening

- OPENING: slowly open the gate (see details below). Exit conditions: when finished, go to open

- OPEN: wait 2 seconds. Exit conditions: when finished waiting, go to closing

- CLOSING: slowly close the gate (see details below). Exit conditions: when finished, go to closed

To slowly open/close the gate, you will have to have a part of code executed every X milliseconds that increments or decrements the current position towards the target position. Something like

if ((millis() - prevUpdate) >= 100) {
prevUpdate += 100;

if (currentPos > targetPos) {
if (currentPos - targetPos > 5)
currentPos -= 5;
else
currentPos = targetPos;
}

if (currentPos < targetPos) {
if (targetPos - currentPos > 5)
currentPos += 5;
else
currentPos = targetPos;
}

servo.write(currentPos);

}

You set the target position to what you want, then this code automatically moves the servo for you in the desired position.

With these values, it will move the servo by 5 every 100ms (this means that the transition 0->90 will take 1.8s).

As for rewriting it as a state machine, I implement it with a standard structure (this is pseudo-code). WARNING: very long code, which is a bit generic to show you MY best way to handle this kind of problems

First define the states and declare the variable to hold the current state of the state machine:

enum SM_states {
closed,
opening,
... };
SM_states current_state;

then, in the loop, you have to

1) evaluate if there is the need for a state change:

SM_states next_state = current_state;
switch (current_state) {
case closed:
if (button_was_pressed)
next_state = opening;
break;
...
}

2) check if there is a variation in the state, and if so perform any exiting or entering condition:

if (next_state != current_state) {
switch(current_state) {
// Exiting conditions; in this program there is none
}
// Actually change state
current_state = next_state;

switch(current_state) {
// Entering conditions; for instance in closed you will print "Pay toll"
}
}

3) perform the actions to be performed during the state:

switch(current_state) {
// In this case you can either put here the servo advancement or do nothing, and advance the servos all the time
}

With this structure, the program will be much more powerful (changing the program will be very easy, and it is very predictible). This structure can look very complicated, but it isn't and it is easily scalable

About This Instructable

445views

9favorites

License:

Bio: Work until you no longer have to introduce yourself.
More by TheCircuit:Carrot Almond MilkshakeDIY - Flower VasePotato Stuffing - Who's Hungry
Add instructable to: