Page 1 of 1

Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 20:37
by Trelsonowsky
Hello,
Is it possible to make a simple counter that would add "1" to a value displayed on SSD1306 display, every time a button is pressed?
I'm pretty clueless if it comes to rules. I have both display and a button setup correctly but I can't get it to work together.
What would be perfect is if the OLED would contantly display "000000" and just add 1 when button is pressed. You can think of it as a stattrak from csgo(because thats exactly what im trying to do).
Also is it possible for it to retain the counter when theres no power?

Cheers!

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 21:25
by TD-er
Let's assume you have a switch task calles "button" with taskvalue called "switch"
You should set the Switch button type to Push Button Active Low and have the de-bounce set to make sure you only see a single action when pressing the button.
See: https://espeasy.readthedocs.io/en/lates ... tch#switch

This will then also generate an event when the button is pressed, like this "button#switch=1"

So in the rules you act on the event like this:

Code: Select all

on button#switch do
  // Increment the counter in variable #1
  let,1,[int#1]+1
endon
In the task for your display you simply put [int#1] to show the integer value.
You can also format the value, like adding leading zeroes.
See: https://espeasy.readthedocs.io/en/lates ... red-values

To format the value to 4 digits, you can use [int#1#D4]

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 21:44
by Trelsonowsky
Hey, works perfectly thanks
And for a "negative button" this would work right?

Code: Select all

on button2#switch do
  // Reduce the counter in variable #1
  let,1,[int#1]-1
endon

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 21:47
by TD-er
Yep

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 21:47
by Ath
To store the counter value you can use a Level Control task. That has an option to save the value, but can incorporate some delay, to avoid wearing out the Flash storage by saving too often. The GPIO option can be ignored, and the Set Level is the value that will be displayed, so can get an initial value. Assuming you don't plan to switch off the device often, the Auto-save interval can be set to something like 30 minutes.

Code: Select all

on button#state do
  let,1,[Level#GetLevel]+1 // Increment last counter value
  config,task,level,SetLevel,[int#1] // Store new value, will be auto-saved
endon
on button2#state do
  let,1,[Level#GetLevel]-1 // Decrement last counter value
  config,task,level,SetLevel,[int#1] // Store new value, will be auto-saved
endon
On the display you can use the predefined layout to display the counter value, using something like this:

Code: Select all

Counter value: [Level#GetLevel#D4]
Edit: Changed button value part to (default) 'state'.

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 21:57
by Trelsonowsky
I unfortunately don't have a Level Controll option as a device. Im using ESP01 and my options are kinda limited( im using ESP_Easy_mega_20220616_custom_ESP8266_1M). I don't mind wearing out flash, these modules are dirt cheap so I can just get a new one.

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:03
by TD-er
Maybe even better to replace the flash chip for a 4M version.
That makes updating a lot easier

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:06
by Ath
We don't have another plugin that can easily store a value for you, but more recent builds do have a normal build for 1MB ESP8266 units, you can download f.e. the latest merge build from Github Actions.

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:06
by Trelsonowsky
Can that be done with dummy device?
My options are kinda limited
I dont think soldering a 4M chip is a viable option since it would require microsoldering and I dont have equipment for that.
Image

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:09
by TD-er
The dummy tasks only keep the values during crash/reboot.
Not on a power loss.

And those 8 pin flash chips are actually relatively simple to solder, even with a rather crude soldering iron.

Edit:
See: https://www.electronics-lab.com/upgrade ... h-4mb-one/

The video (as blocked when opening embedded on that page) https://www.youtube.com/watch?v=7Q6ABad7U6o

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:33
by Trelsonowsky
Ok as it turns out, "normal" version contains both Display and Level Control modules. I tried a counter with Level Control but it's not working. Pressing the button changes it's state from 0 to 1 and vice versa, and so is the level control, just the oposite value.
https://imgur.com/a/ggRIfjH

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:51
by Ath
You're not using the output, but the, not visible on the Devices page, GetLevel value. If you open the task settings you'll see the updated Set level value (not updated when the page is open, but loaded when opening the page)

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 22:55
by Ath
Trelsonowsky wrote: 20 Jun 2023, 22:33 https://imgur.com/a/ggRIfjH
Hm, please attach screenshots to the forum (Attachments tab below the editing area), as those external services tend to 'lose' your images after a couple of months, making the post effectively useless.

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 23:02
by Ath
Your button has the (default) Values name State, but the rules use switch. Best would be to change the rules to use State (not case-sensitive, btw) instead of switch. At least it has to match with what's used on your tasks

Re: Tally counter with SSD1306 OLED

Posted: 20 Jun 2023, 23:34
by Trelsonowsky
Yup, that was it. Thanks!

Re: Tally counter with SSD1306 OLED

Posted: 21 Jun 2023, 00:03
by Trelsonowsky
Tried to do it myself BUT there is no Autosave interval. What do? :'(
Ath wrote: 20 Jun 2023, 21:47 To store the counter value you can use a Level Control task. That has an option to save the value, but can incorporate some delay, to avoid wearing out the Flash storage by saving too often. The GPIO option can be ignored, and the Set Level is the value that will be displayed, so can get an initial value. Assuming you don't plan to switch off the device often, the Auto-save interval can be set to something like 30 minutes.

Code: Select all

on button#state do
  let,1,[Level#GetLevel]+1 // Increment last counter value
  config,task,level,SetLevel,[int#1] // Store new value, will be auto-saved
endon
on button2#state do
  let,1,[Level#GetLevel]-1 // Decrement last counter value
  config,task,level,SetLevel,[int#1] // Store new value, will be auto-saved
endon
On the display you can use the predefined layout to display the counter value, using something like this:

Code: Select all

Counter value: [Level#GetLevel#D4]
Edit: Changed button value part to (default) 'state'.

Re: Tally counter with SSD1306 OLED

Posted: 21 Jun 2023, 08:43
by Ath
If you upgrade to a more recent release of ESPEasy, that should have the Auto-save interval available, as it has been introduced not that long ago. You can download the last mega build from here: https://github.com/letscontrolit/ESPEas ... 5265687433 (You will need a free Github account to be able to download files from there).

Re: Tally counter with SSD1306 OLED

Posted: 22 Jun 2023, 20:08
by Trelsonowsky
It's always uphill for me. It works perfectly when powered via usb programator, but whenever I power it from a regulated 9V battery, the whole thing freezes after one button press(the number increases once though) and voltage regulator gets very hot.
Do you think you could help me? :) Or should I maybe create a separate post?

Re: Tally counter with SSD1306 OLED

Posted: 22 Jun 2023, 20:18
by TD-er
A linear regulator is a really bad idea for such a large voltage drop.

Linear voltage regulator means the regulator acts as sort of resistor to make sure the output voltage remains the fixed.
However the current on the input and the output is the same.

So 9V input, 3.3V output means there is 5.7V drop between input and output.
Let's assume 150 mA current, then the voltage regulator alone consumes 0.855 Watt.
Since the current is the same, the ratio is 3.3 : 5.7 when considering the power consumption of the ESP and the voltage regulator.
Thus the voltage regulator takes nearly 2x as much power as the ESP (& sensors/display)

Better use some DC/DC converter.
Still I think it is a bad idea trying to power this from a 9V battery as you are wasting quite a lot of energy, those batteries don't have a lot of energy to begin with and a lot of those voltage regulators also take quite some energy when the device on the output of those regulators hardly uses anything.

Re: Tally counter with SSD1306 OLED

Posted: 23 Jun 2023, 00:12
by Trelsonowsky
Well the problem is, the same thing happens with a dcdc step down converter. Also I don't have too many other options to get something of this size with protection and 3 volts. LiPo is problematic since most bms for them, cutoff the voltage at 2.5 which might be ok for a 18650 battery, but isn't for a typical lipo pack. And 18650 are way too big for my project anyways