Servo movement as a function of input value

Hello,

I’m trying to make a mechanical shutter with the servo MG995. The shutter movement will be controlled by Labview, by the way. It means the Arduino board will be regarded as VISA serial connection with PC which can send and read a signal via Labview. I’m not sure this is possible or not. But, I will check this after I can finish a code.

What I’m curious about is that can we make a code that servo movement is controlled by input value. There are two input values: “y” or “n”. If “y” is sent to the Arduino, the Servo should rotate 60 degree and return back to its original position. If “n” is sent to the Arduino, the Servo should not move at all.

I can find several codes that Servo do the continuous sweeping action but can’t find any code with the previous description.

I have no experience with computer programming. I’m just trying to fix the old lab equipment by making a new mechanical shutter with a Arduino board.

Please give me any ideas about the code or any resources that can be helpful for making this code.

Thanks!!

Here is some information on handling serial communiction.

What I’m curious about is that can we make a code that servo movement is controlled by input value.

Of course.

  if(Serial.available() > 0)
   {
      char c = Serial.read();
      if(c == 'y')
      {
         yourServo.write(60);
         yourServo.write(0);
      }
   }

It seems pointless to send a value that says do nothing, when not sending a value says the same thing.

That code will do exactly what you asked for, but will probably not do what you want. Adding a delay() between the two calls to Servo::write() will probably be necessary.

takim717: If "y" is sent to the Arduino, the Servo should rotate 60 degree and return back to its original position. If "n" is sent to the Arduino, the Servo should not move at all.

Not that hard but the big unanswered question, what tells the servo to move back? Or probably here, after how long should it move back? Because when you talk microcontroller fast doing

servo.write(60);
servo.write(0);

will happen so quickly you can't see it (or even, the servo has no time to react on it).

And another question, what is that "original position"?

And why the 'n' if it doesn't need to do anything?

Thanks for your comments.

First, I think I have to set the original position. There is no definite original position but any position will do that can block the laser at that moment. So, I would say that any arbitrary value.

So, When the "y" is sent to the device, the arm should move 60 degree from the original position (I would say 0). And stay at that position about 500 ms and go back to the original position. Based on your codes, I can write like this:

yourServo.write(60); delay(500); yourServo.write(0);

As you mentioned, maybe there is no need to define any action for "n"

However, the shutter will be operated by Labview. And Labview code has two cases for shutter action: "y" and "n". My concern is that if there is no definition for "n", some unknown error would occur because of the undefined "n".

I'm trying to understand handling serial communication now. I will post more if I have hard time on understanding.

Thanks again!

My concern is that if there is no definition for "n", some unknown error would occur because of the undefined "n".

There will be no problem ignoring the 'n'. The labview code should be capable of not sending the 'n', but, if not, the Arduino can ignore it, and labview will never know.

Hello guys, I made a code but it works once but does not work perfectly. When I input "y" through the serial monitor, it is moving. But, it only moves slightly (can't reach to the maximum angle). After finishing the one action, no function will not work any longer. Even if I input "y" again, there is no answer from the device. It seems like that UNO connection is lost. Do you think there are any problems with the code? I want to make Servo move, whenever putting "y".

#include "Servo.h"

Servo myservo;  // create servo object to control a servo

void setup() {
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
   Serial.begin(9600);  
   myservo.write(0);
}

void loop () {
      
  recvOneChar();
}

void recvOneChar() {
  if (Serial.available() > 0) {
      char c = Serial.read();
      if(c == 'y') {
        myservo.write(180);
        delay(150);
        myservo.write(0);
        
      }
}}
myservo.write(180);
        delay(150);
        myservo.write(0);

Most hobby servos specs are around 200 milliseconds to move 60 degrees @ 6V. So 180 degrees will take about 600 milliseconds (longer if powered @ 5V). Increasing the delay will make a big difference. I have a servo connected to an Uno (from helping someone else earlier) and if I run your code with the delay increased to 1500 The servo moves to 180 pauses a bit and returns to 0 each time I send 'y'. I sent 'y' (with enter, of course) multiple times and the servo responded correctly each time.

Thanks!!

void loop () {
     
  recvOneChar();
}

There are two problems with this. First, it is pointless to make a function (loop()) that does nothing but unconditionally call one function (recvOneChar()).

Second, there is NOTHING in the name of the function that it does call (recvOneChar()) that indicates that it will make a servo move.

Meaningful variable and function names ARE important.

Thanks for your comment. By the way, I'm not quite following your suggestion. Could you correct the code for me? Even though the code is not perfect, it is working. The problem is that I try to communicate the servo with Labview program, why I add "serial" function in the code, but it is not communicating at all. When I send 'y' to the Arduino through the Labview "visa serial communication" function, the Arduino does not respond at all. Do you think there is a problem on the code itself??

Do you think there is a problem on the code itself??

No. I suspect that it is a PEBCAK issue.

Is the RX light on the Arduino blinking when LabView is supposedly sending data?

You say that Labview operates the shutter. Does Labview handle your timing?

are you sure your system doesn't send 'y' when the shutter needs to close and send 'n' when the shutter needs to open?

Many "cheap" servos (if that's what you have) won't do 0° to 180°. check the min and max angle it can really go to and use those values. Physically install the servo arm so that at min° laser is not visible and max° laser is not blocked anymore (or the opposite)