Servo and virtualwire at the same arduino

Hello guys, I currently have an Arduino project which I dont know how to proceed: I am trying to make a motor rotate using the servotimer 2 but how do I let it rotate smoothly from 0 t o180 non stop while measuring the distance of every angle? I have tried using :

#include <VirtualWire.h> //Load the library
#include <SPI.h>
#include <Wire.h>
#include <ServoTimer2.h>

ServoTimer2 servo2;
#define trigPin 10
#define echoPin 9
int pos = 750;
float duration, distance;
char msg[7];


void setup()
{
  vw_set_tx_pin(12);          // Sets pin D12 as the TX pin
  vw_setup(2000);          // Bits per sec
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  servo2.attach(4);
  // servo.attach(4);    // Vertel het servo object dat de servo op pin 11 is aangesloten
  //servo.write(0);
}

void loop()
{
  for (pos = 750; pos <= 2250; pos+ 100) {
    servo2.write(pos);  // Geef de positie aan de servo door
    delay(10);         // Geef de servo 10ms om naar de positie te draaien

    digitalWrite(trigPin, LOW);
    delay(10);
    digitalWrite(trigPin, HIGH);
    delay(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance = duration / 2 / 29.1;
    Serial.print("Distance: ");
    Serial.println(distance);
    dtostrf(distance, 6, 2, msg);         //converts the float into a char
    vw_send((uint8_t *)msg, strlen(msg)); //transmits the data
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite (12, 0);
    delay(100);
  }

  for (pos = 2250; pos >= 750; pos-100) {
    servo2.write(pos);  // Geef de positie aan de servo door
    delay(10);         // Geef de servo 10ms om naar de positie te draaien
    digitalWrite(trigPin, LOW);
    delay(10);
    digitalWrite(trigPin, HIGH);
    delay(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance = duration / 2 / 29.1;
    Serial.print("Distance: ");
    Serial.println(distance);
    dtostrf(distance, 6, 2, msg);         //converts the float into a char
    vw_send((uint8_t *)msg, strlen(msg)); //transmits the data
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite (12, 0);
    delay(100);
  }
}

but the motor is rotating very slowly (almost unnoticeable)? any clues?

That is easily fixed by following up a simple search for the many tutorials and forum posts on the topic.

It is not that I havent tried anything mate, i wouldnt have asked it I understood it.
for an example how do I let a servo rotate back and fourth with servotimer2?

What have you tried? It should be obvious that you cannot use two servo libraries, as shown in your first post.

If you have questions about code, post the code you are trying to use, using code tags. Explain what you expect to happen, and what happens instead. See the "How to get the best out of the forum" link at the head of every forum topic.

DO NOT try to power the servo from the Arduino 5V, as many bad tutorials suggest, as that may destroy the on board regulator. Use a separate power supply (4xAA batteries will work) and connect the grounds.

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