Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By Electroguard
#58941 Somebody recently asked mmiscool for a driver for the i2c TMI637 4 digit 7-segment display so they could display the IP address - it's a cheap and convenient display which is equally useful for a clock or timer or counter, so I hope something materialises... and I wouldn't be surprised if it was already on TrackerJ's (Mr I2C) ToDo list.

But in the meantime perhaps someone may find this LED IP 'Blinker' of use.

It blinks out the sequences of IP digits with appropriate pauses between the 4 numbers, and because a blank '0' non-event isn't helpful, a zero is denoted by 10 blinks.
By default it uses the onboard gpio01 blue led (can be configured for others) so the example script will run on just about anything.


Some notes:

If BlinkIP is ran at program startup it will be noticed that no screen writes occur until after BlinkIP finishes and the program continues on to the WAIT instruction. This does not matter of course, because if you already knew the IP address in order to connect via browser you wouldn't be needing to blink the led to discover it.

It may be useful to BlinkIP on demand by pressing a local button. Be aware that any gpio button that doesn't already include a pull-up resistor will need to have a pullup physically added... but I strongly advise against adding physical resistors to the likes of Sonoff and Electro-dragon modules with potentially lethal MAINS voltages in close proximity.

The Sonoff hardware button already includes an on-board hardware pullup, but the button may already be being used for manually toggling the relay, in which case you could use the button in MultiMode whereby the same button can be used for different purposes depending on how long it is pressed.

In the example, a short press of less than 2 seconds does a Toggle, but if the button is held down for 2 seconds or longer it does a BlinkIP.

Notice that whatever the Toggle state, after BlinkIP finishes, the led is returned to it's original state - so if the led was showing the status of the relay, it will still show correct relay status again after BlinkIP.

If you join up all the dots you'll see that besides being a BlinkIP example, plus button MultiMode example, this is actually a complete self-contained network relay node with local button activation and led status indication which you could obviously also control however you wish... whether by date/time, local button or switch or sensor activation, or remotely-triggered. Change ledpin = 13 if used on a Sonoff.


Code: Select allmemclear
localIP = ip()     
html "IP address: " & IP() & "<BR>"
html "MultiMode hardware button-press for Toggle and Blink IP<BR>"
buttonmode = "MultiMode" 'Quick press (<2s) to toggle led, long press (>2s)to blink ip
buttonpin = 0 'Uses gpio00 flashing button by default, change to suit (needs pullup resistor).
buttonoff = 1 'Default botton OFF state
ledpin = 1    'Uses onboard gpio01 blue led by default, change to suit
ledoff = 1    'Default led pin off state (allows configuring led pin for active high or active low operation)
relaypin = 12   'Set to the required Relay control pin
relayoff = 0 'Default relay OFF state - Allows for use of active high or active low relay (or alternative output)
if ledoff == 1 then io(po,ledpin,1) else io(po,ledpin,0) 'initialise led to its off state
if relayoff == 1 then io(po,relaypin,1) else io(po,relaypin,0) 'initialise relay to its off state
start = 0     'used by MultiMode button-pressed timer
stop = 0      'used by MultiMode button-pressed timer
interrupt buttonpin, [PRESSED]
gosub [BLINKIP]
wait


[PRESSED]
if io(laststat,buttonpin) == 0 then start = millis() else stop = millis()
if stop > start then
 if stop-start < 2000 then gosub [TOGGLE] else gosub [BLINKIP]
endif
wait

[TOGGLE]
if io(laststat,ledpin) = 1 then io(po,ledpin,0) else io(po,ledpin,1)
if io(laststat,relaypin) = 1 then io(po,relaypin,0) else io(po,relaypin,1)
return

[BLINKIP]
oldstate = io(laststat,ledpin) 'remember original state
blinkon = 200
blinkoff = 400
blinkpause = 1000
blinkgap = 1400
if ledoff == 0 then io(po,ledpin,0) else io(po,ledpin,1) ' turn led off
delay blinkpause
for pos = 1 to len(localIP)
 digitchr = mid(localIP,pos,1)
 if digitchr == "." then
 delay blinkgap
 else
  if digitchr = "0" then digit = val("10") else digit = val(digitchr)
  for count = 1 to digit
   if ledoff == 0 then io(po,ledpin,1) else io(po,ledpin,0)
   delay blinkon
   if ledoff == 0 then io(po,ledpin,0) else io(po,ledpin,1)
   delay blinkoff
  next count
  delay blinkpause
 end if
next pos
delay blinkgap
io(po,ledpin,oldstate) 'restore to original state
return
User avatar
By trackerj
#58945 TMI637 is on my to-do list but the module is coming with the slow boat from China, so probably we need a little bit more patience especially in this period of the year.
If is anybody closer and cand/want to send me a module faster, will be more than happy to do a driver example tutorial for it.

Happy breadboarding,
TJ.
User avatar
By robert badiduwitz
#58954 Electroguard, nice program, will have to try it out.

As far as the TM1637, it is NOT an I2C interface. It has it's own "weird" interface, so that is why there is an Arduino library. If you look at the library you will see it is not "normal" i2c. Otherwise, it probably would have been easier to deal with.

My buddy got this going using great cow basic, but not using I2C, he wrote a bit bang type method basically like the arduino library.