Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By russm
#50207 I decided to hook up a rotary encoder to my NodeMCU running espBasic V3 A8.

To make the code work well, you must stop the winsock connection, otherwise it is too slow. I've not looked at how to end it on program run. Just hit the back arrow on your browser to get back to the edit screen. You can see the rotary encoder incrementing up and down through either the serial port or start the UDP viewer shown in the following thread:
http://www.esp8266.com/viewtopic.php?f=40&t=9451&hilit=udp
Don't forget to change the UDP address it broadcasts to match your subnet!

Here is the code:
Code: Select all'******************************************************
'* Rotary encoder with switch                         *
'*   by Russ McIntire, 7/4/16                         *
'* Stolen heavily from:                               *
'* http://bildr.org/2012/08/rotary-encoder-arduino/   *
'* D4 for switch                                      *
'* D2 for LSB rotary                                  *
'* D3 for MSB rotary                                  *
'******************************************************

udpbegin 5001

msb = 0            'D3 rotary bit
lsb = 0            'D2 rotary bit
encoded = 0        'Combined MSB and LSB
lastencoded = 3    'Previous MSB and LSB - set to 3 to allow the first calculation to work
sum = 0            'Place lastencoded in front of encoded
encoder = 0        'encoder value

'Listen for rotary encoder input
interrupt D4, [switch]
interrupt D2, [change]
interrupt D3, [change]
wait
end

[switch]
if io(laststat,D4) = 1 then
 serialprintln "switch high"
 udpwrite "192.168.10.255", 5001, "SH"
else
 serialprintln "switch low"
 udpwrite "192.168.10.255", 5001, "SL"
endif
wait

[change]
msb = io(laststat,D2)
lsb = io(laststat,D3)

encoded = lsb + (msb << 1)            'Combine MSB and LSB
sum = lastencoded << 2 + encoded      'Put lastencoded in the front and call it sum
lastencoded = encoded                 'record lastencoded

if (sum = 13 or sum = 4 or sum = 2 or sum = 11) then
 encoder = encoder - 1
endif

if (sum = 14 or sum = 7 or sum = 1 or sum = 8) then
 encoder = encoder + 1
endif

'serialprintln " "
'serialprintln "msb = "&msb
'serialprintln "lsb = "&lsb
'serialprintln "sum = "&sum
serialprintln "encoder = "&encoder
udpwrite "192.168.10.255", 5001, "E"&str(encoder)
wait


Here is a picture of the perfboard layout, the resistors are 10K.
Image

The rotary encoder is the cheap ones from electrodragon. You can purchase one with the resistors already mounted on a pcb suitable to plug into your breadboard as well. Additional info on connecting the rotary encoder can be found here:
http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/keyes-ky-040-arduino-rotary-encoder-user-manual/

Within the code in the header is a link to where I stole heavily in writing my basic code. Enjoy, hopefully you can use the ideas in a project. I'd love to hear back on:
1) how to turn off winsock programmatically
2) make a change to reduce the impact on the ESP maintaining the winsock connection to improve performance
3) tweak my setup to make it run a little more smoothly. Sometimes it really jumps about, the encoder I'm using gives 4 steps for each detente. I've used rotary encoders in other projects on PIC HW, here's a link:
http://cabcommand.com/
Last edited by russm on Wed Aug 24, 2016 7:49 pm, edited 1 time in total.
User avatar
By russm
#50209 Decided to play with receiving UDP so here's a quick program to compliment the earlier one and dim/brighten an LED (attached to D4) on a NodeMCU as the rotary encoder is turned:

Code: Select allmemclear
udpbegin 5001

udpbranch [udp.received]
wait


[udp.received]
rec = udpread()
rem = udpremote()
serialprintln "Message received " & rec & " from " & rem

x = left(rec,1)
if x = "E" then
 y = len(rec)
 z = right(rec,y-1)
 bri = val(z)
 serialprintln str(bri)
 io(pwo,d4,1023-bri)
endif
return


As always, code borrowed liberally from examples in espBasic V3 manual.
User avatar
By forlotto
#50216 Now here is what I want to see....

TFT LCD
2 rotary encoders
an accelerameter

The TFT serves as a screen

The two knobs one on the right one on the left

the acc. mounted to it all on a pcb.


Then write code so that this thing functions like an etchasketch :P

I'd love to see it for fun but anyways ... Never thought of using one of these hasn't even remotely crossed my mind but I am sure there is a ton of useful things for these stepper motors pots etc... hrmmm interesting.
User avatar
By russm
#50234 The TFT is really easy to use. Drawing on it is pretty fast. I have one on another board that updates time, temp and heat index as well as changes the display from temp to heat index using a touch screen button. I am planning on combining these at some point but going to do a little network experimentation first, i.e. select which remote ESP and send rotary encoder inputs to it. Going to try to see how many can run at once using a separate, dedicated access point for the network.