How to move a servomotor to press a key?

Hi!

How could a program, start the servo motor is placed at 90 °, when I press "<" moves to the left 3 degrees and if I press ">" to move 3 degrees to the right.

but with the proviso that if I press twice "<" the servo motor moves 6 degrees, and if I press "<" now 9 degrees, about how many pulses you send it to move in three degrees either left or right.

I then drive another servo motor that moves the same, but in the same program.

Any suggestions?

Break the problem down. You need code that can detect the button presses, and code that can determine how much to move based on the current button press and the state left by previous button presses, and code to move the servo to a given position. You should be able to solve each of these problems independently.

look this is my code.

#include <Servo.h>

Servo nazza;
 
void setup() {
  
  Serial.begin (9600);
  nazza.attach(10);
              
             }

void loop () {
  
  if (Serial.available () > 0) {
    
    int jul = Serial.read();
    nazza.write(90);
    
    if (jul == '0' ){
      nazza.write(0);
    }
    
  if (jul == '1'){
    nazza.write(180);
  }
  }
  delay (15);
}

by default when I connect the servo motor moves 90 degrees, then I press "1" my actuator moves from one side, pressing the "0" moves to the side.

far as I can do that by pressing "1" to move 2 degrees, and if you press down again another 2 degrees, and so on until it reaches its limit, the same, if I press the "0", that moves across every two degrees.

That is my question.

I'm not sure I understand your question correctly, but it sounds as if you want to move 2 degrees each time, either one way or the other?

Then I think you need a new variable (say "nazzaPos") to keep the current position, then add or subtract 2 degrees each time it receives say an "A" or an "S"...

Something like this? (Not tested, use with care :stuck_out_tongue: )

#include <Servo.h>

Servo nazza;
int nazzaPos = 90;


void setup() {
  
  Serial.begin (9600);
  nazza.attach(10);
  nazza.write(nazzaPos);  // it's 90 right now from the declaration above
              
             }

void loop () {
  
  if (Serial.available () > 0) {
   
    int jul = Serial.read();
    
    
    if (jul == 'A' ){     // A means Add
      nazza.write(nazzaPos + 2);
     // add some code to check if it got to the end of its range and do something about that
    }
    
  if (jul == 'S'){       // S means Subtract
    nazza.write(nazzaPos - 2);
   // add some code to check if it got to the end of its range and do something about that
  }
  }
  delay (15);
}

Yes it's correct but I have the same problem...

when I press more than once the "a", just move once again and after no longer move.

where is the problem?

Rather than this:

nazza.write(nazzaPos - 2);

You need this:

nazzaPos -= 2;
nazza.write(nazzaPos);

Not compiled.

thank you very much Wildbill

This is the code now

#include <Servo.h>

Servo nazza;
int nazzaPos = 90;


void setup() {
  
  Serial.begin (9600);
  nazza.attach(10);
  nazza.write(nazzaPos);  // Colocamos el servomotor a 90° para tomarlo de 0-90 por izquierda, y 90-80 por derecha.
              
             }

void loop () {
  
  if (Serial.available () > 0) {
   
    int jul = Serial.read();
    
    
    if (jul == 'a' ){     // asignamos una variable "a" para mover posiciones a derecha
      nazzaPos -= 10;
      nazza.write(nazzaPos);
     
    }
    
  if (jul == 'b'){       // asignamos una variable "b" para mover posiciones a izquierda.
    nazzaPos += 10;
   nazza.write(nazzaPos);
  }
  }
  delay (15);
}

but I have a question, now if I want to control another motor, Where I have to put the code?