Torrentula
31.08.2011, 17:31
Hallo RNler!
Bin gerade dabei, einen Roboter zu bauen, der einen Arduino Uno als Haupt-Controllerboard verwendet. Ich weiß nicht, wie viele sich hier genauer mit dem Thema Arduino befasst haben aber ich frage trotzdem.
Ich möchte mit den Pins 9 und 10 meine Motoren in der Geschwindigkeit regeln. Wenn ich nun aber die Servo-Lib verwende, sind diese für PWM allerdings nicht mehr verfügbar. Deshalb trenne ich den Servo immer wieder mittels der Methode detach() von Pin 6 um auf den Pins 9 und 10 wieder ein PWM-Signal ausgeben zu können. Obwohl nun kein Servo mehr "attached" ist, fahren die Motoren nicht wieder an. Auch der Servo bewegt sich kein Stück.
#include <Servo.h>
int duration; //Stores duration of pulse in
int distance; // Stores distance
const int sensorpin = 4;
Servo servo;
int distR;
int distL;
void getDistance(){
distance = 0;
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(sensorpin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(sensorpin, LOW); // Send pin low again before waiting for pulse back in
pinMode(sensorpin, INPUT);
duration = pulseIn(sensorpin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distance = duration/58; // Dividing this by 58 gives us a distance in cm
}
void getDistanceR(){
servo.attach(6); // servo attached to pin 6
servo.write(0); // right position
servo.detach(); // detach servo
distR = 0;
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(sensorpin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(sensorpin, LOW); // Send pin low again before waiting for pulse back in
pinMode(sensorpin, INPUT);
duration = pulseIn(sensorpin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distR = duration/58; // Dividing this by 58 gives us a distance in cm
}
void getDistanceL(){
servo.attach(6); // servo attached to pin 6
servo.write(170); // left position
servo.detach(); // detach servo
distL = 0;
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(sensorpin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(sensorpin, LOW); // Send pin low again before waiting for pulse back in
pinMode(sensorpin, INPUT);
duration = pulseIn(sensorpin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distL = duration/58; // Dividing this by 58 gives us a distance in cm
}
void rightForward(void){
digitalWrite(8, HIGH);
digitalWrite(11, LOW);
}
void leftForward(void){
digitalWrite(12, LOW);
digitalWrite(7, HIGH);
}
void rightBackward(){
digitalWrite(8, LOW);
digitalWrite(11, HIGH);
}
void leftBackward(void){
digitalWrite(12, HIGH);
digitalWrite(7, LOW);
}
void accelerate(){
analogWrite(10, 0); // R
analogWrite(9, 0); // L
for(int i = 0; i < 100; i += 5){
analogWrite(10, i); // R
analogWrite(9, i); // L
delay(40);
}
analogWrite(10, 100); // R
analogWrite(9, 100); // L
delay(40);
}
void slowdown(){
for(int i = 100; i > 0; i -= 5){
analogWrite(10, i); // R
analogWrite(9, i); // L
delay(40);
}
analogWrite(10, 0); // R
analogWrite(9, 0); // L
}
void setup(){
// right motor
pinMode(8, OUTPUT); // 1A
pinMode(11, OUTPUT); // 1B
pinMode(10, OUTPUT); // PWM
// left Motor
pinMode(9, OUTPUT); // PWM
pinMode(12, OUTPUT); // 2A
pinMode(7, OUTPUT); // 2B
//**** SRF05 Power ****
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
//*********************
}
void loop(){
servo.attach(6); // servo attached to pin 6
servo.write(80); // middle position
servo.detach(); // detach servo
getDistance();
rightForward();
leftForward();
accelerate();
while(distance > 30){
getDistance();
delay(100);
}
slowdown();
getDistanceR();
getDistanceL();
if(distL < distR){
servo.attach(6); // servo attached to pin 6
servo.write(80); // middle position
servo.detach(); // detach servo
rightBackward();
leftForward();
accelerate();
while(distance < distR){
getDistance();
delay(100);
}
slowdown();
}
if(distR < distL){
servo.attach(6); // servo attached to pin 6
servo.write(80); // middle position
servo.detach(); // detach servo
rightForward();
leftBackward();
accelerate();
while(distance < distL){
getDistance();
delay(100);
}
slowdown();
}
}
Hoffe jemand kann mir helfen!
MfG
Torrentula
Bin gerade dabei, einen Roboter zu bauen, der einen Arduino Uno als Haupt-Controllerboard verwendet. Ich weiß nicht, wie viele sich hier genauer mit dem Thema Arduino befasst haben aber ich frage trotzdem.
Ich möchte mit den Pins 9 und 10 meine Motoren in der Geschwindigkeit regeln. Wenn ich nun aber die Servo-Lib verwende, sind diese für PWM allerdings nicht mehr verfügbar. Deshalb trenne ich den Servo immer wieder mittels der Methode detach() von Pin 6 um auf den Pins 9 und 10 wieder ein PWM-Signal ausgeben zu können. Obwohl nun kein Servo mehr "attached" ist, fahren die Motoren nicht wieder an. Auch der Servo bewegt sich kein Stück.
#include <Servo.h>
int duration; //Stores duration of pulse in
int distance; // Stores distance
const int sensorpin = 4;
Servo servo;
int distR;
int distL;
void getDistance(){
distance = 0;
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(sensorpin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(sensorpin, LOW); // Send pin low again before waiting for pulse back in
pinMode(sensorpin, INPUT);
duration = pulseIn(sensorpin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distance = duration/58; // Dividing this by 58 gives us a distance in cm
}
void getDistanceR(){
servo.attach(6); // servo attached to pin 6
servo.write(0); // right position
servo.detach(); // detach servo
distR = 0;
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(sensorpin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(sensorpin, LOW); // Send pin low again before waiting for pulse back in
pinMode(sensorpin, INPUT);
duration = pulseIn(sensorpin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distR = duration/58; // Dividing this by 58 gives us a distance in cm
}
void getDistanceL(){
servo.attach(6); // servo attached to pin 6
servo.write(170); // left position
servo.detach(); // detach servo
distL = 0;
pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, LOW); // Make sure pin is low before sending a short high to trigger ranging
delayMicroseconds(2);
digitalWrite(sensorpin, HIGH); // Send a short 10 microsecond high burst on pin to start ranging
delayMicroseconds(10);
digitalWrite(sensorpin, LOW); // Send pin low again before waiting for pulse back in
pinMode(sensorpin, INPUT);
duration = pulseIn(sensorpin, HIGH); // Reads echo pulse in from SRF05 in micro seconds
distL = duration/58; // Dividing this by 58 gives us a distance in cm
}
void rightForward(void){
digitalWrite(8, HIGH);
digitalWrite(11, LOW);
}
void leftForward(void){
digitalWrite(12, LOW);
digitalWrite(7, HIGH);
}
void rightBackward(){
digitalWrite(8, LOW);
digitalWrite(11, HIGH);
}
void leftBackward(void){
digitalWrite(12, HIGH);
digitalWrite(7, LOW);
}
void accelerate(){
analogWrite(10, 0); // R
analogWrite(9, 0); // L
for(int i = 0; i < 100; i += 5){
analogWrite(10, i); // R
analogWrite(9, i); // L
delay(40);
}
analogWrite(10, 100); // R
analogWrite(9, 100); // L
delay(40);
}
void slowdown(){
for(int i = 100; i > 0; i -= 5){
analogWrite(10, i); // R
analogWrite(9, i); // L
delay(40);
}
analogWrite(10, 0); // R
analogWrite(9, 0); // L
}
void setup(){
// right motor
pinMode(8, OUTPUT); // 1A
pinMode(11, OUTPUT); // 1B
pinMode(10, OUTPUT); // PWM
// left Motor
pinMode(9, OUTPUT); // PWM
pinMode(12, OUTPUT); // 2A
pinMode(7, OUTPUT); // 2B
//**** SRF05 Power ****
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(5, LOW);
//*********************
}
void loop(){
servo.attach(6); // servo attached to pin 6
servo.write(80); // middle position
servo.detach(); // detach servo
getDistance();
rightForward();
leftForward();
accelerate();
while(distance > 30){
getDistance();
delay(100);
}
slowdown();
getDistanceR();
getDistanceL();
if(distL < distR){
servo.attach(6); // servo attached to pin 6
servo.write(80); // middle position
servo.detach(); // detach servo
rightBackward();
leftForward();
accelerate();
while(distance < distR){
getDistance();
delay(100);
}
slowdown();
}
if(distR < distL){
servo.attach(6); // servo attached to pin 6
servo.write(80); // middle position
servo.detach(); // detach servo
rightForward();
leftBackward();
accelerate();
while(distance < distL){
getDistance();
delay(100);
}
slowdown();
}
}
Hoffe jemand kann mir helfen!
MfG
Torrentula