I am making a coin sorter, and have been writing some code for it.
After some testing and debugging I found some strange behaviors in the execution.
I will first explain the purpose of the method which behaves strange before showing down my code, results and errors.
So I've made a method for scanning the RGB values of a coin. The method is called when a coin is detected, and will be executed untill the coin is completly scanned.
Here is a picture of my basic coin scanner:

I'll post here the code of my class I made, but you only have to see to the method 'ScanCoin'.
- Code: Select all
public class CoinSensor
{
final int BOUNDARY_VALUE;
SensorPort port;
public CoinSensor(SensorPort p)
{
this.port = p;
port.enableColorSensor();
port.setType(SensorConstants.TYPE_COLORFULL);
BOUNDARY_VALUE = 200;
}
public boolean coinIsDetected()
{
}
public void scanCoin(Coin coin)
{
boolean coinIsScanned = false;
int actualDegree, degree = -1;
int returnedValue;
final int ARRAY_LENGTH = 3;
int[] RGBvalues = new int[4];
Motor.A.resetTachoCount();
Motor.A.forward();
while(!coinIsScanned)
{
actualDegree = Motor.A.getTachoCount();
if(actualDegree > degree)
{
returnedValue = port.readRawValues(RGBvalues);
if(RGBvalues[0] > BOUNDARY_VALUE || RGBvalues[0] == 0)
{
try
{
if(RGBvalues[0] == 0)
{
for(int i = 0; i < ARRAY_LENGTH; i++) coin.RGBvalues[actualDegree][i] = -50;
}
if(returnedValue < 0)
{
for(int i = 0; i < ARRAY_LENGTH; i++) coin.RGBvalues[actualDegree][i] = -100;
}
if(RGBvalues[0] != 0 && returnedValue > 0)
{
for(int i = 0; i < ARRAY_LENGTH; i++) coin.RGBvalues[actualDegree][i] = (short) RGBvalues[i];
}
}
catch(ArrayIndexOutOfBoundsException exception)
{
coinIsScanned = true;
}
}
else
{
coinIsScanned = true;
}
degree = actualDegree;
}
}
}
}
So the method let the coin move under the colorsensor. And every time that the motor has turned 1 degree, the sensorport will read the RGB values from the coin.
The multiple if-instructions I wrote for debugging my program.
And as you can see in my results, there are a few errors which occurs.
First of all, the method 'readRawValues' sometimes doesn't fill the given array with RGB values.
Second, when the motor speed is verry low, the method 'readRawValues' returns verry much an error (equals to value -1).
Then, when the motor has an average speed, the method 'readRawValues' needs a long time for scanning the RGB values.
And last, when the motor speed is high, no errors occures!
Here is the url to my excel document with my results, it maybe better for downloading it rather than viewing online.
https://docs.google.com/open?id=0B1zd8u803341b0ZLNjR3OU5wUWc
The document has three tabs, each with the results of one specified motor speed.
First of all, thank you for reading this all.
And please reply if you know the origin of this behavior. Ask me for more information if some things weren't clear.
Gratz, Stone!
