I need to store the value of the distance in the dist array, then calculate the maximun value and print the index associated with this value:
The problem is when starting the nested for inside another for, the servomotor stops working
// Includes the Servo library
#include <Servo.h>
// Array y valores para funcion valor maximo
int max=0;
int angle=0;
int distance=0;
// Defines Tirg and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
// Variables for the duration and the distance
long duration;
//int distance[180];
Servo myServo1; // Creates a servo object for controlling the servo motor
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
myServo1.attach(12); // Defines on which pin is the servo motor attached
}
void loop() {
distance = calculateDistance();
if (distance <= 15 && distance >=2)
{
// rotates the servo motor from 1 to 179 degrees and calculate the distance at each angle
for(int j=90;j>=0;j--){
myServo1.write(j);
delay(30);}
for(int j=0;j<=180;j++){
myServo1.write(j);
int i=0;
int dist [i] = {calculateDistance()};
for (i=0;i=180;i++){ <--------------------------------------
if (dist[i]>max){
max=dist[i];
angle=i;
Serial.print(angle);
delay(30);}
}
}
// Repeats the previous lines from 179 to 90 degrees
for(int j=180;j>90;j--){
myServo1.write(j);
delay(30);
}
}
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}
1 Answer 1
int i=0;
int dist [i] = {
calculateDistance() };
I'm not sure what you are thinking here, but you obviously cannot store data into a zero-length array. You also cannot put a function call to populate an array of variable length.
You need to decide how many elements you need in the array, in advance, and allocate an array of that size.
Maybe more like this:
int dist [180];
for (i=0;i<180;i++){
dist [i] = calculateDistance ();
if (dist[i]>max){
max=dist[i];
angle=i;
Serial.print(angle);
delay(30);
}
}
Also, this is wrong in your code:
for (i=0;i=180;i++){
You need to test for < 180 here. Your for
loop will assign 180 to i and then check to see if it is non-zero, which will always be true.
-
I modified this part of the code and the problem is the same. Actually I do not know if I'm saving the data in the array with this instruction :/StephanyCh– StephanyCh2017年12月27日 21:20:55 +00:00Commented Dec 27, 2017 at 21:20
-
It doesn't do the first for loop instruction, the servo only runs from 0 to 90 degrees and stops working.StephanyCh– StephanyCh2017年12月27日 21:37:03 +00:00Commented Dec 27, 2017 at 21:37
-
You need to put in some debugging "prints". It's hard to tell what is happening. What value gets stored in
distance
, for example?2017年12月28日 04:02:05 +00:00Commented Dec 28, 2017 at 4:02 -
In distance variable gets stored the value of distance which was measured of the Ultrasonic Sensor.StephanyCh– StephanyCh2018年01月01日 22:09:09 +00:00Commented Jan 1, 2018 at 22:09
-
Obviously. I meant what number are you seeing? Zero? 20? 100?2018年01月01日 23:45:08 +00:00Commented Jan 1, 2018 at 23:45