Help with rf and servos!

Hello,

I'm trying to controll two servos using 433MHz modules between two arduinos but I'm having some problems.
I'm using the RCSwitch and the servo libraries and I have already tried (If someone has an alternative reply me) virtualwire and ServoTimer2.

The actual code is:

For the emitter:

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableTransmit(10); //Usant el pin 10
}

void loop(){
  if(Serial.available()>0){
    mySwitch.send(Serial.read(),8);
  }
  delay(100);
}

And for the receiver:

#include <Servo.h>
#include <RCSwitch.h>
Servo servod;
Servo servoe;
RCSwitch mySwitch = RCSwitch();
int var;

void setup(){
  servod.attach(11);
  servoe.attach(10);
  pinMode(13,OUTPUT);
  mySwitch.enableReceive(0); //interrupt en el pin 2
}
void loop(){
  if(mySwitch.available()){
    digitalWrite(13,1);
    var=mySwitch.getReceivedValue();
    switch(var){
      case '1':
      servod.write(90);
      servoe.write(90);
      break;
      case '2':
      servod.write(180);
      servoe.write(0);
      break;
      case '3':
      servod.write(90);
      servoe.write(0);
      break;
      case '4':
      servod.write(0);
      servoe.write(180);
      break;
      case '5':
      servod.write(180);
      servoe.write(90);
      break;
    }
    digitalWrite(13,0);
    mySwitch.resetAvailable();
  }
}

The numbers are:

1 stop
2 forward
3 turn right
4 backward
5 turn left

The wiring:
Emitter:

Receiver:

Please, help me solve this haha this code is not working for me.

Thanks!

The code does something. Perhaps you'd be so kind as to describe what it actually does.

There is no Serial.begin() or Serial.print() statements in the receiver code. Perhaps you'd be good enough to explain why not. Debugging by guesswork takes at least an order of magnitude longer than debugging with facts.

The idea is to controll a type of robot, three wheels, two on the sides with a servo each one and one omnidirectional wheel on the back.
That's why it doesn't need Serial, because it isn't connected via serial.
servod is right side servo and servoe is left side servo.

Sorry for the lack of information!

That's why it doesn't need Serial, because it isn't connected via serial.

OK. You don't need the forum, then, either, because there is nothing on the Arduino connected to the forum.

So, why are you here?

Information, right? Well, the Arduino has plenty of information to share, if you'd just let it.

OK, so what do you mean?

I've found that it works if I unplug the servos from the arduino, I send the info, and I plug them they move. But if I want to make them stop I have to unplug, send the new information and replug. It doesn't work with the servos plugged!

OK, so what do you mean?

I mean that you should add a Serial.begin() statement to setup(). I mean that you should add Serial.print() statements to loop().

What are you getting from the RCSwitch instance? Until you know what the slave received, it is absolutely pointless to try to use the data.

Oh okey, I've put the following code:

#include <Servo.h>
#include <RCSwitch.h>
Servo servod;
Servo servoe;
RCSwitch mySwitch = RCSwitch();
int var;

void setup(){
  Serial.begin(9600);
  servod.attach(11);
  servoe.attach(10);
  pinMode(13,OUTPUT);
  mySwitch.enableReceive(0); //interrupt on pin 2
}
void loop(){
  if(mySwitch.available()){
    digitalWrite(13,1);
    var=mySwitch.getReceivedValue();
    Serial.print(var);
    switch(var){
      case '1':
      servod.write(90);
      servoe.write(90);
      break;
      case '2':
      servod.write(180);
      servoe.write(0);
      break;
      case '3':
      servod.write(90);
      servoe.write(0);
      break;
      case '4':
      servod.write(0);
      servoe.write(180);
      break;
      case '5':
      servod.write(180);
      servoe.write(90);
      break;
    }
    digitalWrite(13,0);
  }
}

When I have the servos plugged, it doesn't receive anything, but when I unplud them and I send number 2 (forward), I receive:

5050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050

Any idea?

Any idea?

Sure. 50 is the ASCII code for the character '2'. So, now you know that what you sent is what you received. Now, you can begin tracking down what is going wrong with how you are using the data.

Move that Serial.print() into the case '2' block, changing it to Serial.println("Case 2"). Confirm that case 2 is indeed selected when you send 2 (though I am 99.999999% certain that it will be).

If you see Case 2 printed over and over, on separate lines, then you know that the problem is that the Servo class and the RCSwitch class are using the same timer, preventing the servos from working.

At that point, you can determine what to do about the problem.

Ok I've made a little change in the emitter code I've changed the "int var;" for "char var;" and in the receiver code:

#include <Servo.h>
#include <RCSwitch.h>
Servo servod;
Servo servoe;
RCSwitch mySwitch = RCSwitch();
int var;

void setup(){
  Serial.begin(9600);
  servod.attach(11);
  servoe.attach(10);
  pinMode(13,OUTPUT);
  mySwitch.enableReceive(0); //interrupt on pin 2
}
void loop(){
  if(mySwitch.available()){
    digitalWrite(13,1);
    var=mySwitch.getReceivedValue();
    Serial.println(var);
    switch(var){
      case '1':
      Serial.println("Received 1");
      servod.write(90);
      servoe.write(90);
      break;
      case '2':
      Serial.println("Received 2");
      servod.write(180);
      servoe.write(0);
      break;
      case '3':
      Serial.println("Received 3");
      servod.write(90);
      servoe.write(0);
      break;
      case '4':
      Serial.println("Received 4");
      servod.write(0);
      servoe.write(180);
      break;
      case '5':
      Serial.println("Received 5");
      servod.write(180);
      servoe.write(90);
      break;
    }
    digitalWrite(13,0);
    mySwitch.resetAvailable();
  }
}

I've sent 2 (with the servos unplugged) and the result is the following:

So, now you know that the radios are working fine, and that what you send is what you receive.

If the servos don't move, that indicates to me that the RCSwitch class uses the same timer as the Servo class. You could post a link to the library, so we could confirm that.

Or, you could look for the ServoTimer2 library, and try that with your servos (all that changes is the class name). It uses a different timer, for those cases where timer 1 is used by some other class.

I'm going to try with ServoTimer2, where I can download the library?

I googled "ServoTomer2 library Arduino" and got just 5 hits. Not surprisingly, the first one is a link to the library.

I've tried ServoTimer2, the code I used is the following:

Emitter:

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
char v;

void setup() {
  Serial.begin(9600);
  mySwitch.enableTransmit(10); //Usant el pin 10
}

void loop(){
  if(Serial.available()>0){
    v=Serial.read();
    Serial.print("Sent: ");
    Serial.println(v);
    mySwitch.send(v,8);
  }
  delay(100);
}

Receiver:

#include <RCSwitch.h>
#include <ServoTimer2.h>
ServoTimer2 servod;
ServoTimer2 servoe;
RCSwitch mySwitch = RCSwitch();
int var;

void setup(){
  Serial.begin(9600);
  servod.attach(11);
  servoe.attach(10);
  pinMode(13,OUTPUT);
  mySwitch.enableReceive(0); //interrupt on pin 2
}
void loop(){
  if(mySwitch.available()){
    digitalWrite(13,1);
    var=mySwitch.getReceivedValue();
    Serial.println(var);
    switch(var){
      case '1':
      Serial.println("Received 1");
      servod.write(ang2ms(90));
      servoe.write(ang2ms(90));
      break;
      case '2':
      Serial.println("Received 2");
      servod.write(ang2ms(180));
      servoe.write(ang2ms(0));
      break;
      case '3':
      Serial.println("Received 3");
      servod.write(ang2ms(90));
      servoe.write(ang2ms(0));
      break;
      case '4':
      Serial.println("Received 4");
      servod.write(ang2ms(0));
      servoe.write(ang2ms(180));
      break;
      case '5':
      Serial.println("Received 5");
      servod.write(ang2ms(180));
      servoe.write(ang2ms(90));
      break;
    }
    digitalWrite(13,0);
    mySwitch.resetAvailable();
  }
}
float ang2ms(float ang)
{
  return 1000.0 + ang * 50.0/9.0;
}

But I still have the same problem, when I have the servos plugged it doesn't work, the receiver doesn't receive!
But when I have them plugged, the receiver receives...

I've found that it works if I unplug the servos from the arduino

You can't power servos from the arduino as you have shown. The servos will crash the arduino because they use more power than the arduino can supply. Below is a basic external power supply setup. Also it is often best to get the arduinos working a wired setup, then move to wireless.

servo-wire.jpg

Yes, that was exactly the problem hahaha! now I have it working thank you very much!