I have searched, but couldn't find what I was after. I am new!
I am trying to create a automated barrier system for use in a Project i am doing and currently have the following code(there is much more to be added, ie LCD Display output, and RFID input):
When the servo is sent the command to go up, I want that command to stay until a input is received, if you understand that?
Here is my current code;
#include <Servo.h>
int button1 = 4; //Down button pin, connect to ground to move barrier Down
int press1 = 0;
int button2 = 5; //Up button pin, connect to ground to move barrier Up
int press2 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(6);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(150);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(60);
}
}
When the servo is sent the command to go up, I want that command to stay until a input is received, if you understand that?
Servo/button code that might somewhat relate to your project description.
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(170);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(10);
}
}
zoomkat:
Servo/button code that might somewhat relate to your project description.
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually DOES NOT WORK.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
My original code is based upon your very code you listed above. I just want to be able to write the servo until I get a digital input?
I have set it to open 90 degrees, like a normal barrier, however for the principle I need some safety factors and i'm going to use microswitches to detect when the barrier is in position, hence why I want it to stop on input.
There maybe a couple of options, the easiest being to detach the servo when a limit switch is actuated. This will stop the servo movement, but the servo will not hold position. A more complex possibility is to make incremental servo movements as in the servo "sweep" example, and freeze the last servo position command prior to the limit switch actuation.
KinHell:
When the servo is sent the command to go up, I want that command to stay until a input is received, if you understand that?
I think you are approaching the logic of the system from the wrong end. If you have selected a suitable servo then the command to move X degrees is all that is needed.
If you are concerned that the servo might fail, and either move too little or too much, then you could indeed include extra sensors. But their purpose should only be to shut down the system with a fault warning if they are triggered. There is no need for them to part of the normal working.
Video of a servo making small incremental moves until the paperclip touches the aluminum foil, at which time the servo is stopped and returned to the starting position.
zoomkat:
Video of a servo making small incremental moves until the paperclip touches the aluminum foil, at which time the servo is stopped and returned to the starting position.
I have come up/modified this code atm, i though it was working, but maybe i was wrong could you read over it?
something's not right.
void openDoor( int setDelay ) {
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
digitalWrite(redLed, LED_OFF); // Turn off red LED
digitalWrite(greenLed, LED_ON); // Turn on green LED
digitalWrite(relay, LOW); // Unlock door!
servo1.write(60);
if (digitalRead(sensor) == LOW) { // when button pressed pin should get low, button connected to ground
digitalWrite(blueLed, LED_ON);
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
servo1.write(60); // open servo
delay(2000);
Serial.println("Barrier Sensor Blocked 1");
delay(2000); // Give user enough time to cancel operation
if (digitalRead(sensor) == LOW) { // If button still be pressed
digitalWrite(blueLed, LED_ON);
Serial.println("Barrier Sensor locked - Waiting 2 more seconds 2");
digitalWrite(relay, LOW); // Unlock door!
servo1.write(60); // open servo
delay(2000);
}
else{
Serial.println("Barrier Sensor Clear, closing barrier");
delay(500);
servo1.write(150); //close servo
}
}
delay(setDelay); // Hold door lock open for given seconds
if (digitalRead(sensor) == LOW) { // If button pressed
delay(1000);
}
else
{
Serial.println("Barrier Sensor not Blocked - Closing barrier");
digitalWrite(relay, HIGH); // lock door!
servo1.write(150); // close servo
delay(5000);
}
delay(2000); // Hold green LED on for 2 more seconds
}
zoomkat:
Video of a servo making small incremental moves until the paperclip touches the aluminum foil, at which time the servo is stopped and returned to the starting position.