Unresponsive Servo

I made a post a few days ago (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250793723/) about getting communication between two Arduinos with NRF24L01 modules. I got that working, but on the receiving end I am completely unable to run a servo. I tested it out first with the “Knob” example and it worked fine, but servo.write refuses to work no matter where I put it in the receiving Arduino’s code. Here’s the receiving end:

#include <Spi.h>
#include <mirf.h>
#include <nRF24L01.h>

#include <Servo.h>

int input[3];
Servo brake;
int brakeVal;

void setup(){
  Serial.begin(9600); 
  Mirf.init(); 
  Mirf.setRADDR((byte *)"bedcr");
  Mirf.config();
  Mirf.payload = 16;
  
  brake.attach(5);
}

void loop(){
  byte data[Mirf.payload];
  
  if(Mirf.dataReady()){
    do{
      Mirf.getData((byte *) &input);
    }while(!Mirf.rxFifoEmpty());
    
    brakeVal = input[0];
    brakeVal = map(brakeVal, 0, 1023, 0, 359);
    if(brakeVal < 179){
      Serial.println(brakeVal);
      brake.write(brakeVal);
    }
    delay(15);
  }
}

The serial monitor prints out the values as it receives them, but the servo remains inactive. Any ideas?

but the servo remains inactive. Any ideas?

If you're using pre-0017 Arduino, move the servo to a pin where it will work.

http://arduino.cc/en/Reference/Servo

Alright, I updated to Arduino 17, which mostly worked. THe servo is moving at least. However, as long as the program is running, the servo constantly twitches. If the potentiometer on the transmitting end remains in one position for a few seconds, the servo will move into position, but I'm looking for smooth and quick response.

Alright, I’ve been reworking my code and I’ve gotten everything working almost perfectly. My setup has a two-axis joystick pot attached to one Arduino and two servos attached to another Arduinio. They communicate with the NRF24L01 module. This all works, except the servos twitch almost non-stop, occasionally get “stuck” and after a minute or so, they stop responding altogether. This last problem can be fixed by resetting the sending Arduino, so it’s not a servo problem. My thought is that I just have too much power usage between the two servos and the wireless module, but I can’t figure out why the joystick Arduino stops sending data after a minute. Here’s my code:

Sending:

#include <Spi.h>
#include <mirf.h>
#include <nRF24L01.h>
 
int potV = 0;
int potH = 1;
int button = 2;
  int buttonState = 0;
  int lastButtonState = 0;
  int clickCount = 1;

int vertical = 0;
int horizontal = 0;

int output[3];
int outputCheck[3];

int red = 6;
int green = 5;
int blue = 3;
 
void setup(){ 
  pinMode(potV, INPUT);
  pinMode(potH, INPUT);
  pinMode(button, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  
  Mirf.init();
  Mirf.setTADDR((byte *)"bedcr");
  Mirf.config();
  Mirf.payload = 16;
} 
 
void loop(){
  vertical = analogRead(potV);
  output[0] = vertical;
  horizontal = analogRead(potH);
  output[1] = horizontal;
  
  buttonState = digitalRead(button);
  if (buttonState != lastButtonState){
    if (buttonState == LOW){
      if ((vertical >= 450) && (vertical <= 550)){
        if ((horizontal >= 450) && (horizontal <=550)){
          clickCount++;
        }
      }
    }
  }
  lastButtonState = buttonState;
  if (clickCount % 2 == 0){
    digitalWrite(green, LOW);
    digitalWrite(blue, HIGH);
    output[2] = 1;
  }
  else{
    digitalWrite(green, HIGH);
    digitalWrite(blue, LOW);
    output[2] = 0;
  }
  if (vertical < 450){
    digitalWrite(green, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(red, HIGH);
  }
  else{
    digitalWrite(red, LOW);
  }

  if((output[0] != outputCheck[0]) || (output[1] != outputCheck[1]) || (output[2] != outputCheck[2])){
    Mirf.send((byte *) &output);
  }
  
  outputCheck[0] = output[0];
  outputCheck[1] = output[1];
  outputCheck[2] = output[2];
  
  delay(50);
}

Receiving:

#include <Spi.h>
#include <mirf.h>
#include <nRF24L01.h>
#include <Servo.h>

int input[3];
  int accelVal;
  int steerVal;
  int reverse;
int inputCheck[3];

Servo accel;
Servo brake;
Servo steer;
Servo revrs;

/////////////////////////////////SETUP
void setup(){
  //Serial.begin(9600);
  
  Mirf.init(); 
  Mirf.setRADDR((byte *)"bedcr");
  Mirf.config();
  Mirf.payload = 16;

  accel.attach(3);
  brake.attach(5);
  steer.attach(6);
  revrs.attach(4);
}

/////////////////////////////////LOOP
void loop(){

  //byte data[Mirf.payload];
 
  if(Mirf.dataReady()){
    do{
      Mirf.getData((byte *) &input);
   
      if((input[0] != inputCheck[0]) || (input[1] != inputCheck[1]) || (input[2] != inputCheck[2])){
        
        //Acceleration Control
        accelVal = input[0];
        accelVal = map(accelVal, 0, 1023, 0, 179);
        //if(accelVal <= 179){
          brake.write(accelVal);
        //}
        //else{
        // brake.write(179);
        //}
        
        //Steering Control      
        steerVal = input[1];
        steerVal = map(steerVal, 0, 1023, 0, 179);
        steer.write(steerVal);
      }

    
      inputCheck[0] = input[0];
      inputCheck[1] = input[1];
      inputCheck[2] = input[2];
      
      delay(10);
    }while(!Mirf.rxFifoEmpty());
  }
}

Hi Seth, If I were you I would first try to locate the problem more precisly by eliminating the wireless link by using a wire to verify if it is really about data stream (which I don't think at all)

if problem is solved so wireless is in trouble,I would verify the specs more precisly to ensure that optimum data transfer rates are selected and if data volume is the actual problem,I would strip down the string sent between the 2 arduino to something really basic as:

A115B90C

Where stuff is: A:START & First Servo Data B:Scnd Servo Data C:Stop

this string would be analysed using DCC (direct control Console) see this blog for more info: http://exper-i-mental-jay.skyrock.com/ the car use DCC then you've basicly stripped down data stream,if problem is still there,try changing the wireless system used,maybe it'ms not sufficient.

In hope this might help you as other helped me before, have a great day Seth ^^

.. hi, I am also trying to get this kind of system working. see: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1289089043/15