My robot needs to do a sweeping scan. Im trying to get it to sweep from 0 to 180 and collect data values from each angle. I have that part down. Im trying to read the array and find the largest value.
This turned out to be harder than I thought. So I made a new Arduino program seperate of the robot's code to make things simpler. This code does a scan from 0 to 180 and prints all the data values (Flame Photodiode), then it waits a second and prints the highest value in the array.
The problem is, my code always gives me the final value in the array, despite the fact that I want the largest value in the array, not the final value.
Here it is: http://pastebin.com/VvsCDBtS
Here is a video of what I see (the robot sweeping, and the serial monitor output.
https://www.youtube.com/watch?v=y4xTQMTYKdk
Alright, thanks for reading/watching.
The reason
The reason that you see only the last value in the array is that you’ve confused the index of an array with the value of the array element at that index.
In line 35 you set Next_Value to Angle + 1. The last time you run the loop when Angle is 179, Next_Angle is set to 180.
In line 36 you compare the index Next_Angle to the value Flame
So the last time you run the loop (which is the only time this counts because of the bug, in line 36 you compare Next_Angle to Highest_Flame_Value. Remembering that Next_Angle is equal to 180, then if 180 > Flame_Data[179], Highest_Flame_Value will equal 180, otherwise it will equal Flame_Data[179].
What you probably want to remember is the index of the highest element of the array. I'll show you a quick version of this but I'm going to use lower case variable names because that is the style I used to working in. This would replace lines 33-44.
I first add another global variable:
int highestFlameIndex = 0;
the following code replaces your code from lines 33-44. I've altered the variables to suit my code styles. You can change the to suit yours or use mine and change yours.
// find the index of the highest element in flameData[]
int highestFlameValue = -1;
for (int angle = 0; angle < 180; angle++) {
if (highestFlameValue < flameData[angle]) {
highestFlameIndex = angle;
highestFlameValue = flameData[angle];
}
}
// At this point, the angle of the highest data point
// should be in highestFlameIndex and the highest
// data point (which corresponds to highestFlameIndex)
// is in highestFlameValue.
This code is not formatted with hilite.me because I'm on an iPad and copying code isn't working right now.
That worked perfectly. I
That worked perfectly. I still dont know how it worked, Im going to read your reply over probably a few more times, and read the code many more times. I’ll be sure to mention you when I post the robot when its finished. Thanks for the help.
Array Size
The storage array should be:
Flame_Data[180];
0 to 179 is 180 entries.
I’m glad it helped. I will
I’m glad it helped. I will admit to writing it while extremely tired because the wife has been keeping me up, and not on a good way.
That probably explains why I didn’t notice that Flame_Data should be declared as int Flame_Data[180] as pointed ou by gallant.
Also written on my iPad which explains why the formatting was crap. Sorry about that,
Why?
I dont understand the code fully yet. But if I need 180 values dont I just need [179] since 0 counts as 1 so that makes 180? Why does it have to be [180]? Im missing something big here…damn
What I don’t understand is
why not check the element number for the highest value as it is found rather than run back through the array? A simple
if new value > old value then highest == current element
No extra time spent running through the array again.
You are right, it would be
You are right, it would be simpler to do that. However, I was trying to keep my explanation as simple as possible.
In C, if you make an array,
In C, if you make an array, you are right that the first element starts with 0, but when declaring the array you must declare as many elements as you use. Since you are storing 180 elements, you need to declare 180 elements.
For example, let’s make a four element array.
int x[4];
This creates four elements that are addressed:
x[0]
x[1]
x[2]
and
x[3]
It is traditional in C to use a for loop to go through an array like this:
for (int i = 0; i < 4; i++) {
}
At least for this four element array.
When you accessed Flame_Data[179] you actually accessed memory that didn’t belong to the array. Unfortunately C let’s you do this so you have to be careful.
Does this make sense?
Some people claim that a C programmer counts out eggs by going 0,1,2,3,4,5,6,7,8,9,10,11; good that’s a dozen.