BondOsman
Üye
- Katılım
- 26 May 2020
- Mesajlar
- 2
- Puanları
- 1
- Yaş
- 32
newping 15 örnegini kullanıyorum. bu şekilde 10 sensr uygun şekilde okuyabiliyorum. istediğim bazı if ifadelerinin karşılanmasına göre belirlediğim açılara dnmesi. ama servoya herhangi bir hareket gelmiyor. yardım etmeniz mümkün mü ?
Kod:
#include <Servo.h>
#include <NewPing.h>
Servo servom ;
#define SONAR_NUM 10 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(13, 16, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(13, 8, MAX_DISTANCE),
NewPing(13, 17, MAX_DISTANCE),
NewPing(12, 9, MAX_DISTANCE),
NewPing(12, 18, MAX_DISTANCE),
NewPing(13, 10, MAX_DISTANCE),
NewPing(13, 15, MAX_DISTANCE),
NewPing(13, 7, MAX_DISTANCE),
NewPing(13, 14, MAX_DISTANCE),
NewPing(12, 11, MAX_DISTANCE),
};
const int position1 = 80;
const int position1_5 = 85;
const int position2 = 90;
const int position2_5 = 95;
const int position3 = 100;
int distance1 = 0;
int distance2 = 0;
int distance3 = 0;
int distance4 = 0;
int distance5 = 0;
const int threshold = 200;
void setup() {
Serial.begin(115200);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
servom.attach(3);
// Other code that *DOESN'T* analyze ping results can go here.
}
void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
// The following code would be replaced with your code that does something with the ping results.
for (uint8_t i = 0; i < SONAR_NUM; i++) {
Serial.print(i);
Serial.print("=");
Serial.print(cm[i]);
Serial.print("cm ");
Serial.println("");}
{
if (cm[0] < 55 && cm[0] > 1)
{ if (cm[1] > 45 && cm[1] < 1)
{
servom.write(10);
delay(150);
}
}
else if (cm[2] < 50 && cm[2] > 1)
{ if ( cm[3] > 45 && cm[3] < 1)
{servom.write (100);
delay(250);
}
}
else if (cm[4] < 60 && cm[4] > 1)
{ if (cm[5] > 45 && cm[5] < 1)
{ servom.write( position1_5);
delay(200);
}
}
else if (cm[6] < 50 && cm[6] > 1)
{ if (cm[7] > 45 && cm[7] < 1)
{ servom.write(position3);
delay(300);
}
}
else if (cm[8] < 60 && cm[8] > 1)
{if (cm[9] > 45 && cm[9] < 1)
{ servom.write(position2_5);
delay(250);
}
}
}
}