Servo library wont work with radio head library

The servo works using the example code from its library but when I add code from the radio head library to control a 433mhz rf module it stops working, here is the code I am using

#include <PWMServo.h>
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

String  bar_state = "close"; //bar state open or close
String  prev_bar_state = "close";

String msgReicieved = "";//message from reiciver

const int servo_rotation = 180 ;//amount the servo needs to move to completly move bar
const int servo_pin = 9;
int pos = 0;    // variable to store the servo position

PWMServo Lock_servo;  // create servo object called "servo"
RH_ASK driver (2000,4,11,3);// (speed,riecieve,transmit,push to talk)

void setup() {
  Serial.begin(9600);	
  Lock_servo.attach(SERVO_PIN_A);  // (pin 9)

    if (!driver.init())
         Serial.println("init failed");

}

void loop() {
  Serial.println("seffgdfg ");
  uint8_t buf[12];//amount of characters that is goign to be recieved
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)) {
    int i;
    // Message with a good checksum received, dump it.
    Serial.print("Message: ");
    msgReicieved = (char*)buf;
    Serial.println(msgReicieved);
  }


  for(pos = 0; pos < 180; pos += 1) { // goes from 0 degrees to 180 degrees, 1 degree steps
    Lock_servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1) {   // goes from 180 degrees to 0 degrees
    Lock_servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

when I delete these lines from the setup it starts working again

    if (!driver.init())
         Serial.println("init failed");

but those lines of code are needed to run the RF module, anyone know what is going on?

There is a library conflict over timer usage. You might try the ServoTimer2 library.

State which Arduino you are using, as there might be other solutions.

I am using arduino nano

There are about a half-dozen versions of "Arduino Nano", but I would guess you have the Classic Nano with the ATmega328. Try ServoTimer2.

I have the ATmega328P(old boot loader) if that changes anything
Okay I will try the servo timer 2

It worked thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.