Shop OBEX P1 Docs P2 Docs Learn Events
Sharp GP2D12 Analog IR Rangefinder — Parallax Forums

Sharp GP2D12 Analog IR Rangefinder

kokopellixkokopellix Posts: 4
edited 2011-08-17 09:36 in General Discussion
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);
}
}

}

Comments

  • SRLMSRLM Posts: 5,045
    edited 2011-08-16 02:17
    Preface: I'm not a Javalin expert...

    What happens to each point (free memory and recorded distance) when the following changes are made?
    /***
    * 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 = 100; //Lowered the delay a bit...
    
    	public static void main()
    	{
    		ADC IRRangeFinder = new ADC(IRInPin, IROutPin);
    
    		System.out.println("EEPROM available = " + EEPROM.size());
    
    		//Declared the variables outside the loop...
    		int totalRange;
    		int i;
    		int avgRange;
    		int intRange;
    
    		while(true) // Monitor IR rangefinder forever
    		{
    			System.out.println("Memory available = " + Memory.freeMemory());
    
    			totalRange = 0;
    			for (i = 0; i < 5; i++)
    			{
    				CPU.delay(delayVal);
    
    				intRange = IRRangeFinder.value();
    				totalRange = totalRange + intRange;
    			}
    
    			avgRange = totalRange / 5;
    			System.out.println("Range is " + avgRange);
    		}
    	}
    
    }
    

    ps: please place your formatted code in [ code ] tags in future posts. It makes it easier for everyone to read.
  • jmspaggijmspaggi Posts: 629
    edited 2011-08-16 04:56
    The issue is here:
    System.out.println("Memory available = " + Memory.freeMemory());
    

    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:
    System.out.print("Memory available = ")
    System.out.println(Memory.freeMemory());
    

    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
  • kokopellixkokopellix Posts: 4
    edited 2011-08-17 09:32
    This change has no effect.

    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.
  • kokopellixkokopellix Posts: 4
    edited 2011-08-17 09:36
    You got it. Should have read page 196 of the Javelin Stamp User's Manual. I appreciate it.

    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.
Sign In or Register to comment.