I’ve been tweaking on the BRAT a lot lately. I added the Pixy Cam to it so it can see color now. My idea is to have the robot only go to certain objects and kick them, but roam around and avoid walls and furniture. Once I got the camera working I put the IR sensor back on the robot. I thought that would be a simple task to implement it back in the code but I was wrong. The only time the IR sensor causes a reaction from the robot is when the pan servo is to the extreme right. I’m using a lot of the same code from the water bottle tutorial. My question is from the IR read portion of the code. [code]int read_Ir()
{
int A = analogRead(3);
delay(1);
int B = analogRead(3);
int number = 0;
for(int x = 0; x < 10; x++)
{
A = analogRead(3);
B = analogRead(3);
while(abs(A - B) > 5)
{
A = B;
B = analogRead(3);
}
irtemp[x] = (A + B) / 2;
}
ir = 0;
for(int x = 0; x < 9; x++)
ir = ir + irtemp[x];
ir = ir / 10;
if(ir > Far)
detpos = pos;
}
[/code]
Not trying to be funny, but what does all of that mean and why go that rout instead of a simple #define IR A3 and use analogRead?
If your question is about 3 vs A3, you can do both analogRead(3) or analogRead(A3). The function will convert you 3 to A3 inside. If your question was about the #define, this code is old and it could have been cleaner and use a #define instead of manually put the port number everywhere.
Thanks Simon. I think you answered it. My question was why did they use int and all the math? If I understand your post correctly, it isn’t really needed, it could be simplified.
The math is needed.
All the math seems to be there to do multiple reading and do some means to reduce “noise” or unwanted peaks.
while(abs(A - B) > 5) : if two consecutive reading a too different, there but be an error so we don’t calculate it.
irtemp[x] = (A + B) / 2: get a mean value of the two readings.
If you only get simple reading, you may have bad unwanted data that affect your real value. Again, this is only an example and there is probably ways to do thing differently and probably even better.