Thank you very much,adafruit_support_bill wrote:You can use the map() function to do that:5cm-30cm, send signal to servo
while 5cm is 90° and 30cm is 0° (rotation)
Code: Select all
int d=distanceM(); // Messe Distanz und gebe ihn in der Variablen “d” aus Serial.println(d, DEC); // Gib die Variable “d” im Serial Monitor aus. if (d < 30) // only move if less than 30cm { int angle = map(d, 5, 30, 90, 0); // map 5-30cm to 90-0 degrees // add some code to move your servos here }
I am not really sure how to include code for moving servos since I have already this listed
Code: Select all
pwm.setPWM(2, 0, pulseWidth(0));
pwm.setPWM(3, 0, pulseWidth(0));
pwm.setPWM(4, 0, pulseWidth(0));
pwm.setPWM(5, 0, pulseWidth(0));
pwm.setPWM(6, 0, pulseWidth(0));
pwm.setPWM(7, 0, pulseWidth(0));
pwm.setPWM(8, 0, pulseWidth(0));
pwm.setPWM(9, 0, pulseWidth(0));
pwm.setPWM(10, 0, pulseWidth(0));
pwm.setPWM(11, 0, pulseWidth(0));
pwm.setPWM(12, 0, pulseWidth(0));
pwm.setPWM(13, 0, pulseWidth(0));
pwm.setPWM(14, 0, pulseWidth(0));
pwm.setPWM(15, 0, pulseWidth(0));
delay(1000);
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(1, 0, pulseWidth(180));
pwm.setPWM(2, 0, pulseWidth(180));
pwm.setPWM(3, 0, pulseWidth(180));
pwm.setPWM(4, 0, pulseWidth(180));
pwm.setPWM(5, 0, pulseWidth(180));
pwm.setPWM(6, 0, pulseWidth(180));
pwm.setPWM(7, 0, pulseWidth(180));
pwm.setPWM(8, 0, pulseWidth(180));
pwm.setPWM(9, 0, pulseWidth(180));
pwm.setPWM(10, 0, pulseWidth(180));
pwm.setPWM(11, 0, pulseWidth(180));
pwm.setPWM(12, 0, pulseWidth(180));
pwm.setPWM(13, 0, pulseWidth(180));
pwm.setPWM(14, 0, pulseWidth(180));
pwm.setPWM(15, 0, pulseWidth(180));
Code: Select all
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Servo servo;
int angle = 0; // Position des Servos in Grad
int mycell = 0; // Pin des analogen Servos
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define MIN_PULSE_WIDTH 600
#define MAX_PULSE_WIDTH 1200
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50
int trigPin = 8; // Beziehe Signal
int echoPin = 7; // Erhalte Signal
float v=331.5+0.6*20; // Ausbreitungsgeschwindigkeit des Schalls bei 20°C (ca 340 m/s)
void setup() {
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
Serial.begin (9600);
pinMode(trigPin, OUTPUT); // Löse Schaltsignal aus
pinMode(echoPin,INPUT); // Messe die Dauer bis zum Eintreffen des Schalls
}
float distanceM() {
digitalWrite(trigPin,LOW);
delayMicroseconds(3);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
float tUs = pulseIn(echoPin, HIGH);
float t= tUs / 1000.0 / 1000.0 / 1;
float d= t*v;
return d*100;
}
void loop() {
int d=distanceM(); // Messe Distanz und gebe ihn in der Variablen “d” aus
Serial.println(d, DEC); // Gib die Variable “d” im Serial Monitor aus.
if (d < 30) // only move if less than 30cm
{
int angle = map(d, 5, 30, 90, 0); // map 5-30cm to 90-0 degrees
pwm.setPWM(0, 0, pulseWidth(0));
pwm.setPWM(1, 0, pulseWidth(0));
pwm.setPWM(2, 0, pulseWidth(0));
pwm.setPWM(3, 0, pulseWidth(0));
pwm.setPWM(4, 0, pulseWidth(0));
pwm.setPWM(5, 0, pulseWidth(0));
pwm.setPWM(6, 0, pulseWidth(0));
pwm.setPWM(7, 0, pulseWidth(0));
pwm.setPWM(8, 0, pulseWidth(0));
pwm.setPWM(9, 0, pulseWidth(0));
pwm.setPWM(10, 0, pulseWidth(0));
pwm.setPWM(11, 0, pulseWidth(0));
pwm.setPWM(12, 0, pulseWidth(0));
pwm.setPWM(13, 0, pulseWidth(0));
pwm.setPWM(14, 0, pulseWidth(0));
pwm.setPWM(15, 0, pulseWidth(0));
delay(1000);
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(1, 0, pulseWidth(180));
pwm.setPWM(2, 0, pulseWidth(180));
pwm.setPWM(3, 0, pulseWidth(180));
pwm.setPWM(4, 0, pulseWidth(180));
pwm.setPWM(5, 0, pulseWidth(180));
pwm.setPWM(6, 0, pulseWidth(180));
pwm.setPWM(7, 0, pulseWidth(180));
pwm.setPWM(8, 0, pulseWidth(180));
pwm.setPWM(9, 0, pulseWidth(180));
pwm.setPWM(10, 0, pulseWidth(180));
pwm.setPWM(11, 0, pulseWidth(180));
pwm.setPWM(12, 0, pulseWidth(180));
pwm.setPWM(13, 0, pulseWidth(180));
pwm.setPWM(14, 0, pulseWidth(180));
pwm.setPWM(15, 0, pulseWidth(180));
delay(1000);// add some code to move your servos here
}
delay(200);
}
int pulseWidth(int angle)
{
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
return analog_value;
int reading = analogRead(mycell);
int newAngle = reading/5 - 30; // Zwischen Wert 5 und 30 messen
//Serial.print(newAngle);
//Serial.print("\n");
servo.write(newAngle);
delay(50);
}