Instead of writing random garbage and expecting it to work, save yourself some time and read an elementary book or online tutorial about basic C/C++ syntax and programming.
@michinyon
Very helpful.
Figured it out. I first let it go to the position with the highest light intensity and then get the value x[pos].
Thanks foor the help.
would have been great to see your code...but I guess telling you that 10 times was just not enough...
A wise and helping hint of advice to you, telling us what is happening is far less helpful that showing us your code.
And remember that in C, unlike BASIC, arrays have zero-origin indexing.
@Ps991
10? There were only a few giving helpful hints. You and AWOL are not of them. But thanks anyway for typing.
The ten (maybe that's a little exaggerated) hints were that here we are, 26 replies into a thread in the Programming section of the forum, and you haven't posted any code.
Another thing - arrays are plural, not possessive.
Now the int [170] works
x[1]......... x[171] with the values are stored.
This worries me.
Some various questions:
In "your code", why first go to the max x[pos] before determining the min x[pos? (Seems like a waste of time and processing cycles). Are the values before the max x[pos] skipped?
If the array is not pre-initialized, it could contain random values in memory. Do you pre-fill the complete array prior to determining the above?
What's preventing the servo from going to 0° or 172° to 180°? If there's nothing preventing this, how does "your code" handle this?
Since you haven't shown us your non-working program, I was wondering, by chance, if you would consider showing us your working version?
ffpbar:
The servo is going from 1 tot 171 degrees. While doing that, it is measuring light intensity. So is this case for eg. x[1] is reading a value of 721, x[2] 786, x[3] 210 etc So the minimum is not nessesarily at the beginning. It can be everywhere.
x[1]......... x[171] with the values are stored.
We know that the value of x[1] is measured while servo had postion 1. So if i can get the minimum, by camparison, I gen get the position.
Why do you need to store the readings in an array? Just take readings as your servo sweeps around its arc, and at each step take the min of the current reading and the previous minimum reading. At the start of the arc, set "minimum_previous_reading" to some impossibly high value (since analog inputs go up to 1023, setting the initial minimum to 1024 will be sufficient).
Likewise, if you need the average reading, just add up the readings as you sweep through the arc and divide the total by the number of readings at the end.
If you need the position at the minimum readingm then we are talking:
int previous_minimum_reading = 1024;
int minimum position = -1;
for(int angle = 1; angle <= 171; angle++) {
move_servo_to_position(angle);
int reading = analogRead(SENSOR_PIN);
if(reading < previous_minimum_reading) {
previous_minimum_reading = reading;
minimum position = angle;
}
}
@ffpbar, if you'd told us that English isn't your first language, we could've pointed you at the Dutch section much earlier and saved us all a lot of time.
My point is, even after I explicitly asked you to post you code, you still haven't posted your code.
You were also asked in reply #16, #17, #23, #26, #27, #30, and AWOL even put some implicit requests, but because they were not very clear I did not include them in this list...
For your sanity and mine, PLEASE read this thread!
How To Use This Forum
Paying particular attention to these points in the thread
Point 7
Point 10
Point 11
Point 14
14 even says "Look for hints about what is wanted. For example: "what code?" means: "Please post your code.""
It also says "Answer questions. It is frustrating to have to take three pages of a thread to drag out of someone what code they are using, or what their wiring is."
And it says "Similarly, if you have worked the problem out, as a courtesy to other people reading the thread later, explain what you changed that made it work (eg. post the corrected circuit, or corrected code)."
11 states "Post a complete sketch (program code)! If you don't you waste time while people ask you to do that. However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code. If the problem goes away in the minimal sketch, it wasn't where you thought it was."
It also says "Copy and paste code. Don't retype "from memory" on your mobile phone, in the train."