Sharp GP2D12 Analog IR Rangefinder
kokopellix
Posts: 4
Hooked up the GP2D12 as shown on Javelin Users Manual, pages 159-60.
Used program given below.
Upon each iteration the free memory is reduced by 164 bytes and program eventually crashes due to lack of free memory.
The distances recorded range fro 102 to 124 regardless of distance.
Solution???
Program:
import stamp.core.*;
/***
* Test the Javelin Stamp's Analog-To-Digital Class with an IR Range Finder
***/
public class ShortIRRangeTest
{
final static int IRInPin = CPU.pin13;
final static int IROutPin = CPU.pin15;
final static int delayVal = 1000;
public static void main()
{
ADC IRRangeFinder = new ADC(IRInPin, IROutPin);
System.out.println("EEPROM available = " + EEPROM.size());
while(true) // Monitor IR rangefinder forever
{
System.out.println("Memory available = " + Memory.freeMemory());
int totalRange = 0;
for (int i = 0; i < 5; i++)
{
CPU.delay(delayVal);
int intRange = IRRangeFinder.value();
totalRange = totalRange + intRange;
}
int avgRange = totalRange / 5;
System.out.println("Range is " + avgRange);
}
}
}
Used program given below.
Upon each iteration the free memory is reduced by 164 bytes and program eventually crashes due to lack of free memory.
The distances recorded range fro 102 to 124 regardless of distance.
Solution???
Program:
import stamp.core.*;
/***
* Test the Javelin Stamp's Analog-To-Digital Class with an IR Range Finder
***/
public class ShortIRRangeTest
{
final static int IRInPin = CPU.pin13;
final static int IROutPin = CPU.pin15;
final static int delayVal = 1000;
public static void main()
{
ADC IRRangeFinder = new ADC(IRInPin, IROutPin);
System.out.println("EEPROM available = " + EEPROM.size());
while(true) // Monitor IR rangefinder forever
{
System.out.println("Memory available = " + Memory.freeMemory());
int totalRange = 0;
for (int i = 0; i < 5; i++)
{
CPU.delay(delayVal);
int intRange = IRRangeFinder.value();
totalRange = totalRange + intRange;
}
int avgRange = totalRange / 5;
System.out.println("Range is " + avgRange);
}
}
}
Comments
What happens to each point (free memory and recorded distance) when the following changes are made?
ps: please place your formatted code in [ code ] tags in future posts. It makes it easier for everyone to read.
In Java, when you are going this, the JVM is creating a new string which will contain "Memory available = XXXX" and pass it to the println function. When you are going out of the function, you loose the reference of the string and so the memory.
Replace that by:
Same thing for the Range display.
It's so a pleasure to code for the Javelin! I really hope Java will be working for the propeller...
JM
Changing the println statements from: System.out.println("The range is: " + range);
to: System.out.print("The range is: ");
System.out.println(range);
stops the bleeding memory.
The range finder value will have to be, I think, adjusted by changing resistor values to mesh more closely with the rangefinder output.
Thanks.
The rangefinder values will, I now think, have to be corrected by changing the resistor values to generate a reference voltage that more closely matches the rangefinder output.
I appreciate it.