Go Down

Topic: Help please with creating a Rangefinder with Processing. (Read 628 times) previous topic - next topic

biotech

Hi,

I am trying to take on this project:
http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-part-3-visualising-the-data-from-sharp-infrared-range-finder/

The original code for Arduino:
Code: [Select]

/*
luckylarry.co.uk
Radar Screen Visualisation for Sharp GP2Y0A02 IR range finder
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/
#include <Servo.h>            // include the standard servo library
Servo leftRightServo;         // set a variable to map the servo
int leftRightPos = 0;         // set a variable to store the servo position
const int numReadings = 10;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
float total = 0;              // the total of all readings must be a float to allow totaling of float values
int average = 0;              // the average
int IRpin = 1;                // analog pin for reading the IR sensor

/* setup the pins, servo and serial port */
void setup() {
 leftRightServo.attach(9);
 // initialize the serial port:
 Serial.begin(9600);
}

/* begin rotating the servo and getting sensor values */
void loop() {
 for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) {  // going left to right.                                
   leftRightServo.write(leftRightPos);            
     for (index = 0; index<=numReadings;index++) {            // take x number of readings from the sensor and average them
       float volts = analogRead(IRpin)*0.0048828125;          // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
       float distance = 65*pow(volts, -1.10);                 // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
       total = total + distance;                              // update total
       delay(20);
     }
   average = (int) total/numReadings;                         // create average reading CAST TO INT!! remove the decimal places

   if (index >= numReadings)  {                               // reset the counts when at the last item of the array    
     index = 0;          
     total = 0;    
   }
   Serial.print("X");                                         // print leading X to mark the following value as degrees
   Serial.print(leftRightPos);                                // current servo position
   Serial.print("V");                                         // preceeding character to separate values
   Serial.println(average);                                   // average of sensor readings
 }
 /*
 start going right to left after we got to 180 degrees
 same code as above
 */
 for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) {  // going right to left                                
   leftRightServo.write(leftRightPos);            
   for (index = 0; index<=numReadings;index++) {
     float volts = analogRead(IRpin)*0.0048828125;            // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
     float distance = 65*pow(volts, -1.10);                   // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
     total = total + distance;
     delay(20);
   }
   average = (int) total/numReadings;  
   if (index >= numReadings)  {          
     index = 0;          
     total = 0;    
   }
   Serial.print("X");
   Serial.print(leftRightPos);
   Serial.print("V");
   Serial.println(average);
  }  
}


However I have two problems at the moment:

1) I don´t have an IR sensor but an Ultrasonic HC-SRO4 instead. I don´t know what to modify on the original code to make it work.

2) When I download the Processing code I get the error message "ArrayIndexOutOfBoundExeption: 1"
The error seems to came from the following line of code:
Code: [Select]

myPort = new Serial(this, Serial.list()[1], 9600);


My wiring:

- Servo attached to pin 9

- Ultrasonic Sensor: Trig to pin 6 and Eco to pin 7

- COM port assigned by Arduino at the moment: 21

Please help me. Thanks!

Riva

If you put the below line in processing before the error line what comm ports does it list? For me the arduino is always on entry [0] not [1]
Code: [Select]
   println(Serial.list());                                 // List all the available serial ports
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

biotech

Thanks for answering.

I put the line in the code in Processing. As I expected it printed "COM21".

The error line in Processing goes like this:

myPort = new Serial(this, Serial.list()[21], 9600);

Error: ArrayIndexOutofBound: 21

I don´t know what is wrong!


Riva


I put the line in the code in Processing. As I expected it printed "COM21".

The error line in Processing goes like this:

myPort = new Serial(this, Serial.list()[21], 9600);

Error: ArrayIndexOutofBound: 21

I don´t know what is wrong!

If the only printed result was COM21 then it is the first entry in the array of comm ports so you should be using
Code: [Select]
myPort = new Serial(this, Serial.list()[0], 9600);
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

groundfungus


Go Up