Problem with SHIELD-PS2 arduino and servo

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

Problem with SHIELD-PS2 arduino and servo

Postby malikai » Sun Dec 29, 2013 10:08 pm

Recently I have bought the ps2 sheild..I just simply want to use it for moving a servo using ps2 library that given..
But the servo wont stop move on it's own even when there is no signal from ps2 controller..I think it's some kind of noise
I would like to know if there is a way to solve this problem..This is the sketch
CODE: SELECT_ALL_CODE
#include <Servo.h>
#include <Shield_PS2.h>

PS2 ps2=PS2();                 
Servo myservo;

void setup()
{
myservo.attach(9);
   ps2.init(9600, 2, 3);
}

void loop()
{
  if( ps2.getval(p_left)==0)     
  {
myservo.writeMicroseconds (1100);
delay (10);
  }

 
    if( ps2.getval(p_up)==0)
  {
myservo.writeMicroseconds (1600);
delay (10);
  }
 
      if( ps2.getval(p_right)==0)
  {
 myservo.writeMicroseconds (2300);
delay (10);
  }
 
}
malikai
Greenhorn
 
Posts: 2
Joined: Sat Dec 14, 2013 10:01 am

Re: Problem with SHIELD-PS2 arduino and servo

Postby ober » Thu Jan 02, 2014 8:45 am

Are you using external power source to power the Arduino and everything? Unstable RC servo behavior might due to insufficient power.

Try writing program for the servo only, and later add in PS2 shield program.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Problem with SHIELD-PS2 arduino and servo

Postby malikai » Sat Jan 04, 2014 1:29 pm

Yes I connect it to external power..But if I just use the remote stand alone to send high/low bit..There is no problem at all
Here's the sample code when I connect it to the remote
CODE: SELECT_ALL_CODE
#include <Servo.h>
#include <Shield_PS2.h>
PS2 ps2=PS2();

Servo servox;
Servo servoy;

int vcw=1;
int vccw=1;
int hcw=1;
int hccw=1;

int posx=0;
int posy=0;


void setup()
{

 
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
servox.attach(9);
servoy.attach(11);

}

void loop()
{
hccw=digitalRead(7);
hcw=digitalRead(6);

vccw=digitalRead(2);
vcw=digitalRead(4);

posx=constrain(posx,0,180);
posy=constrain(posy,0,180);


if(vccw==0 && vcw==1)
{
posy++;
servoy.write(posy);
delay(10);
}

if(vcw==0 && vccw==1)
{
posy--;
servoy.write(posy);
delay(10);
}

if(hccw==0)
{
posx++;
servox.write(posx);
delay(10);
}

if(hcw==0)
{
posx--;
servox.write(posx);
delay(10);
}

}
malikai
Greenhorn
 
Posts: 2
Joined: Sat Dec 14, 2013 10:01 am

Re: Problem with SHIELD-PS2 arduino and servo

Postby ober » Sun Jan 05, 2014 7:04 am

Looking through your code again trigger some thought, now I understand the possible root cause. This is due to interrupt.

Servo library uses interrupt to output pulses further control the RC servo, while softserial also uses interrupt to transmit and receive UART. Softserial will disable the interrupt for a short period of time and this affect the Servo function to generate proper pulses and drive the RC servo.

There are plenty of discussion in the Internet, example: http://forum.arduino.cc/index.php/topic,28456.0.html

One easy method you can try is to use hardware UART (D0 and D1) for PS2 shield. Anyway this will affect the bootloader process as pin 0 and pin 1 is being used for program loading, just disconnect the jumper D0 when you expect to load program and place it back after done uploading program. Do not use softserial.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm


Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 37 guests