Servo control of a flap from 0-180 wait and then move to zero

Hi All Can someone help with flap issue on a machine i have installed a MZ999 servo due to a failed arm that was controlled by a latching solenoid but to get it going i needed a work around but think a servo will work much better

But having issues with the code I'm great on the automation of the machine but need a fix to get it going any one available to help

Servo is a MZ999 controlled by a Nano v3 due to space at the front of the machine once the plc gives the signal the flap will close and wait until the second sensor is made then return back to zero with the flap open below is my code please be gentle my coding is in scl and tia portal but not in C but need a quick fix Can anyone help ........

// the sensor is no longer true the flap should close 180 degrees until sensor 2 becomes true then waits 2 seconds then will return 
// back to zero until sensor 1 is made true again to start the cycle again between 0-180 wait 2 seconds then return to zero 
#include <Servo.h>
Servo myservoNose;  //servo1
//----------------------------------------Setting of the inputs and output to the servo------------------------------------------------ 
const int buttonPin1 = 7; // pin 7 for sensor 1 
const int buttonPin2 = 2; // pin 2 for Sensor 2
const int ServoPin = 3;


void setup()
{
  Serial.begin(115200); //activating Serial Monitor
  
  myservoNose.attach(ServoPin); //activating pin 9 for servo
  pinMode(buttonPin1 , INPUT); //To set the signal from the first sensor 
  pinMode(buttonPin2 , INPUT); //To set the signal from the second sensor 
}


void loop()
{
  int buttonState1 = digitalRead(buttonPin1); //reading buttonPin and storing it as buttonState.
  Serial.println(buttonState1); //telling computer to print buttonState.
  if (buttonState1 == HIGH)  //if  The sensor is high move the servo form 0-180 degrees...
  {
    delay (1000);
    myservoNose.write(0); // the servo from 0 degrees   
    delay(2000); //wait 
    myservoNose.write(180); 
    delay(500); //wait as the door is closed 
  }


  int buttonState2 = digitalRead(buttonPin2); //reading buttonPin and storing it as buttonState.
  Serial.println(buttonState2); //telling computer to print buttonState.
  if (buttonState2 == HIGH)  //if sensor 2 is high move the servo back to zero ...
  {
    myservoNose.write(180); // Sensor 2 is met and the servo will now return from 180-0
    delay(500); //
    myservoNose.write(0); //
  }
}
type or paste code here

I am sorry but I don't know what you mean by that

More details please

Hi Mate My programming is mainly with siemens using Scl and ladder and a little python

The issue is the plc once the sensor is made will give me a output of 24v i have used a relay with a 5 v line to convert 24 v signal to 5 v suitable for the Arduino nano so the nano only see's the 5v input
once the signal is sent the flap closes 180 degrees on the servo will fine tune once the program works then once the second sensor is made the flap returns to zero position then awaits the signal again to close the flap

Hope this explains it better

simon

In your sketch you test whether pin 7 is HIGH and if it is you wait 1 second, move the servo to 0, wait 2 seconds, move the servo to 180 and wait half a second

That does not match what you say you want to do.

In addition to that, you test the state of the input pins and if they are HIGH you take actions. This implies that the input pins are normally LOW. Is there anything in your circuit keeping them LOW or are they floating at an unknow voltage, maybe HIGH, maybe LOW, maybe changing ?

Your sensor could drive a logic level N-type mosfet and avoid any need for debounce.
The servo sweep example in the IDE would give a basic idea on how to drive your servo with any pause.

Hi mate
This is why I'm asking for help I understand my code makes no scene And so far not even told it my input is bool as I'm giving it 5v signal I think its best just to wait 2 days for the part and control the flap with the plc as normal
But thank you again

simon

Thanks is the only part of your reply I could understand.
Good luck.

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