how to use servotimer2

i tried to compile servo.h and virtualwire.h and got the error "vector_11_ etc....etc....." did google search and found that it's the single timer that is being asked by both libraries and the solution is to use servotimer2 so i got it downloaded and installed but what is the command to map a pot like how we use "map()"

map(data[i],0,1023,0,179);

the code bellow is still rough but here's the transmitter code:-

#include <VirtualWire.h>
#include <ServoTimer2.h>
const int numberOfAnalogPins = 4; // how many analog pins to read
int data[numberOfAnalogPins]; // the data buffer
const int dataBytes = numberOfAnalogPins * sizeof(int); 

void setup() 
{
 //    pinMode(7, OUTPUT); 
 // Initialize the IO and ISR
 vw_set_ptt_inverted(true); // Required for DR3100
 vw_set_tx_pin(7);
 vw_setup(2000); // Bits per sec
}

void loop() 
{
 int values = 0;
 // digitalWrite(7, HIGH); 
 for(int i=0; i <= numberOfAnalogPins; i++) 
 {
  data[i] =map(data[i],0,1023,0,179);
 }
 send((byte*)data, dataBytes);
 delay(10); 
 //digitalWrite(7, LOW);
}

void send (byte *data, int nbrOfBytes) 
{
 vw_send(data, nbrOfBytes);
 vw_wait_tx(); // Wait until the whole message is gone
}

the receiver end's job is to write the value it gets from rf signals to the 4 servo:-

#include <SoftwareServo.h>
#include <SoftwareSerial.h>
#include <VirtualWire.h>

SoftwareServo myservo1;
SoftwareServo myservo2;
SoftwareServo myservo3;
SoftwareServo myservo4;

const int numberOfAnalogPins = 4; // how many analog integer values to receive
int data[numberOfAnalogPins]; // the data buffer
int value[numberOfAnalogPins];
// the number of bytes in the data buffer
const int dataBytes = numberOfAnalogPins * sizeof(int);
byte msgLength = dataBytes;

void setup() 
{
 myservo1.attach(9);  //Propeller
 myservo2.attach(10);  //Radar
 myservo3.attach(8);  // Aeleron
 myservo4.attach(12);   // Elevator

 Serial.begin(9600);
 Serial.println("Ready");
 // Initialize the IO and ISR
 vw_set_ptt_inverted(true); // Required for DR3100
 vw_setup(2000); // Bits per sec
 vw_set_rx_pin(11);
 vw_rx_start(); // Start the receiver
}

void loop()
{
 if (vw_get_message((byte*)data, &msgLength))  
 {    // Non-blocking
  Serial.println("Got: ");
  if(msgLength == dataBytes)
  {
   for (int i = 0; i < numberOfAnalogPins; i++) 
   {
    Serial.print("pin ");
    Serial.print(i);
    Serial.print("=");
    Serial.println(data[i]);
    value[i]=data[i];  // Write into the servo
   }

   myservo1.write(value[0]);
   myservo2.write(value[1]);
   myservo3.write(value[2]);
   myservo4.write(value[3]);
   delay(10);
   SoftwareServo::refresh();   //refresh the servo
  }
  else 
  {
   Serial.print("unexpected msg length of ");
   Serial.println(msgLength);
  }
  Serial.println();
 }
}

(the original code "analogue read" the pots then sent those values which were unstable as resistance value kept changing and led to jittery servos)

what is the command to map a pot like how we use "map()"

The ServoTimer2 library changes which timer the servo class uses. It has no impact on the values that you supply to the instances of the ServoTimer2 class. So, the "command" (properly called a function) is still map(), used exactly the same way.

You need to read the analogue pin otherwise you have no value to map()

I have a vague recollection that ServoTimer2 only uses microseconds and not degrees - but I may be wrong.

...R

thanks a lot for the replies guys!

UKHeliBob:
You need to read the analogue pin otherwise you have no value to map()

So what your saying my code does not have anything to map! :o

data[i] =map(data[i],0,1023,0,179);

so to correct it i got to put something like:-

data[i]=read(i)

then put this?

data[i] =map(data[i],0,1023,0,179);

Oh yes i read this too but sadly that's all they type (the person who typed the comment)!!!

Robin2:
I have a vague recollection that ServoTimer2 only uses microseconds and not degrees - but I may be wrong.

...R

Yes, ServoTimer2 uses microseconds as the parameter to its write() method. What is the problem ?

darkSaber:
Oh yes i read this too but sadly that's all they type (the person who typed the comment)!!!

This sounds like a complaint, but I don't understand your comment ?
What should I have said?

...R

Robin2:

darkSaber:
Oh yes i read this too but sadly that's all they type (the person who typed the comment)!!!

This sounds like a complaint, but I don't understand your comment ?
What should I have said?

...R

i don't know the microseconds for all the angles, as in my project it keeps changing depending on my pots (joysticks) and i don't know how to implement it!!!!! :sob:

oh and is the above solution correct:-

So what your saying my code does not have anything to map! :o

data[i] =map(data[i],0,1023,0,179);

so to correct it i got to put something like:-

data[i]=read(i)

then put this?

data[i] =map(data[i],0,1023,0,179);

[/quote]

You can use servo.writeMicroseconds() with the standard servo library. I assumed you knew that and know how to use it.

Microseconds (usecs) are the natural unit for controlling a servo. The angles you provide have to be converted into usecs before the servo can be controlled.

In theory 1000 usecs is the same as 0 deg, 1500 = 90 deg and 2000 = 180 deg. In practice there is some variation between individual servos so you may need to experiment.

What all that means is that the statement you were using with degrees

map(data[i],0,1023,0,179);

would become

map(data[i],0,1023,1000,2000);

for microseconds.

...R

map(data[i],0,1023,1000,2000);

You might as well just add 1000 to data [ i ] - it'd be close enough :wink:

AWOL:
You might as well just add 1000 to data [ i ] - it'd be close enough :wink:

Well spotted.

...R

Thanks a lot for the help but AWOL what do you mean by 'adding 1000'?

AWOL:

map(data[i],0,1023,1000,2000);

You might as well just add 1000 to data [ i ] - it'd be close enough :wink:

darkSaber:
Thanks a lot for the help but AWOL what do you mean by 'adding 1000'?

The value being mapped is between 0 and 1023 and you are moving into a range between 1000 and 2000. Taking some representative input values and comparing the mapped values with the result of just adding 1000 to the input values gives results like this :

Input 100
Mapped 1097
Input + 1000 1100

Input 500
Mapped 1488
Input + 1000 1500

Input 1000
Mapped 1977
Input + 1000 2000

As you can see the mapped value is close to the result of just adding 1000 to the input depending on how accurate you need to be. Adding 1000 to the input avoids the overhead of using the map() function.

You can explore the discrepancy further using this program

void setup() 
{
  Serial.begin(115200);
  for (int input = 0; input < 1024; input++)
  {
    Serial.print(input);
    Serial.print("\t");
    Serial.print(map(input, 0, 1023, 1000, 2000));
    Serial.print("\t");
    Serial.println(input + 1000);
  } 
}

void loop() 
{
}

darkSaber:
Thanks a lot for the help but AWOL what do you mean by 'adding 1000'?

Does nobody try to work anything out for themselves any more?

What does "add" mean?

...R

sorry about that but it's too vague! Didn't you said that 2000 is the max PWM signal for 180 degree?

darkSaber:
sorry about that but it's too vague! Didn't you said that 2000 is the max PWM signal for 180 degree?

I'm sorry too because I don't understand your question. What is too vague ?