If sentence with servo

i want to make a if sentence using a servo. so like if a servo is at 90 degrees it should make a output

We need a lot more information.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

the servo will go where you told it to go, and it's pretty fast at going there.
So in the grand scheme of things your code should know where it is.

the Servo library offers a read() function which will tell you where the servo is (basically where you told it to go)

its like: if (servo13.write(90)== true){
digitalwrite(12,HIGH);
}

Hi @billycamaro

welcome to the arduino-forum.

I do not yet understand what making an "if-sentence using a servo" means.

"sentence" is a rather uncommon term in the field of programming.
Please explain what you mean with this word.

Another option is to describe the wanted functionality with everyday words.

best regards Stefan

#include <Servo.h>

Servo servo_13;

void setup() { 
 servo_13.attach(13, 500, 2500);
pinmode(12,OUTPUT);

}

void loop() {
if( servo_13.read(== 90)){
digitalWrite(12,HIGH);
}

}

Your code tells the servo to move to a position.

When the code says go to 90, you do digitalwrite(12,HIGH);.

how to write it the right way
sorry i am a beginner in arduino

I don't believe that Servo.write returns a value, Boolean or int.

What are you trying to do?

for(int angle = 0; angle <= 90; angle ++)
{
   servo.write(angle);

   if (angle == 90)
   {
      digitalwrite(12,HIGH);
   }
}

Hello billycamaro

Take a view to gain the knowledge:

hth

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

sorry but i stil dont know where i should put this

Time for you to learn the basics of C++ (Arduino) programming.

There are lots of Youtube videos that cover basic and advanced programming.

You should master the examples that come with the Arduino IDE.

1 Like

we don't either... what's your code ?

this kind of RC-servo

is a device where your code is commanding it turn to 90 degrees.
Or where your code is commanding the servo to turn to any other angle
examples

servo_13.write(90); // servo turn to position 90 degrees

servo_13.write(10); // servo turn to position 10 degrees

servo_13.write(25); // servo turn to position 25 degrees

servo_13.write(164); // servo turn to position 164 degrees

This means whenever you write a line of code with a function-call
servo_13.write (90);
your code is command the servo to a position.

At this point I recommend to you that you describe what you want to do
in normal words. In everyday words.

In this moment you have some misconceptions about how programming works
and to be cleary to understand you should use normal words.

best regards Stefan

The line should be written like this:

  if ( servo_13.read() == 90) {

BUT... 90 is not what you will get... you will get a value halfway between MIN and MAX of your servo.attach()

The servo "homes" to 90 degrees (center of left/right deflection).

You can also use readMicroseconds() for a finer value.

  int read();                        // returns current pulse width as an angle between 0 and 180 degrees
  int readMicroseconds();            // returns current pulse width in microseconds for this servo (was read_us() in first release)

This is your code:

#include <Servo.h>
Servo servo_13;

void setup() {
  servo_13.attach(13, 500, 2500);
  pinMode(12, OUTPUT);
}

void loop() {
  if ( servo_13.read() == 90) {
    digitalWrite(12, HIGH);
  }
}

...but you need to be aware that Servo read only returns the value you last wrote.
The servo may not have reached that position yet, because, well, it's mechanical, and really slow.

2 Likes

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