I've been updating firmware on my NodeMCU a few times without issues - at least up to 0.9
The installation procedure has changed with SDK 1.5 - I've spent a few hours before I got it right, so maybe this post will help you save time.
Here you will find all the required information, but depending on your chip manufacturer, you will need different combinations of some options, and this is exactly the part when it's getting tricky.
Since things are changing rapidly, I will point out exact versions in this tutorial so that it's clear what was working with what at the time of this writing.
How do you tell? I've no idea - just Google "Manufacturer: e0 Device: 4016" and check what people think.
Building custom firmware
Now we have to obtain firmware containing the Lua interpreter for our chip - it's everything you will need to upload Lua scripts over serial. Basically, you will need two binary files:
Obviously, you will need matching versions. There are two options to get those files:
you can download it from my github repo - this will give you SDK 1.5.4.1
you can also use cloud service to build latest release. The link to latest initial-data-block can be found here - just search for esp_init_data_default.bin
I would prefer the latest version - as usual. But in case of some problems, you can try using the version provided on my github repo. At least this one was working in my case with ESP-12E, this might help you to locate a problem.
Erase flash
Put your chip into flash mode as described here. If you have a development board, it should have two buttons: flash and reset. In such case, press flash, and without releasing it, press reset, after that, release flash.
Now you must determine and change the com port in all commands in this tutorial. In my case, it's /dev/tty.wchusbserial1410
Let's finally erase flash!
sudo python esptool.py -p /dev/tty.wchusbserial1410 erase_flash
esptool.py v1.1
Connecting...
Erasing flash (this may take a while)...
Now disconnect ESP from usb port, so that it can fully reset - pressing reset button does not have the same effect!
Flashing new firmware
Put your chip again into flash mode.
I am assuming that you have in your current directory two binary files: one with firmware (nodemcu-dev-7-modules-2016-08-10-10-43-59-integer.bin) and a second with init-stuff (esp_init_data_default.bin).
I wanted to pimp my son's bobby car by installing a few lights, they should go on for a few minutes once he starts moving the car. I've installed a few LEDs and a micro switch attached to the rear axle for this. So that the microswitch will get triggered from time to time.
Connecting LEDs was quite simple, but I had some difficulties building a circuit that would enable the lights for a few minutes. I've decided to use a 555 timer - plenty of examples exist of how to assemble it. I just wanted one that would be energy efficient on stand-by, and the lights should always go off after a few minutes. This was difficult because you have to trigger the 555 by short impulse, otherwise, it will never go off. My solution with micro switch has this one catch: it can remain shorted for a very long time. I could ask my son to keep pushing the car until he hears the second click..... instead, I've used a trigger network.
In the end, I had to find the right values for all components in a way that would guarantee the functionality and ensure low power consumption to not drain batteries over a few days. Actually, I did not calculate those values, I've used an oscilloscope to make sure that timing on 555 pins is right. Probably I've got not a perfect value, but on the other hand side it does what it is supposed to be doing, and it runs on a single battery for months :)
My latest project (https://github.com/maciejmiklas/LEDDisplay) contains the driver for 8x8 LED Modules controlled via
MAX722xx. It allows you to build a display of custom size that is only
limited by the hardware itself. Vertical and horizontal size can contain
up to 256 modules, but before reaching this limit, you would run out of
Slave Select lines for controlling MAX chips, or you would be limited by the amount of RAM. The fact is: you can control a reasonable amount of MAX
chips and build a display of custom size ;)
I've tested the whole idea on display which consist of 8 LED Modules
in horizontal and 3 in vertical position. This gives us 24 modules that
contain 1536 LEDs (88 * 38).
Hardware
First, let's start with the controller, actually, any Arduino will
work, I've used Mega due to the large number of digital output pins. You
could also use Nano with a shift register and alter the way of addressing
Select Slave lines in Display::send(...).
You will need an extra power supply for driving LEDs - assuming that you will use more than one LED Matrix.
Driving single LED Matrix Module
I've used the MAX7219 and 788BS LED Matrix, this is the one with a common
anode. The schematic below illustrates the wiring of LEDs, MAX and Arduino:
This one is equivalent, but instead of single LEDs we have PIN layout of LED Module:
It might happen that your LED Module has a common cathode, in this case, you have to rewire the connection to the MAX chip. You just have to remember that MAX has two sets of relevant pins: Dig0-Dig7
are supposed to be connected to cathodes (-) and SegA-SegG to anodes(+). Such a change will also swap rows with columns within the sine LED
module.
Connecting all LED Matrix together
In the previous chapter, we've seen how to connect a single LED Module with an MAX chip. Now we will connect multiple LED Modules together into one
large display.
Below is the physical display that I've used for testing and examples.
Each LED Module has label indicating its position and Select Slave line.
Each 3-PIN connector on the schematic above symbolizes one module
described in the previous chapter (LED Matrix + MAX72xx), now we've
connected all those modules together.
All MAX722xx chips share common MOSI and SCK lines, MISO is not used, and each chip occupies a separate Slave Select line.
The position of LED Matrix on the schematic above directly
corresponds to their location on the physical display that I've used for
testing. Additionally, each module has a description indicating its
position and Select Slave line, so for example: (2,1) SS: 35 gives us a second module on the third row (counting from zero) and PIN:35 on Arduino for the Select Slave line.
Software
Compiling
We are using standard Arduino libraries, which are already available, the only exception is ArdLog. You have to import this LIB into your IDE. This basically means that you have to download the right release: https://github.com/maciejmiklas/ArdLog/releases/tag/v1.0.0 and unzip it into a folder, where you usually place external libraries. In the case of standard Arduino IDE on Mac, it's ~/Documents/Arduino/libraries. In the end you should have the following structure:
$ pwd
~/Documents/Arduino/libraries/ArdLog
$ ls
ArdLog.cpp ArdLog.h LICENSE README.md
Communication with MAX72xxx
We are using the standard SPI library and Select Slave line on the MAX chip for
addressing. MAX is configured in LED Matrix mode - so there is nothing
special. The setup method can be found in Display::setup()
Setting things up
The main class of our interest will be the Display - it's responsible for the setup of MAX chips and provides API for painting.
Before we start painting, it's necessary to set things up. The code below
creates a 2D array containing Select Slave lines and initializes the display.
The display consists of 3 rows, each with 8 LED Modules.
You can choose any responsible size, but I will stick to this
one.
The layout of the mentioned 2D array corresponds to the physical display:
each LED Module has a dedicated MAX chip, and each chip has a dedicated
Select Slave line. The first dimension of our array indicates the physical row
on display, the second dimension indicates LED Module within this row, and
the value itself contains the PIN number for the Select Slave line.
#include <Display.h>Display*disp;/** * Orientation of LED Kits (8x8 LED matrix) on display that I've used for testing. * The numbers are indicating Select Slave line of MAX7219. * 48, 46, 49, 47, 45, 43, 41, 39 * 36, 34, 32, 30, 28, 26, 24, 22 * 37, 35, 33, 31, 29, 27, 25, 23 */ss_t**ss;ss_t**createSS(){ss_t**ss=alloc2DArray8(3,8);// first rowss[0][0]=48;ss[0][1]=46;ss[0][2]=49;ss[0][3]=47;ss[0][4]=45;ss[0][5]=43;ss[0][6]=41;ss[0][7]=39;// second rowss[1][0]=36;ss[1][1]=34;ss[1][2]=32;ss[1][3]=30;ss[1][4]=28;ss[1][5]=26;ss[1][6]=24;ss[1][7]=22;// third rowss[2][0]=37;ss[2][1]=35;ss[2][2]=33;ss[2][3]=31;ss[2][4]=29;ss[2][5]=27;ss[2][6]=25;ss[2][7]=23;returnss;}voidsetup(){util_setup();log_setup();ss=createSS();// Test display consist of 8x3 LED Modules (3 rows, each one 8 Modules)disp=newDisplay(8,3,ss);disp->setup();}
There is one more method worth mentioning: log_setup(). The whole project has quite a precise logger - so that you can see what is actually
happening. By default, it's disabled, in order to enable it to check out its
documentation: https://github.com/maciejmiklas/ArdLog
Painting on the display
The display consists of a few LED Modules, but from an API perspective, they are
connected together into one continuous canvas. You can place on this
canvas bitmap any position given by such coordinates:
(0,0) -----------------------------> (x)
|
|
|
|
|
|
| (x max, y max)
v (y)
The paint method has the following syntax: paint(pixel_t x, pixel_t y, pixel_t width, pixel_t height, uint8_t data).
It allows you to paint a bitmap on given coordinates with limited width
and height. So you can, for example, paint a bitmap on (3,4) that has
25x3 pixels. It might be larger than the actual display size - in this
case, it will get trimmed.
This is obvious and simple, but there is one catch - you must provide the right data.
This is a 2D array, where the first dimension indicates the vertical and the second
horizontal position on the display. Technically speaking, data is a flat array of pointers, and each pointer points to an array that represents one horizontal line on the display.
Moving over the first dimension of data traverses over lines of the display. The second data dimension represents horizontal pixels within a single line; each byte
represents 8 pixels. Since our display consists of simple LEDs, they can
be either on or off, so each pixel is not represented by
one byte but by one bit. In order to cover 16 pixels in a horizontal
position, we need two bytes, 24 pixels require 3 bytes, and so on.
For example, to fully cover a display consisting of 8x3 LED kits (one used in our examples), we would need data[3][8]. Usually, you will take an array small enough to fit your bitmap and not one that will cover up the whole display.
The paint(...) method updates the internal buffer, in order to send the content of this buffer to MAX chips, you have to call flush().
The idea behind this is to allow you to shift a few bitmaps into the display and then paint the result. You can program a few
independent routines that will update different display parts and
flush all changes at once.
Communication with MAX chips is not very fast, and sending the content of the whole display with every flush() is time-consuming. You can speed up this process by enabling double buffering (set DEOUBLE_BUFFER in Display.h to true). In this case, flush() method will send only bytes that have changed, so you can call flush()
with every loop and not worry about losing performance. The
only drawback is increased usage of RAM: we are creating 2D array that
allocates 8 bytes per each LED Kit plus a few pointers that are usually
required to maintain arrays.
2D arrays in this project have reduced memory footprint because to create a dynamic 2D array, we are creating a single continuous array with a calculated offset (see: alloc2DArray8(....) in DisplayUtil.h).
In this example, we will display a simple static bitmap with 8x8 pixels:
Here is the Arduino sketch: SimpleBitmap, now lest go over it:
First, we have to initialize the display, as we have done in the above chapter Setting things up.
Next, we have to create data that can hold our bitmap - it will have 8x2
bytes. This gives us up to 8 lines and 16 horizontal pixels. But the
size of our bitmap is 9x8 pixels (width x height), which will also be the size of the painted rectangle. It should be as small as possible so you can place another bitmap next to it.
The display will obviously only paint the rectangle given by width/height and not the whole data
array. This is normal that a data array can hold more pixels than the accrual size of our bitmap because size of the data is a multiplication o 8, and the bitmap is not necessary.
voidsetup(){util_setup();log_setup();ss=createSS();disp=newDisplay(8,3,ss);disp->setup();data=alloc2DArray8(8,2);data[0][0]=B01100001;data[0][1]=B10000000;data[1][0]=B01100001;data[1][1]=B10000000;data[2][0]=B01100001;data[2][1]=B10000000;data[3][0]=B01100001;data[3][1]=B10000000;data[4][0]=B01100001;data[4][1]=B10000000;data[5][0]=B00110011;data[5][1]=B00000000;data[6][0]=B00011110;data[6][1]=B00000000;data[7][0]=B00001100;data[7][1]=B00000000;disp->paint(27,9,9,8,data);}voidloop(){util_cycle();log_cycle();// Paint method updates only internal buffer, in order to send data to // MAX chips you have to flush display. disp->flush();delay(100000);}
Static Text
Now we will display static text, actually those are going to be two independent lines.
Here you can find an Arduino sketch containing the whole example: StaticText.
Your sketch needs a setup method, as we've already seen above (chapter: Setting things up), so we will not discuss it again.
In order to display text, you should use StaticText8x8.
The font is defined in Font8x8, each character has 8x8 pixels.
Your code could look like this one (plus initialization stuff from Setting things up):
Display*disp;ScrollingText8x8*message;constchar*textMessage;voidsetup(){util_setup();log_setup();ss=createSS();disp=newDisplay(8,3,ss);disp->setup();message=newScrollingText8x8(disp,48,50,5);message->init();textMessage="This is an example of multiple scorlling areas ;)";message->scroll(8,8,ScrollingText8x8::LOOP,textMessage);}voidloop(){util_cycle();log_cycle();message->cycle();disp->flush();}
The initialization of the display is the same as in the examples above, so it's omitted here.
In order to display scrolling text, we are using ScrollingText8x8. In setup(), we are creating an instance of this class and calling method scroll(...). This part only initializes scrolling but does not play the animation itself. In order to play the animation, you have to call cycle() and flush() in the main loop, and you must not have any additional delays there, otherwise, you might get jagged animation.
During the creation of ScrollingText8x8 we have provided the speed of animation - actually, it's a delay of 50ms per frame. Now calling cycle() in the main loop will produce animation frames according to provided delay. When the time comes, the method cycle() will update the display, and finally, method flush() will send updated content to MAX chips.
The whole implementation of ScrollingText8x8 is nonblocking and consumes CPU only when something is to be done. Internally it's using a simple State Machine.
There is one last thing: you must keep the text used for animation in the global variable to avoid deletion. It's not being copied in the scroll() to avoid memory fragmentation.
Scrolling Text Mixed
This example is similar to the one above, but this time we will display
several scrolling areas:
Here is the sketch.
This code is similar to one with one scrolling area, but this time we have a few:
voidsetup(){util_setup();log_setup();ss=createSS();disp=newDisplay(8,3,ss);disp->setup();uint8_tborderSpeed=20;textUpDown="* * * * * ";up=newScrollingText8x8(disp,64,borderSpeed,1);up->init();up->scroll(0,0,ScrollingText8x8::CONTINOUS_LOOP,textUpDown);down=newScrollingText8x8(disp,64,borderSpeed,2);down->init();down->scroll(0,16,ScrollingText8x8::CONTINOUS_LOOP,textUpDown);textLeftRight="* ";left=newScrollingText8x8(disp,8,borderSpeed,3);left->init();left->scroll(0,8,ScrollingText8x8::CONTINOUS_LOOP,textLeftRight);right=newScrollingText8x8(disp,8,borderSpeed,4);right->init();right->scroll(56,8,ScrollingText8x8::CONTINOUS_LOOP,textLeftRight);message=newScrollingText8x8(disp,48,50,5);message->init();textMessage="This is an example of multiple scrolling areas ;)";message->scroll(8,8,ScrollingText8x8::LOOP,textMessage);}voidloop(){util_cycle();log_cycle();up->cycle();down->cycle();right->cycle();message->cycle();left->cycle();disp->flush();}
We have created a few instances of ScrollingText8x8, each containing different text and positions on the display. In order to play an animation, you have to call cycle() on each instance, but you have to call only once flush(). Each call on cycle() will update its part of the display, and flush will send the changed display to MAX chips.
I am bit tired of using Serial as logger - mainly for two reasons: it does not support sprintf syntax and string are being held in RAM. For this reason I've implemented new library: https://github.com/maciejmiklas/ArdLog
ArdLog serves as simple logger for Arduino that creates formatted messages over Serial:
Each message has timestamp.
Each message within single loop has the same timestamp, so that you can logically connect activities together.
Messages can be formatted using sprintf syntax.
Text for the messages is being held in PROGMEM.
Installation
In order to install ArdLog you have to download desired release and unpack in into folder containing Arduino libraries. The is the result on MacOS:
Logger is disabled by default, in order to enable it set LOG to true.
Messages are created over default Serial port. You can choose alternative port by setting: USE_SERIAL_1, USE_SERIAL_2 or USE_SERIAL_3 to true.
In order to print current time for each message set USE_CURRENT_TIME to true. By default logger will sample time only once at the beginning of each loop.
Getting up and running
Choose suitable configuration in ArdLog.h. In most cases you have to only set LOG to true.
Call log_setup() in setup() method - this will initialize serial port.
Call log_cycle() at the beginning of each loop() - it will sample current time.
Put log messages into #if LOG log(F("....") #endif - once logger is disabled, it will not waste RAM and CUP.
Microcontroller is meant to control stuff and not to generate periodic signals - for this propose we would use a dedicated hardware - something like Atmega328 and AD9850.
But on the other side this might be an interesting project - not very useful, but at least we can manually build DDS and get good understanding of its functionality. The basic idea of such synthesizer is to create a software loop where each iteration will output single point of particular wave. Higher amount of points within single period increases resolution and reduces frequency - due to limited processing power.
There is also a good motivation to optimize the code - each extra operation consumes CPU cycles and decreases maximal output frequency.
Generated wave signals
Sine with 120 probes per period
Sine with 360 probes per period
Max frequency is limited to 1.8Khz where sine with 120 steps reaches 5.58Khz
Signal at 10Hz is smoother when compared to sine with 120 steps.
Square
Saw
Wave Generator
Arduino does not have Digital to Analog Converter - there is only PWM generator, but in order to generate smooth wave we need stable analog voltage. Common pattern to solve this problem is to take a few digital outputs and connect them together trough voltage divider - you can see that on schematics in the right bottom corner. We are using Arduino's digital output D0-D7 because they are all managed by single register (Port D). This means that we can change value of all 8 bits (D0-D7) with single assignment, for example: PODTD = B10000001 would set D0 and D7 to 1. This also means that mapping between byte value and analog output is linear, here are few examples that I've measured using oscilloscope:
PORTD
Out
B00000001
22 mV
B00000010
38 mV
B00000100
73 mV
B00001000
140 mV
B00010000
276 mV
B00100000
545 mV
B01000000
1.08 V
B10000000
2.18 V
PORTD
Out
B00000001
22 mV
B00000011
56 mV
B00000111
125 mV
B00001111
265 mV
B00011111
543 mV
B00111111
1.1 V
B01111111
2.23 V
B11111111
4.47 V
Each generated wave consist of few steps - for example sine has 120 (or 360 because there are two versions). Each step is pre-calculated and stored in an array - main loop simply goes over this array and assigns value from it to PORTD.
To smooth out generated signal I've used capacitor 470pF on the output. The op-amp works as a buffer so that resistance of a device connected to the output cannot influence generated wave - capacitor would discharge faster and we would change characteristics of voltage divider.
Configuration
There are four buttons:
Frequency Up - increase frequency
Frequency Down - decrease frequency
Delay Step - value of the step for frequency change
Select Wave - you can choose from from sine 120, sine 360, square and saw
Reaction on buttons is handled by interrupts, so that the main loop can concentrate on generating high frequency wave.
Frequency adjustment
There is a small catch... single press on Frequency Up does not necessary change the frequency by one hertz, but it changes the amount of wasted CPU cycles per step .... I will try to explain this from the beginning:
as you remember we are generating wave by going over pre-calculated table - each byte in such table will be assigned to PORTD and this happens within a single "step". In order to plot wave we have to go over whole table, once we are done, we have to start from the beginning. In order to change frequency, we have to alter time for each step - this is the only possibility to proportionally scale up he whole wave. The smallest amount of delay in our case is the single CPU operation - it's called NOP and it takes 1327 nano seconds - NOP itself is quicker but I've also considered time required to call a method.
For example single period of sine consist of 120 steps, increasing delay by one, would add one NOP operation to each step, meaning that single period would take additional 120 * 1327ns.
The good news is, that LCD display always displays correct frequency in hertz, only pressing Up and Down buttons changes it by few hertz. The bottom line of LCD display shows period time in nano seconds.
Code Optimization
My first version has used Arduino API and I was getting about 150Hz for sine with 120 steps - final optimization went up to 5,6KHz. I've used interrupts to handle input from buttons, direct registry access, removed all unnecessary method calls, reduced size of a variables - like from 16 to 8bits and finally exchanged floating points with integers.
There is always a tradeoff - code readability had decreased, direct access to registers is also tricky, because they can be used for different proposals. Arduino API takes care of all those problems, but it need few extra CPU cycles.
The goal of this project is to build a controller that will inform you when your plant needs watering. You will see the maximal and current moisture level and the time passed since last watering. Finally when moisture level drops below defined threshold the alarm will rise - in form of LED flashing SOS signal.
4 PIN socket in the left bottom corner is meant to be connected to moisture sensor. It's be cheap one that you can get on ebay
photo resistor regulates insensitivity of LCD backlight
R1 regulates LCD brightness
R5 regulates backlight insensitivity for LCD display
R10 sets alarm threshold
R3 value is not 220 ohm but 10k (I have to correct schematic)
Software design
I've used Eclipse, source code is here . Whole project is divided into modules, each one in separate class file.
I dis not use interrupts, so every module is controlled from main loop in PlantMonitor.cpp.
Each module has to be initialized and has to execute some operations once in a while. For this propose each module defines two functions: abc_setup() and abc_cycle() - abc would be a module name. The setup method has to be called only once and this happens in PlantMonitor#setup(). The cycle method will be called on every loop. Each cycle method internally measures time and runs only when it should (like every 100ms) without blocking.
The method Util#util_millis() is used by each module to measure time. This method caches current time and returns this cached value - internally it obtains time at the beginning of each cycle.
Using the same time during each cycle not only reduces amount of calls going to the timer, also each log statement within the same loop contains identical time, so we can logically connect events.
Logger
The log module will produce messages over COM port. It cannot be disabled because ... there is no reason to do so - we do not have any other usage for COM port and sometimes it's useful to see what is happening.
There are a few variable resistors, that can be used to set up things like alarm threshold. Those changes will be also printed over COM port, so that you can actually see how they effct functionality. Here is a log example:
>>[000-00:00:00,000]-> Initializing LCD module
>>[000-00:00:00,000]-> Initializing hygrometer module >>[000-00:00:00,038]-> Adopting LCD backlight. Sensor: 519, LCD: 177, Adjust(def 500): 605 >>[000-00:00:00,038]-> Adjusted alarm sensitivity. 370 = 36% >>[000-00:00:01,000]-> Status -> Free RAM: 1270 >>[000-00:00:12,000]-> Moisture has changed = 30%, Max: 30%, Status: 3 >>[000-00:00:12,000]-> Recognised plant watering, status: 3 >>[000-00:00:18,000]-> Reseting max proc based on adoption time after watering >>[000-00:00:18,000]-> Moisture has changed = 24%, Max: 24%, Status: 1 >>[000-00:00:36,000]-> Reseting max proc based on adoption time after watering >>[000-00:00:36,000]-> Moisture has changed = 19%, Max: 19%, Status: 1 >>[000-00:00:49,013]-> Adopting LCD backlight. Sensor: 559, LCD: 169, Adjust(def 500): 605 >>[000-00:00:57,000]-> Reseting max proc based on adoption time after watering >>[000-00:00:59,169]-> Adopting LCD backlight. Sensor: 679, LCD: 145, Adjust(def 500): 605 >>[000-00:01:12,139]-> Adopting LCD backlight. Sensor: 639, LCD: 153, Adjust(def 500): 604
Moisture - Hardware
We are using cheap moisture sensor that consists of two plates that you have to stick into a soil. The sensor delivers 0-5 volts based on moisture level. This means that we can directly connect it to Arduino analog input (A0).
Sensor plates corrode quickly, so it's a good idea to cover them with tin. Without this fix your sensor will last at most few weeks, or maybe you can get one that is already resistive to corrosion (one with gold surface). Applying extra tin will unfortunately increase current flow and influence readings, but we will correct this in a software (Hygrometer#MOISTURE_PROC_ADOPT).
Moisture - Software (Hygrometer.cpp)
The method hygro_sample(Moisture) returns current moisture level and status: "no change", "small change" and "level increased". hygro_sample(Moisture) is being called on every loop, internally it executes only every 100ms, otherwise it returns "no change". With each run (every 100ms) it probes moisture level, but it does not return it immediately, it stores it in internal array and returns "no change". First after collecting 30 probes, it finds the median and returns proper status. It's worth mentioning, that moisture change is being recognized with tolerance of 5% - just to avoid bouncing.
LCD Display - Hardware
LCD display is connected in a standard way, variable resistor R1 can be used to adjust brightness.
The insensitivity of LCD backlight is controlled by photo resistor. Photo resistor is connected to analog input A1 over voltage divider - this is a common pattern used to archive stable readings that are independent from current changes.
Software calculates insensitivity of backlight based on reading from photo resistor - this is a linear function. Additionally variable resistor R5 connected to A2 is used to calculate constant for our function: lower resistance increases measured voltage on A2 and increases backlight. This means, that resistance change proportionally influences backlight for all light conditions (readings of photo resistor).
Resistance changes on R5 are logged over COM, so you can exactly see it's influence on backlight.
The LCD backlight is connected to PWN output on D9, but it does not go directly to LCD input, instead it goes trough RC filter and op-amp. The RC filter uses large capacitor, this gives us smooth changes of backlight insensitivity.
Actually we would not need this whole RC filter, because PWM frequency it to high to observe flickering and smoothness could be archived in software, but I just wanted to solve this in hardware and have nice constant voltage.
You can see on screenshots below dependency between PWM duty (blue) and constant voltage (yellow).
The time required to change LCD backlight from maximal brightness to minimal takes 440 ms.
LCD Display - Software (Lcd.cpp)
The initialization phase (lcd_stup()) prints all static characters - those will not change any more, all other characters will be printed only if they have changed - this reduces flickering.
The lcd_cycle() will get called on every loop from PlantMonitor#loop() and it adopts LCD backlight for changing enviorment. Lcd.cpp contains also one function that prints time and another function that prints moisture - that's all.
SOS LED
It's just a LED connected to A10. The Sos.cpp controls the blinking frequency. The sos_setup() is called only once and it configures PIN A10 for output. sos_cycle() has to be called on every loop - it's non blocking method and it will switch the LED on and off in SOS rhythm. You can configure all possible time periods by changing corresponding variables in Sos.h. There are two more methods, that play significant role: sos_on() will enable SOS signal and respectively sos_off() will disable it. The cycle method have to be called on every cycle, even when SOS is disabled - in this case it returns immediately.
Low moisture level alarm
When moisture gets below defined level the LED controlled by Sos.cpp will start flushing. This is being controlled by Alarm.cpp. This module knows when the alarm has to be raised and when it stops, it also controls kind of alarm - currently it's a LED signal, but you could connect something else. In this case you have to only plug it on the right place in Alarm.cpp.
Alarm starts when moisture drops below level defined by variable resistor connected to PIN 3. The resistance will be mapped to percentage and changes to this resistor are printed to COM port - when you change the resistance you can read the threshold for alarm.
Getting up and running
Download latest release from here and compile cpp files, PlantMonitor.cpp contains main loop. I've used Eclipse, but importing this project makes no sense, because it contain hardcoded paths.