Go Down

Topic: Servomotor with IRRemote (Read 43 times) previous topic - next topic

jarvuschen

Thank you for watching this topic.
I have a question about servomotor.
My double servomotor is work for generally using.

Example code
-------------------------------------------------------------


 void setup() {
  // put your setup code here, to run once:
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (int i = 0;i <=20;i++)
  {
    digitalWrite(12,1);
    delayMicroseconds(1300);
    digitalWrite(12,0);
    delay(20);
    digitalWrite(13,1);
    delayMicroseconds(1700);
    digitalWrite(13,0);
    delay(20);
  }
}
-------------------------------------------------------------

By using this code, my Arduino car can go ahead.
But if I use it with infrared remotely control, it will failed.

Example code
-------------------------------------------------------------

#include <IRremote.h>

const int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;

long remoteCode2 = 0x94DF0175;
long remoteCode8 = 0xAD832A15;
long remoteCode5 = 0x280C09B5;

void setup() {
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results))
  {
    //Serial.println(results.value,HEX);
    if(results.value == remoteCode2)
    {
      forward();
      Serial.print("forward");
      Serial.println();
    }
    else if (results.value == remoteCode8)
    {
      backward();
      Serial.print("backward");
      Serial.println();
    }
    else if (results.value == remoteCode5)
    {
      bestop();
      Serial.print("stop");
      Serial.println();
    }
    results.value = 0;
    irrecv.resume();
  }

}


int backward()
{
  for (int i = 0;i <=20;i++)
  {
    digitalWrite(12,1);
    delayMicroseconds(1700);
    digitalWrite(12,0);
    delay(20);
    digitalWrite(13,1);
    delayMicroseconds(1300);
    digitalWrite(13,0);
    delay(20);
  }
}

int forward()
{
  for (int i = 0;i <=20;i++)
  {
    digitalWrite(12,1);
    delayMicroseconds(1300);
    digitalWrite(12,0);
    delay(20);
    digitalWrite(13,1);
    delayMicroseconds(1700);
    digitalWrite(13,0);
    delay(20);
  }
}
int bestop()
{
  for (int i = 0;i <=20;i++)
  {
    digitalWrite(12,1);
    delayMicroseconds(1500);
    digitalWrite(12,0);
    delay(20);
    digitalWrite(13,1);
    delayMicroseconds(1500);
    digitalWrite(13,0);
    delay(20);
  }
}
-------------------------------------------------------------

Both of motors counterclockwise rotate although I set different PWM for them.
What's my problem to control servomotors?

BTW, I have been used <Servo.h>, but my motors never stop to rotate.
(I didn't program any command, just attach them)

Thanks again!

JimboZA

#1
Jun 16, 2015, 03:31 pm Last Edit: Jun 16, 2015, 03:38 pm by JimboZA
BTW, I have been used <Servo.h>, but my motors never stop to rotate.
(I didn't program any command, just attach them)
servo.attach() sends a 1500ms pulse repeated at 20ms intervals. Although 1500ms is "nominally" the stop pulse for a continuous servo, it may not be correct for all servos, so you should experiment with doing a servo.write(xxx) (edit.... or better yet, writeMicroseconds() )with slightly different values of xxx.

I also believe that some continuous servos have a small screw to adjust it to stop when sent the 1500.
meArm build blog:     http://jimbozamearm.blogspot.com/
Please don't PM for technical advice.
Beware the XY Problem..... http://xyproblem.info/

Go Up