Let's Make Robots!

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

  }
  }
  } 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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.

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); // prepares pin A1 to power the laser
  
    servoMain.attach(14); // servo on pin A0
    servoMain.write(90);
    delay (500); // do you need a delay here?
}


void loop() {
    // I believe this could be [180]
    double distancedata[181]; // defines our search deflection array
        
    servoMain.write(0); // turns servo to start position
    delay (500); // gives servo time to get to 0 position
    
    // this for loop will make the robot pan its servo 180 degrees and
    // take a distance measurement
    for (int def=0; def <= 180; def++){  
        servoMain.write(def);  // Turn servo
        delay (5);
    
        // set distance at each deflection in array
        distancedata[def] = ranger(); 
        // I used these commands to check that this section works (it does)
        Serial.print (distancedata[def]);  
        Serial.print (" at ");
        Serial.print (def);
        Serial.print ("  ");
    }
    
    // Sets total number of times that bot will conduct scan
    for (int iterations=0; iterations <= 5; iterations++){
        // scan and check distances from 180 to 0 degrees
        for (int def=180; def >= 0; def--){
	    servoMain.write(def);  // Turn servo
    	    delay (5);

            scan();
        }
        
        // scan and check distances from 0 to 180 degrees
        for (int def=0; def <= 180; def++){  
	    servoMain.write(def);  // Turn servo
    	    delay (5);

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

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);
            
    // checks if current distance is over 10cm different
    // from original distance
    if (scandif >= 10){
        lase();
    }
}
 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); // prepares pin A1 to power the laser
  
    servoMain.attach(14); // servo on pin A0
    servoMain.write(90);
}


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(0); // turns servo to start position
    delay (500); // gives servo time to get to 0 position
    
    // this for loop will make the robot pan its servo 180 degrees and
    // take a distance measurement
    for (int def=0; def <= 180; def++){  
        servoMain.write(def);  // Turn servo
        delay (5);
    
        // set distance at each deflection in array
        distancedata[def] = ranger(); 
        // I used these commands to check that this section works (it does)
        Serial.print (distancedata[def]);  
        Serial.print (" at ");
        Serial.print (def);
        Serial.print ("  ");
    }
    
    // Sets total number of times that bot will conduct scan
    for (int iterations=0; iterations <= 5; iterations++){
        // scan and check distances from 180 to 0 degrees
        for (int def=180; def >= 0; def--){
	    servoMain.write(def);  // Turn servo
    	    delay (5);

            scan();
        }//this is the end of your reverse for loop scan. you missed it.
        
        // scan and check distances from 0 to 180 degrees
        for (int def=0; def <= 180; def++){  
	    servoMain.write(def);  // Turn servo
    	    delay (5);

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

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);
            
    // checks if current distance is over 10cm different
    // from original distance
    if (scandif >= 10){
        lase();
    }
}

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