Gate controlled by a servo

I need this program with arduino uno: a button turns on a servo that turns counterclockwise reaching its limit switch, stops for 5 seconds and then restarts in the opposite direction reaching the opposite limit switch. when the servo moves, a led flashes every 500 milliseconds. when it reaches the second limit switch, the system stops

Seems nice. What’s your best attempt?

The forum does not write codes on demand.
We can help you if you tried to write yourself and ran into problems
But if you want to get a finished program -please post to the Paid Consultancy section

The topic title is autocorrect at its best

The butler opens my gate

Hi, @david56
Is this similar to this;

That you never replied too?

And this one different subject but same plea.

Tom.. :smiley: :+1: :coffee: :australia:

I tried to make this program but on tinkercad it doesn't work as it should, can you tell me if I did something wrong..
#include <Servo.h>

Servo myservo; // crea un oggetto Servo
int buttonPin = 2; // pin per il pulsante
int ledPin = 3; // pin per il led
int pos = 0; // posizione del servo
int dir = 1; // direzione del movimento del servo
int lastButtonState = LOW; // stato precedente del pulsante
unsigned long lastDebounceTime = 0; // ultimo tempo di debounce
unsigned long debounceDelay = 50; // tempo di debounce

void setup() {
myservo.attach(5); // attiva il pin del servo
pinMode(buttonPin, INPUT); // imposta il pin del pulsante come input
pinMode(ledPin, OUTPUT); // imposta il pin del led come output
}

void loop() {
int buttonState = digitalRead(buttonPin); // leggi lo stato del pulsante

// esegui il debounce del pulsante
if (buttonState != lastButtonState) {
lastDebounceTime = millis();
}
if (millis() - lastDebounceTime > debounceDelay) {
if (buttonState == HIGH) {
dir = -dir; // cambia la direzione del movimento del servo
pos = 0; // riparti dalla posizione iniziale
}
}
lastButtonState = buttonState;

// muovi il servo
pos += dir;
if (pos < 0) {
pos = 0;
dir = -dir;
delay(5000); // pausa di 5 secondi alla finecorsa
} else if (pos > 180) {
pos = 180;
dir = -dir;
delay(5000); // pausa di 5 secondi alla finecorsa
}
myservo.write(pos); // imposta la posizione del servo

// fai lampeggiare il led
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}

Please insert your code using the code tags

I tried to make this program but on tinkercad it doesn't work as it should, can you tell me if I did something wrong..

#include <Servo.h>

Servo myservo; 
int buttonPin = 2;
int ledPin = 3;
int pos = 0;
int dir = 1; 
int lastButtonState = LOW; 
unsigned long lastDebounceTime = 0; 
unsigned long debounceDelay = 50;
void setup() {
myservo.attach(5); 
pinMode(buttonPin, INPUT); 
pinMode(ledPin, OUTPUT); 
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
lastDebounceTime = millis();
}
if (millis() - lastDebounceTime > debounceDelay) {
if (buttonState == HIGH) {
dir = -dir; 
pos = 0;
}
}
lastButtonState = buttonState;

// muovi il servo
pos += dir;
if (pos < 0) {
pos = 0;
dir = -dir;
delay(5000); 
} else if (pos > 180) {
pos = 180;
dir = -dir;
delay(5000); 
}
myservo.write(pos); 
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}

Hi,
Have you been able to write code that JUST basically controls a servo?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hello david56

Use a logic analyzer to see what happens.

Insert Serial.println()´s at points of interrest.

Have a nice day and enjoy coding in C++.

Your gate will constantly open and close, regardless of the button, since the servo code does not depend on the button state at all.
You must move the gate only after pressing the button.

And also - your debouncing code is incorrect, you should reread the button state after debounce interval was expired.

Hi,
Seeing as you are looking for switch TRANSITIONS, do you need debounce.
Once you detect the switch change your servo moves, any bouncing should not effect it.
Likewise the limit switches.
Once you detect a limit change, the servo stops, any bouncing of that switch can't stop it any further.
I would suggest you use flags, servo stop, servo open, servo close, each flag set on a switch transition.
If you press the open button, if the contacts bounce or not, once you have set the servo open flag, you cannot set it any further no matter how many times the contacts bounce.

I try and write my code such that contact bounce is if possible not a problem.
State Machine coding.

Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.