Rotate while pressing button

Hi, is there a way to rotate a servo into a direction while pressing a button? in a nutshell: while (i press left button) rotate anticlockwise and if i release the button it stops. the servo i used is a standard 180 degrees modified into a continuous rotation servo.

If you mean a hobby servo that is limited to about 180 degrees rotation then it will stop when it hits the mechanical limit.

If you men a "continuous rotation" servo then yes, it's fairly easy to do. You've already written most of the code.

while (digitalRead(buttonPin)) {
    servo.attach(servoPin);
    servo.write(80);  // Values below 90 rotate one way, values above 90 rotate the other
}
servo.detach();  // Stop the rotation

johnwasser:
If you mean a hobby servo that is limited to about 180 degrees rotation then it will stop when it hits the mechanical limit.

If you men a “continuous rotation” servo then yes, it’s fairly easy to do. You’ve already written most of the code.

while (digitalRead(buttonPin)) {

servo.attach(servoPin);
    servo.write(80);  // Values below 90 rotate one way, values above 90 rotate the other
}
servo.detach();  // Stop the rotation

my button is by an Infrared remote control, so i use IRremote library, but the servo rotates continuosly only reading the first press
that’s the code

#include <Servo.h>
#include <IRremote.h>

int Lled=4, Rled=3;
int IRrec=2;
Servo motore;
decode_results results;
IRrecv irrecv(IRrec);

void setup()
{
  pinMode(Lled, OUTPUT);
  pinMode(Rled, OUTPUT);
  irrecv.enableIRIn();
}

void loop()
{
  if (irrecv.decode(&results))
  {
    while (results.value==16756815)
    {
      motore.attach(9);
      motore.write(180);
      digitalWrite(Lled, HIGH);
    }
    motore.detach();
    while (results.value==16711935)
    {
      motore.attach(9);
      motore.write(0);
      digitalWrite(Rled, HIGH);
    }
    motore.detach();
       irrecv.resume();
  }
}

Your code stays in a while() loop as long as (results.value==16756815). Since you don’t call irrecv.decode() inside the loop that value will never change and you will be stuck in that loop forever.

Since the IR signal is sent over time and you have to receive the entire message to detect a button push you can’t use the same code as you would for a physical button. You have to figure out how the IR remote tells you that the button is being held down. Some buttons only send once when the button is pushed. Some remotes send the same code repeatedly a long as the button is held. Some remotes have a “repeat” code that they send repeatedly after the initial button code while the button is being held. Some might send one code when the button is pressed and a different code when the button is released.

Since I don’t know what remote you are using my best guess is that it will send the same code repeatedly. I put in a 1/4 second delay in turning off the motor so the motor doesn’t turn on and immediately off for each repeat of the code. Of course this means that the motor will run at least 1/4 second after you release the button.

#include <Servo.h>
#include <IRremote.h>

const int Lled=4, Rled=3;
const unsigned long Left = 16756815;
const unsigned long Right = 16711935;
const int servoPin = 9;
const int IRrec=2;
Servo motore;
decode_results results;
unsigned long LastReceive = 0;
IRrecv irrecv(IRrec);

void setup()
{
  pinMode(Lled, OUTPUT);
  pinMode(Rled, OUTPUT);
  irrecv.enableIRIn();
}

void loop()
{
  if (irrecv.decode(&results))
  { // Signal decoded
    LastReceive = millis();
    if (results.value==Left)
    {
      motore.attach(servoPin);
      motore.write(180);
      digitalWrite(Lled, HIGH);
      digitalWrite(Rled, LOW);
    }
    else if  (results.value==Right)
    {
      motore.attach(servoPin);
      motore.write(0);
      digitalWrite(Lled, LOW);
      digitalWrite(Rled, HIGH);
    }
    irrecv.resume();
  }
  else { // No signal ready for decode
    if (LastReceive-millis() > 250) {
      // It has been more than 250 milliseconds since the last IR
      // signal was received.
      motore.detach();
    }
  }
}

johnwasser:
Your code stays in a while() loop as long as (results.value==16756815). Since you don’t call irrecv.decode() inside the loop that value will never change and you will be stuck in that loop forever.

Since the IR signal is sent over time and you have to receive the entire message to detect a button push you can’t use the same code as you would for a physical button. You have to figure out how the IR remote tells you that the button is being held down. Some buttons only send once when the button is pushed. Some remotes send the same code repeatedly a long as the button is held. Some remotes have a “repeat” code that they send repeatedly after the initial button code while the button is being held. Some might send one code when the button is pressed and a different code when the button is released.

Since I don’t know what remote you are using my best guess is that it will send the same code repeatedly. I put in a 1/4 second delay in turning off the motor so the motor doesn’t turn on and immediately off for each repeat of the code. Of course this means that the motor will run at least 1/4 second after you release the button.

#include <Servo.h>

#include <IRremote.h>

const int Lled=4, Rled=3;
const unsigned long Left = 16756815;
const unsigned long Right = 16711935;
const int servoPin = 9;
const int IRrec=2;
Servo motore;
decode_results results;
unsigned long LastReceive = 0;
IRrecv irrecv(IRrec);

void setup()
{
  pinMode(Lled, OUTPUT);
  pinMode(Rled, OUTPUT);
  irrecv.enableIRIn();
}

void loop()
{
  if (irrecv.decode(&results))
  { // Signal decoded
    LastReceive = millis();
    if (results.value==Left)
    {
      motore.attach(servoPin);
      motore.write(180);
      digitalWrite(Lled, HIGH);
      digitalWrite(Rled, LOW);
    }
    else if  (results.value==Right)
    {
      motore.attach(servoPin);
      motore.write(0);
      digitalWrite(Lled, LOW);
      digitalWrite(Rled, HIGH);
    }
    irrecv.resume();
  }
  else { // No signal ready for decode
    if (LastReceive-millis() > 250) {
      // It has been more than 250 milliseconds since the last IR
      // signal was received.
      motore.detach();
    }
  }
}

For starters, thank’s of all,but I think the problem is the signal from the IR remote control, i made a sketch to read the signal, and i noticed that every signal pressed for a while is codified with a unique code for every buttons, is there anyway to solve, using this remote control? This project is to use a servo to rotate an axe that should be the motor shaft, in a nutshell, this make the rotation of the robot.

giangix26: i made a sketch to read the signal, and i noticed that every signal pressed for a while is codified with a unique code for every buttons,

Can you describe your results in more detail? If you press and hold the "Left" button, what codes do you get and what is the time between codes?

You might try the below code. It assumes your continuous rotation neutral command value is ~1500us. Most modified continuous rotation servos ramp rotation speed ±100us from the neutral value.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    myservo.writeMicroseconds(1400);
  }
  else {
    myservo.writeMicroseconds(1500);
  }
}