Rotary encoder read/write

troyjcm

New Member
Hi,



I have a rotary encoder and used some code from Hippy and some other posts that works well apart from 2 points below. The code will be used to set 2 values for a program to refer to. It is for my brewery where two temperatures are preset by the user.


I'm using a picaxe 40x2 and a 4 row OLED screen.


1. Can only increase the counter value when turning clockwise or anticlockwise (not sure what I missed in the posts but can't get it to decrease)

2. I can't get both the counter values to write to memory - only the second

The rotary encoder is used to increase the temp on the display OLED. Encoder switch takes user to a screen showing new temp has been set to "x". The user selects the next value for the second temperature and hits the rotary switch taking them to the screen showing the second value preset. Pause in the code for 5000 then show both new preset values.

This all works fine except I have added pinc.2 as a switch on start up to take the user to "Test" which should show the two temp values saved to internal memory so I can check that they have saved. The problem is that both values are equal to the second or last value preset. and the first is lost.

Many thanks

Phil

pins b.0 and b.1 for the two signal pins on the encoder and pinc.3 for the switch


#Picaxe 40X2


; 21-
Symbol IRQ_A = %010 ; Signal A = pin B.0 HINT1
Symbol PIN_B = pinB.1 ; Signal B = pin B.1

Symbol counter = w0 ; b1:b0
Symbol detect = b2


Symbol thisCount = w2 ; b5:b4
Symbol lastCount = w3 ; b7:b6




symbol OLED1 = d.7


symbol ROTstrikesave = b22
symbol ROTspargesave = b15



main:

if pinc.2 = 1 then goto test 'If pinc.2 is high on start up got to test

MainProgram:



serout OLED1,N2400,(252,1)
pause 30

pullup on
Gosub Interrupt_Enable
Do
if pinc.3 = 1 then goto saved 'if rotary switch is high goto saved

thisCount = counter
If thisCount <> lastCount Then
lastCount = thisCount
If thisCount >= $8000 Then ; Negative
thisCount = -thisCount
write b16, word thiscount 'write thisount value into b16

serout d.7,N2400,(254,128,"Set Stike Temp:",#thiscount,%11010010,"C ")


End If
End If
Loop



saved:



read b16, ROTstrikesave ' read b16 into ROTstrikesave

serout OLED1,N2400,(252,1)
pause 30
serout OLED1,N2400,(254,128,"New stike Temp will ",254,192,"be saved at: ",#ROTstrikesave,%11010010,"C ",254,148," ",254,212," ")

SETINT OFF ' set interupt off so that program can move to mainprogram2


pause 2500




mainprogram2:

let counter = 0 ' let counter = 0 so new value to be preset starts with 0 rather than previous preset value


serout d.7,N2400,(254,128,"Set Sparge Temp:",#thiscount,%11010010,"C ",254,192," ",254,148," ",254,212," ")


serout OLED1,N2400,(252,1)
pause 30

pullup on
Gosub Interrupt_Enable


Do
if pinc.3 = 1 then goto saved2

thisCount = counter
If thisCount <> lastCount Then
lastCount = thisCount
If thisCount >= $8000 Then ; Negative
thisCount = -thisCount
write b17, word thiscount

serout d.7,N2400,(254,128,"Set Sparge Temp:",#thiscount,%11010010,"C ")


End If
End If
Loop



saved2:

SETINT OFF



read b17, ROTspargesave

serout OLED1,N2400,(252,1)
pause 30
serout OLED1,N2400,(254,128,"New Sparge Temp will ",254,192,"be saved at: ",#ROTspargesave,%11010010,"C ",254,148," ",254,212," ")

Pause 5000



serout OLED1,N2400,(254,128,"New Strike is: ",#ROTstrikesave,%11010010,"C ",254,192,"New Sparge is: ",#ROTspargesave,%11010010,"C ",254,148," ",254,212," ")
pause 5000



test:

read b16, ROTstrikesave

read b17, ROTspargesave


serout OLED1,N2400,(254,128,"New Strike2 is: ",#ROTstrikesave,%11010010,"C ",254,192,"New Sparge is: ",#ROTspargesave,%11010010,"C ",254,148," ",254,212," ")

pause 500

goto test



end

Interrupt:


counterdetect:

counter = detect / 16 Max 1 ^ PIN_B * 2 - 1 + counter



Interrupt_Enable:
detect = IRQ_A * 16 ^ detect | IRQ_A
flags = 0
HintSetup detect
SetIntFlags IRQ_A, IRQ_A
Return
 

AllyCat

Senior Member
Hi,

It's difficult to analyse how the program is supposed to work without any hardware, but the following appears to have a number of potential errors:
Code:
If thisCount >= $8000 Then       ; Negative
thisCount = -thisCount
write b16, word thiscount     'write thisount value into b16
....
write b17,  word thiscount
Firstly, WRITE b16 , WORD thiscount does NOT write the value into b16 because b16 is an address pointer to an EEPROM memory location. Since you do not appear to have initialised b16 it will still have the value 0 (as will b17). So it appears that your program is reading and writing "ROTstrikesave" and "ROTspargesave" to the same memory location (zero). You probably need, for example SYMBOL ROTstrikesave = 1 (i.e. EEPROM memory location 1) and then use READ / WRITE ROTstrikesave , thiscount , and a different location for "ROTspargesave".

Secondly, the WORD qualifier writes to two consecutive bytes, so IF the line above was writing to b16, it would also write (the high byte) to b17, thus again the ROTspargesave value would be lost (or overwrite a required value).

Thirdly, the code is simply flipping any negative value to the opposite positive value before saving it. I don't know if you actually want to support negative values, but if not, then it would be better to set a limit value, e.g. with a MIN 1 (or a Limit Test if the value has already under-flowed).

To be honest, I can't see how the counter = detect / 16 Max 1 ^ PIN_B * 2 - 1 + counter is supposed to work. I suspect it was devised to work with some very specific pin numbers that might not have been reproduced correctly?

Cheers, Alan.
 
Top