BV510 Smart Thermostat April 05, 2017 06:50PM |
Registered: 7 years ago Posts: 10 |
// Keyes Rotary Encoder Module input for BV510 connected as follows: // ENCODER <---------> GPIO // CLK -------- b4 // DT -------- b5 // SW -------- (NOT IN USE) // + -------- 3v3 // GND -------- GND // include the lib_io.bas file // #include "[www.byvac.com]; dim enc.run,enc.max,enc.min,enc.val dim enc.clk,enc.dt function enc.start(min,max) if enc.run <> 1 then enc.min = min enc.max = max enc.val = min //check for encoder movement every 100 ms taskadd("enc.check()",1,100,1) endif endf function enc.check() enc.clk = io_pinGet(*RB4()) enc.dt = io_pinGet(*RB5()) // Check if encoder has moved in clockwise direction if enc.dt > enc.clk then io_pinSet(*RB4(),1) io_pinSet(*RB5(),1) if enc.val < enc.max enc.val = enc.val + 1 endif print "Encoder has moved clockwise, new value is " print enc.val print "\r\n\r\n" else // Check if encoder has moved in anti clockwise direction if enc.dt < enc.clk then io_pinSet(*RB4(),1) io_pinSet(*RB5(),1) if enc.val > enc.min then enc.val = enc.val - 1 endif print "Encoder has moved anti clockwise, new value is " print enc.val print "\r\n\r\n" endif endif endf
Re: BV510 Smart Thermostat April 06, 2017 05:44PM |
Admin Registered: 11 years ago Posts: 351 |
// Rotary Encoder Example // Cant set up as a library because of interrupt // // NOTES: rot.sw is 1 when not pressed and 0 when pressed, this will change // immediately the sw is pressed // // #include "[www.byvac.com]; // #include "[www.byvac.com]; // constant ROTARY_A {PORTB(PORT),3} constant ROTARY_B {PORTB(PORT),13} constant ROTARY_SW {PORTB(PORT),1} // dim rot.seqA, rot.seqB, rot.sw, rot.count // ***************************************************************************** // ***************************************************************************** function rotary.start() io_pinRole(*ROTARY_A(),IN,WPU) io_pinRole(*ROTARY_B(),IN,WPU) io_pinRole(*ROTARY_SW(),IN,WPU) ir_io(*PORTB(),3,"rotary.irq",ROTARY_A(1)) ir_io(*PORTB(),3,"rotary.irq",ROTARY_B(1)) ir_io(*PORTB(),3,"rotary.irq",ROTARY_SW(1)) rot.count = 0 rot.sw = 1 endf // ***************************************************************************** // rotary encoder interrupt, sets global vearibales // ***************************************************************************** function rotary.irq() rot.sw = io_pinGet(*ROTARY_SW()) // rot.seqA = rot.seqA << 1 rot.seqA = rot.seqA | io_pinGet(*ROTARY_A()) rot.seqB = rot.seqB << 1 rot.seqB = rot.seqB | io_pinGet(*ROTARY_B()) // rot.seqA = rot.seqA & 0xf rot.seqB = rot.seqB & 0xf // if (rot.seqA = 9) && (rot.seqB = 3) then rot.count = rot.count - 1 endif if (rot.seqA = 3) && (rot.seqB = 9) then rot.count = rot.count + 1 endif endfThis works really well but I must confess that I got the idea of using 3 and 9 elsewhere, unfortunately I can't remember where. The code works in the background so you can set rot.count=0 and see if there is any change later on.
Re: BV510 Smart Thermostat April 06, 2017 07:54PM |
Registered: 7 years ago Posts: 10 |
Re: BV510 Smart Thermostat April 10, 2017 03:18PM |
Registered: 7 years ago Posts: 10 |
Re: BV510 Smart Thermostat April 10, 2017 04:32PM |
Admin Registered: 11 years ago Posts: 351 |
Re: BV510 Smart Thermostat April 10, 2017 05:12PM |
Registered: 7 years ago Posts: 10 |
Re: BV510 Smart Thermostat April 18, 2017 03:11PM |
Registered: 7 years ago Posts: 10 |
dim thSettingTemp(6) dim ActivationTemp, CurrentTemp, HeatingActive, Run, date_time(8), boost_end(8) dim lastSecs#, runMode, HeatSwitch, restBusy, ActiveSchedule, prevSetting dim isDebug, tpaBusy ... function timeSync() if restBusy = 0 then dim newdate$[80] newdate$ = wifi.dt$() if newdate$ <> "" then newdate$ = dt.bst$(newdate$) dt.time(newdate$,*date_time()) endif endif endf ... function startup() ... //Set initial time (IMPORTANT not to be empty so needs this) dt.time("Fri, 31 Mar 2017 13:09:00",*date_time()) timeSync() taskadd("clock()",1,60000,1) ... endf ... function clock() dim newDate newDate = dt.UTC(*date_time()) newDate = newDate + 60 dt.UTC2time(newDate,*date_time()) th.disp.updtime(*date_time()) //this is just to update the time/date on the display, works fine endf
// Cant set up as a library because of interrupt // // NOTES: rot.sw is 1 when not pressed and 0 when pressed, this will change // immediately the sw is pressed // // #include "c:\ByPic\lib\lib_intr.bas" // constant ROTARY_SW {PORTB(PORT),1} // dim rot.seqA, rot.seqB, rot.sw, rot.min, rot.max, rot.prevSW, rot.updatePending // ***************************************************************************** // ***************************************************************************** function rotary.start(min,max) rot.updatePending = 0 io_pinRole(*RB4(),IN,WPU) //CLK io_pinRole(*RB5(),IN,WPU) //DT io_pinRole(*RB2(),IN,WPU) //SW ir_io(*PORTB(),3,"rotary.irq",RB4(1)) ir_io(*PORTB(),3,"rotary.irq",RB5(1)) ir_io(*PORTB(),3,"rotary.irq",RB2(1)) rot.sw = 1 rot.min = min rot.max = max taskadd("rotary.updateThermostat()",5,100,1) endf function rotary.updateThermostat() if rot.updatePending = 1 then rot.updatePending = 0 dim t t = ActivationTemp / 100 th.disp.updlevel(t) endif endf // ***************************************************************************** // rotary encoder interrupt, sets global vearibales // ***************************************************************************** function rotary.irq() rot.sw = io_pinGet(*RB2()) // rot.seqA = rot.seqA << 1 rot.seqA = rot.seqA | io_pinGet(*RB4()) rot.seqB = rot.seqB << 1 rot.seqB = rot.seqB | io_pinGet(*RB5()) // rot.seqA = rot.seqA & 0xf rot.seqB = rot.seqB & 0xf // dim temp temp = ActivationTemp / 100 if (rot.seqA = 9) && (rot.seqB = 3) then if temp > rot.min then ActivationTemp = ActivationTemp - 100 rot.updatePending = 1 //taskadd("th.temp.set()",5,0,0) endif endif if (rot.seqA = 3) && (rot.seqB = 9) then if temp < rot.max then ActivationTemp = ActivationTemp + 100 rot.updatePending = 1 //taskadd("th.temp.set()",5,0,0) endif endif if rot.sw <> rot.prevSW then rot.prevSW = rot.sw if rot.prevSW = 0 then if th.setting.get(0) >= 2 then th.mode.set(0) else th.mode.set(th.setting.get(0) + 1) endif endif endif endf
Re: BV510 Smart Thermostat April 18, 2017 04:01PM |
Admin Registered: 11 years ago Posts: 351 |
Re: BV510 Smart Thermostat April 18, 2017 05:41PM |
Registered: 7 years ago Posts: 10 |
Re: BV510 Smart Thermostat April 19, 2017 03:22PM |
Admin Registered: 11 years ago Posts: 351 |
Re: BV510 Smart Thermostat April 22, 2017 10:36AM |
Registered: 7 years ago Posts: 10 |