As part of my thesis work, I am trying to program an Arduino to do something that I am beginning to worry will be relatively complicated. The device itself is just an Arduino Uno R3 with an attached Adafruit TSL2561 light intensity sensor, and three white LEDs.
I need this device to record light intensity at the surface of a body of water, and then again when it reaches the bottom, and then use those variables to calculate its depth. It will then need to use that depth to calculate what the light intensity WOULD be at that depth given a hypothetical surface light intensity, and then maintain that light intensity by activating a set of LEDs when ambient light intensity falls below that threshold.
I've managed to get the mathematical operations worked out on my own, but the trouble is that I am now trying to call these functions used to initially calculate depth and intensity within loop(), which means the device is perpetually trying to recalculate depth based on a light intensity that is not changing, fooling itself into thinking that its depth is 0m, and then emitting too much light.
I have tried using flag variables (i.e. bool alreadyRun = false, etc.), but the problem is that, insofar as I have seen, float variables (which I used for depth and light intensity) can't be called outside the scope of an if statement, let alone a separate function.
Effectively, I need the device to calculate its depth and the light intensity it should maintain ONCE, but I need to be able to apply the results of these calculations in loop(). Does anyone have any suggestions as to how I might accomplish this?