Trying to fill data into an array for the first time

Let me apologize right up front if I sound stupid here, this is my first adventure into programming and I'm trying to learn.

What I'm trying to do is use an ultrasonic rangefinder attached to a servo to scan the area in front of the robot in a 180 degree arc, and record the distances at each degree in an array.  Then I want to have my bot scan the area over and over, re-looking that 180 degree arc.  When one of the distances changes, that should trigger the bot to fire a laser at the spot where the "intruder" is.

I think maybe I'm having trouble because I have an "if" statement within a "for" loop, but I'm not sure.  Maybe its the "continue" command? I don't know, there's a lot of commands I haven't used before in this bit of code.  Right now the bot is properly recording the ranges at different degrees, but then starts slewing the laser back and forth when it goes into its scan mode.  If nothing else everything should stop when the laser fires... or so I would think.

 

Here's the code:

#define trigPin 6
#define echoPin 7

// servo stuff
#include <Servo.h> //include servo library
Servo servoMain; // Define our Servo.  It will start set at 90 degrees

int laser1 = 5;  // set laser trigger to dig pin 5


void setup()
{
  Serial.begin (9600);  //Serial input for rangefinder if we hook it up to computer
  pinMode(trigPin, OUTPUT); //rangefinder setup
  pinMode(echoPin, INPUT);  //rangefinder setup
 
  servoMain.attach(14); // servo on pin A0
 
  pinMode (laser1, OUTPUT); // prepares pin A1 to power the laser
 
  servoMain.write(90);
}


void loop()
{
  int distancedata[181]; // defines our search deflection array
  int iterations;  // variable for total number of iterations
  int def; // sets variable for deflection
  int duration;
  int currentdist;
  int scandif;
 
  servoMain.write(0); // turns servo to start position
  delay (500); // gives servo time to get to 0 position
 
  for (int def=0; def <= 180; def++){  // this for loop will make the robot pan its servo 180 degrees and take a distance measurement
    servoMain.write(def);  // Turn servo
    delay (5);
   
    digitalWrite(trigPin, HIGH); // get distance at current deflection
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distancedata[def] = (duration/2) / 29.1; // set distance at each deflection in array
    Serial.print (distancedata[def]);  // I used these commands to check that this section works (it does)
    Serial.print (" at ");
    Serial.print (def);
    Serial.print ("  ");
  }
 
  for (int iterations=0; iterations <= 5; iterations++){ // Sets total number of times that bot will conduct scan
 
   for (int def=180; def >= 0; def--){  // scan and check distances from 180 to 0 degrees
    servoMain.write(def);  // Turn servo
    delay (5);
     
    digitalWrite(trigPin, HIGH); // get distance at current deflection
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH); 
    currentdist = (duration/2) / 29.1; // set distance at each deflection in array
   
    scandif = (distancedata[def] - currentdist);
   
    if (scandif >= 10){  // checks if current distance is over 10cm different from original distance
      digitalWrite(laser1, HIGH); // fires on new target if not
      delay(1500);
      digitalWrite(laser1, LOW);
    }
    else {  // continues scan if current distance is not more than 10 cm of original distance
      continue;
    }
 
   for (int def=0; def <= 1; def++){  // scan and check distances from 0 to 180 degrees
    servoMain.write(def);  // Turn servo
    delay (5);
     
    digitalWrite(trigPin, HIGH); // get distance at current deflection
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH); 
    currentdist = (duration/2) / 29.1; // set distance at each deflection in array
   
    scandif = (distancedata[def] - currentdist);
   
    if (scandif >= 10){  // checks if current distance is over 10 cm different from original distance
      digitalWrite(laser1, HIGH); // fires on new target if not
      delay(1500);
      digitalWrite(laser1, LOW);
      continue;
    }
    else {  // continues scan if current distance is not more than 10 cm of original distance
      continue;
    }

  servoMain.write(0); // turns servo to start position
  delay (500); // gives servo time to get to 0 position

  }
  }
  } 

I rearranged your code a bit.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#define trigPin 6
#define echoPin 7

// servo stuff
#include <Servo.h> //include servo library
Servo servoMain; // Define our Servo. It will start set at 90 degrees

int laser1 = 5; // set laser trigger to dig pin 5

void setup() {
//Serial input for rangefinder if we hook it up to computer
Serial.begin (9600);
pinMode(trigPin, OUTPUT); //rangefinder setup
pinMode(echoPin, INPUT); //rangefinder setup

pinMode (laser1, OUTPUT); <span style="color: #888888;">// prepares pin A1 to power the laser</span>

servoMain.attach(<span style="color: #0000dd; font-weight: bold;">14</span>); <span style="color: #888888;">// servo on pin A0</span>
servoMain.write(<span style="color: #0000dd; font-weight: bold;">90</span>);

}

void loop() {
int distancedata[181]; // defines our search deflection array
// your iterations variable is like your def variable
//int iterations; // variable for total number of iterations
// your def variable is only used inside the loops
// it doesn’t need to be defined as a variable outside of the loops
//int def; // sets variable for deflection
int duration;
int currentdist;
int scandif;

servoMain.write(<span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">// turns servo to start position</span>
delay (<span style="color: #0000dd; font-weight: bold;">500</span>); <span style="color: #888888;">// gives servo time to get to 0 position</span>

<span style="color: #888888;">// this for loop will make the robot pan its servo 180 degrees and</span>
<span style="color: #888888;">// take a distance measurement</span>
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> def=<span style="color: #0000dd; font-weight: bold;">0</span>; def &lt;= <span style="color: #0000dd; font-weight: bold;">180</span>; def++){  
    servoMain.write(def);  <span style="color: #888888;">// Turn servo</span>
    delay (<span style="color: #0000dd; font-weight: bold;">5</span>);

    <span style="color: #888888;">// set distance at each deflection in array</span>
    distancedata[def] = ranger(); 
    <span style="color: #888888;">// I used these commands to check that this section works (it does)</span>
    Serial.print (distancedata[def]);  
    Serial.print (<span style="color: #dd2200; background-color: #fff0f0;">" at "</span>);
    Serial.print (def);
    Serial.print (<span style="color: #dd2200; background-color: #fff0f0;">"  "</span>);
}

<span style="color: #888888;">// Sets total number of times that bot will conduct scan</span>
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> iterations=<span style="color: #0000dd; font-weight: bold;">0</span>; iterations &lt;= <span style="color: #0000dd; font-weight: bold;">5</span>; iterations++){
    <span style="color: #888888;">// scan and check distances from 180 to 0 degrees</span>
    <span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> def=<span style="color: #0000dd; font-weight: bold;">180</span>; def &gt;= <span style="color: #0000dd; font-weight: bold;">0</span>; def--){
    servoMain.write(def);  <span style="color: #888888;">// Turn servo</span>
	    delay (<span style="color: #0000dd; font-weight: bold;">5</span>);

        scan();
    }<span style="color: #888888;">//this is the end of your reverse for loop scan. you missed it.</span>
    
    <span style="color: #888888;">// scan and check distances from 0 to 180 degrees</span>
    <span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> def=<span style="color: #0000dd; font-weight: bold;">0</span>; def &lt;= <span style="color: #0000dd; font-weight: bold;">180</span>; def++){  
    servoMain.write(def);  <span style="color: #888888;">// Turn servo</span>
	    delay (<span style="color: #0000dd; font-weight: bold;">5</span>);

        scan();
    }
servoMain.write(<span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">// turns servo to start position</span>
delay (<span style="color: #0000dd; font-weight: bold;">500</span>); <span style="color: #888888;">// gives servo time to get to 0 position</span>
}

}

double ranger() {
digitalWrite(trigPin, HIGH); // get distance at current deflection
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return (duration/2) / 29.1;
}

void lase() {
digitalWrite(laser1, HIGH); // fires on new target if not
delay(1500);
digitalWrite(laser1, LOW);
}

void scan() {
// begin comparing distance to each deflection in array
currentdist = ranger();
scandif = (distancedata[def] - currentdist);

<span style="color: #888888;">// checks if current distance is over 10cm different</span>
<span style="color: #888888;">// from original distance</span>
<span style="color: #008800; font-weight: bold;">if</span> (scandif &gt;= <span style="color: #0000dd; font-weight: bold;">10</span>){
    lase();
}

}

I used http://hilite.me/ and the pastie style. Hopefully someone will correct me, if I am totally off. :slight_smile:

Okay, it took me a bit to

Okay, it took me a bit to understand it, but I’ve gone through this now and I think I understand the differences from my original code.

I get the re-organization of the code, this will be easier to work with.

Aside from that re-organization… I think what I’m reading is that the end brackets from the first for loop is the only true mistake in here, is that correct?

I’ll load this code up in the bot and see if ti behaves properly.

It looks like I made an error. :slight_smile:

int distancedata[181];

int currentdist;
int scandif;

 

needs to be
double distancedata[181]; // I think it could be 180
double currentdist;
double scandif;

because
double ranger() { //means that this function will return a double and not an int
I changed it to a double, because, you have floating point values in your calculation.

You will also need to move
int duration;
double currentdist;
double scandif;
above void setup(), because, with where they are they will be visible only to the loop() function.

Reworked it should look something like: 

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define trigPin 6
#define echoPin 7

// servo stuff
#include <Servo.h> //include servo library
Servo servoMain; // Define our Servo. It will start set at 90 degrees

int laser1 = 5; // set laser trigger to dig pin 5
double duration;
double currentdist;
double scandif;
//the above can also be written
//double duration, currentdist, scandif;

void setup() {
//Serial input for rangefinder if we hook it up to computer
Serial.begin (9600);
pinMode(trigPin, OUTPUT); //rangefinder setup
pinMode(echoPin, INPUT); //rangefinder setup

pinMode (laser1, OUTPUT); <span style="color: #888888;">// prepares pin A1 to power the laser</span>

servoMain.attach(<span style="color: #0000dd; font-weight: bold;">14</span>); <span style="color: #888888;">// servo on pin A0</span>
servoMain.write(<span style="color: #0000dd; font-weight: bold;">90</span>);
delay (<span style="color: #0000dd; font-weight: bold;">500</span>); <span style="color: #888888;">// do you need a delay here?</span>

}

void loop() {
// I believe this could be [180]
double distancedata[181]; // defines our search deflection array

servoMain.write(<span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">// turns servo to start position</span>
delay (<span style="color: #0000dd; font-weight: bold;">500</span>); <span style="color: #888888;">// gives servo time to get to 0 position</span>

<span style="color: #888888;">// this for loop will make the robot pan its servo 180 degrees and</span>
<span style="color: #888888;">// take a distance measurement</span>
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> def=<span style="color: #0000dd; font-weight: bold;">0</span>; def &lt;= <span style="color: #0000dd; font-weight: bold;">180</span>; def++){  
    servoMain.write(def);  <span style="color: #888888;">// Turn servo</span>
    delay (<span style="color: #0000dd; font-weight: bold;">5</span>);

    <span style="color: #888888;">// set distance at each deflection in array</span>
    distancedata[def] = ranger(); 
    <span style="color: #888888;">// I used these commands to check that this section works (it does)</span>
    Serial.print (distancedata[def]);  
    Serial.print (<span style="color: #dd2200; background-color: #fff0f0;">" at "</span>);
    Serial.print (def);
    Serial.print (<span style="color: #dd2200; background-color: #fff0f0;">"  "</span>);
}

<span style="color: #888888;">// Sets total number of times that bot will conduct scan</span>
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> iterations=<span style="color: #0000dd; font-weight: bold;">0</span>; iterations &lt;= <span style="color: #0000dd; font-weight: bold;">5</span>; iterations++){
    <span style="color: #888888;">// scan and check distances from 180 to 0 degrees</span>
    <span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> def=<span style="color: #0000dd; font-weight: bold;">180</span>; def &gt;= <span style="color: #0000dd; font-weight: bold;">0</span>; def--){
    servoMain.write(def);  <span style="color: #888888;">// Turn servo</span>
	    delay (<span style="color: #0000dd; font-weight: bold;">5</span>);

        scan();
    }
    
    <span style="color: #888888;">// scan and check distances from 0 to 180 degrees</span>
    <span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #888888; font-weight: bold;">int</span> def=<span style="color: #0000dd; font-weight: bold;">0</span>; def &lt;= <span style="color: #0000dd; font-weight: bold;">180</span>; def++){  
    servoMain.write(def);  <span style="color: #888888;">// Turn servo</span>
	    delay (<span style="color: #0000dd; font-weight: bold;">5</span>);

        scan();
    }
servoMain.write(<span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">// turns servo to start position</span>
delay (<span style="color: #0000dd; font-weight: bold;">500</span>); <span style="color: #888888;">// gives servo time to get to 0 position</span>
}

}

double ranger() {
digitalWrite(trigPin, HIGH); // get distance at current deflection
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return (duration/2) / 29.1;
}

void lase() {
digitalWrite(laser1, HIGH); // fires on new target if not
delay(1500);
digitalWrite(laser1, LOW);
}

void scan() {
// begin comparing distance to each deflection in array
currentdist = ranger();
scandif = (distancedata[def] - currentdist);

<span style="color: #888888;">// checks if current distance is over 10cm different</span>
<span style="color: #888888;">// from original distance</span>
<span style="color: #008800; font-weight: bold;">if</span> (scandif &gt;= <span style="color: #0000dd; font-weight: bold;">10</span>){
    lase();
}

}