Close

The Arduino is Not Playing Ball?

A project log for Daylight Geolocator Remix

This is a remix of https://hackaday.io/project/28550-light-level-geolocator

agpcooperagp.cooper 02/25/2018 at 14:210 Comments

Memory Corruption

I have been getting EEPROM memory corruption. Usually at the same memory locations. I also know that the code modules work in isolation (if "jump stated") and are being called but I am not getting a successful run. So I am getting frustrated.

I wrote a bit of code to test the EEPROM and it passes. No problems! So what is going wrong?

In my code you cannot read/write addresses within a page going backwards without corruption (writing forward is okay) but the test code has no problems?

The only thing I found was that Wire.begin() has to be called before access the AT24Cxx library but not if the RTC library is called first. So I added a Wire.begin() to my code anyway.

One thing it could be is a stack/memory clash but I am not using that much RAM.

I have relook at the code so many times trying to workout what the problem is.

A relook at old but simpler code

I relooked at the old code before jumping to the full daylight fitted model. Althought the intercept concept is flawed, it still can work. Here is some old work where I uses an exponential smoothing (blue) to trigger a cature widow (red horizonal bars). I also fitted a linear regression to the transition slope to "estimate" sunrise/sunset:

The problem with this idea is that the transition is a function of the light sensor (LDR) maths. Well not quite, if we look at the daylight (Lux) function (red) there is an inflection around sunrise/sunset (circlued):

I spend a day or two looking the transition area, trying different things (model, filters etc.). In the end Iiked ths one the best:

Here I heavily smoothed the data and then used a threshold to determine -6 degrees altitude. Here is another:

Here the code calculates latitude and longitude during the day and during the night. The black vertical lines are the calculated dawn/dusk markers. Note the false trigger during startup. From the two sets of data above the simple code works quite well. This code calculates on the fly and does not need the EEPROM.

For the code I looked at linear regression, quadratic regression, logistic regression, IIR LP, FIR LP, exponential, quadradic and cubic smoothing. In the end I used two passes of of a window smoother. A window or box smoother just averages a window (in this case 21) values (and used no weights).

With smoothed data I can reliably trip the twilight (i.e. dawn/dusk) marker with a threshold.

The threshold/altitude pair is calibrated for a setup, but once setup should be reasonably robust (to be investigated further). The current setup (dawn/dust) requires low night light levels.

Possible Problem with EEPROM

I had no problems with simplified version of the code that is less the menu, rotary encoder polling ISR and the Solver. I suspect that the polling ISR is causing problems with the Wire library.

The simplified version of the code is ready for a trial.

Sensor Sensitivity

I checked my sensor against a Lux sensor:

The current threshold of 988 returns -6 degres alititude (i.e twilight or 3.4 Lux).

If I look at the Lux curve:

I should be targeting sunrise/sunset (red circle) rather than dawn/dusk. For the smoothing (lowpass filter) to work in my application I need to reduce the sensor sensitivity (especially if it is located outside). This will move the sunrise transition to the right and the sunset curve to the left. Currently the LDR load resistor is 22k so perhaps a value of 4k7 may be better.

Sunrise and sunset have light level around 400 Lux (40 Lux if heavily overcast) and twilight around 3.4 Lux.

Sensor Calibration

As sensor was not following the mathematics I developed from the datasheet, I took three measurements:

Fitting a model to this data I got:

Based on this model the sunrise and sunset threshold (~400 Lux) should be ADC=779.

Lower than I would like but good enough for the time being. For low power I will need to redesign the sensor circuit, perhaps use the Lux sensor instead.

Anyway, the box is on the roof collecting a sunset and tomorrow a sunrise. Lets see how it goes.

Well, latitude was way off (all though in the right hemisphere) but longitude was spot on. With one minute samples the longitude accuracy (1/2 minute) is 10.67km, for my latitutde. I got 17.5 km (0.2 degrees).

I should do a proper LDR versus Lux calibration but I want to try an LED as Ted (https://hackaday.io/ted.yapo) suggested a while back. I am going to replace the rotary encoder with a simple push button as well.

Magic 

Discussions