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?