put, get, some math... then do something

ol boy

Member
Using a 20X2.

I want to count on pin6 for 1 second. Readings will be from 0 to 140 hz and are stored in b6. I want to jump to parts of my code if the change in hz is eighter within or outside a rate of change. If the count changes more than ie. 5 from one second to the next then flash_1... If the change in hz is less then 5 continue on with the code.

Can someone give be a better example of how I'm PUTing and GETting info in to the RAM and retreving it?

There is what I've came up with so far.

put 1, b6
put 1, word w5
get 1, b9
get 1, word w7
symbol previous = w5
symbol new = w7

if w7-w5>5 then flash_1
if w5-w7>5 then flash_1

not sure this is right.
Thanks Ryan
 

westaust55

Moderator
In terms of the need to PUT and GET, it depends on how many variables you are already using.

If you posted you full code someone could possibly give you better options.

What are b6 and b9 used for?

You declare symbols:
symbol previous = w5
symbol new = w7​

but then do not use them (ie the “previous” and “new” as variable names


One variant in determining the difference between previous and latest reading is as follows:
If w7 >= w5 then ; determine the difference from previous reading
Diff = w7 – w5
Else
Diff = w5 = w7
Endif
If diff > 5 then flash_1 ; action if difference exceeds a specified value
 

hippy

Ex-Staff (retired)
put 1, b6
put 1, word w5


After you first "PUT 1,b6", the value which is stored in scratchpad location 1 is then overwritten by the subsequent "PUT 1, WORD w5" data.
 

ol boy

Member
Thanks, I had to reread and reread the manual to figure out the deal with using "word". You only need to use word when the number put or get will be larger than 255. The hole reason for a word var. got it now.

After some trial and errior I came up with this and it seems to work on the sim. Increasing the count quicker then the code is cycling will cause the code to jump to flash_1, but when changed at a slower rate it resumes the rest of the code.

main:
let dirsc = %00000000 'set portC as inputs
let dirsb = %10111111 'set portB as out puts not C.6(input)
let adcsetup = %0000000111000100

put 1, b6

readadc 9, b1 'read adc1 airtank psi
readadc 8, b2 'adc2 front height input
readadc 7, b3 'adc3 rear height input
readadc 3, b7
count 6, 1000, b6 'count low to high pulses over 1 second (raising edge)
if b1 < 220 then high 1 'turn on compressor when below adc count 220
elseif b1 > 230 then low 1 'turn off compressor when above adc count 230
endif

if b1 < 180 then flash_1 'if tank below 180 adc all output will stay low

get 1, b5
let b10 = b5 - b6
let b11 = b6 - b5
if b10 >= 5 then flash_1
if b11 >= 5 then flash_1
Placeing PUT before pin6 is counted makes sure the last reading is measured first before the current is read...? Is this right or can the PUT and GET be placed anywhere in the code? Or does it matter?

Thanks, Ryan

This is getting fun.
 

hippy

Ex-Staff (retired)
Why not simply move b6 into b5 rather than doing a PUT and a later GET ?

Also, given the PICAXE only deals with positive numbers ( what would be negative numbers become large positive numbers ), your difference code is not very reliable; see westaust55's code in post #2.

let b10 = b5 - b6
if b10 >= 5 then flash_1

If b5 is 20, b6 is 21, b10 becomes -1, which is actually 255 in PICAXE maths. That would give the impression of there having been a change greater than 5 even though there wasn't.
 

ol boy

Member
Here is what I came up with and seems to be working on the sim. Now I can get a possitive number when the numbers go from high to low and low to high.

What does the code do while counting for 1 second? Does it continue to store b5 until b6 is updated? What about clearing b10 and b11, I noticed if you don't clear the values in b10 and b11, every cycle they will begin to add and grow larger as b6 changes.

How do you slow the sample rate down, or can you?

Thanks Ryan
symbol prev=b5
symbol current=b6

readadc 9, b1 'read adc1 airtank psi
readadc 8, b2 'adc2 front height input
readadc 7, b3 'adc3 rear height input
readadc 3, b7
get 1, prev
count 6, 1000, current 'count low to high pulses over 1 second (raising edge)

put 1, current
if b1 < 220 then high 1 'turn on compressor when below adc count 220
elseif b1 > 230 then low 1 'turn off compressor when above adc count 230
endif

if b1 < 180 then flash_1 'if tank below 180 adc all output will stay low
let b10 = 0
let b11 = 0
if current > prev then accel
if current = prev then speed
if prev > current then deccel

accel:

let b10 = current-prev
if b10 > 5 then flash_1
goto speed

deccel:

let b11 = prev-current
if b11 > 5 then flash_1
goto speed
 

hippy

Ex-Staff (retired)
What does the code do while counting for 1 second?
Nothing other than count. When it finishes it puts the value counted into the variable specified.

Does it continue to store b5 until b6 is updated?
All variables and memory remain as they are set until you explicitly write a different value to them.

What about clearing b10 and b11, I noticed if you don't clear the values in b10 and b11, every cycle they will begin to add and grow larger as b6 changes.
It's very confusing referring to 'b' variable names when your code is a mix of 'b' variables and variables with meaningful names. It's best to use just one or the other throughout the code and in discussion and meaningful names would be recommended, eg -

Code:
Symbol rearHeight    = b3
Symbol unknown       = b4
Symbol currentSpeed  = b5
Symbol previousSpeed = b6
Symbol difference    = b7

ReadAdc 9, airTankPsi 
ReadAdc 8, frontHeight
ReadAdc 7, rearHeight
ReadAdc 3, unknown

previousSpeed = currentSpeed

count 6, 1000, currentSpeed

Select Case airTankPsi
  Case < 220 : High 1 ' Turn compressor on
  Case > 230 : Low  1 ' Turn compressor off
End Select

If airTankPsi < 180 Then Flash_1

If currentSpeed >= previousSpeed Then
  difference = currentSpeed - previousSpeed
Else
  difference = previousSpeed - currentSpeed 
End If
If difference > 5 Then Flash_1
As to variables growing; they'll do whatever your program tells them to.

How do you slow the sample rate down, or can you?
You can put a PAUSE in the main loop of your code.
 

ol boy

Member
okay, thanks It's starting to come together now.

As for the mix of b.. and symbols I'll pick one or the other and get it fixed.

Give me a few days to make the changes and I'll post the full code for you all to look at.

Thanks again, Ryan
 

ol boy

Member
Made the changes and it seems to work well in the sim. Is there a way to make smaller changes in values when using the generic field for count? 50 is way to big for what I'm trying to test.

'airride based on veh speed, 4 ride hights for 4 different speed zones 423 byts. PIX 20X2 TEST>>>>>!
'This code uses the count command to count the low to high transitions over 1 second of time(Hz) to determin
'speed.
main:
let dirsc = %00000000 'set portC as inputs
let dirsb = %10111111 'set portB as out puts
let adcsetup = %0000000111000100


symbol airtank=b1
symbol front=b2
symbol rear=b3
symbol prevspeed=b5
symbol currentspeed=b6
symbol difference=b7
symbol manual=b8

readadc 9, airtank 'read adc1 airtank psi
readadc 8, front 'adc2 front height input
readadc 7, rear 'adc3 rear height input
readadc 3, manual 'manual setting

prevspeed = currentspeed

count 6, 1000, currentspeed 'count low to high pulses over 1 second (raising edge)

select case airtank
case < 220 : high 1 ' Turn compressor on
case > 230 : low 1 ' Turn compressor off
end select

if airtank < 180 then flash_1 'if tank below 180 adc all output will stay low

if currentspeed >= prevspeed then
difference = currentspeed - prevspeed
else
difference = prevspeed - currentspeed
endif
if difference > 5 then flash_1

select case manual
case 55 to 155 goto flash_4 'maunal set ride height by switching input pin high
case 156 to 200 goto flash_3 'maunal set ride height by switching input pin high
case 201 to 255 goto flash_2
endselect 'maunal set ride height by switching input pin high

select case currentspeed
case 0 to 60 goto flash_5 'numbers are in Hz. Pulses over 1 seconds time
case 61 to 83 goto flash_4 '2 pulses per drive shaft turn
case 84 to 100 goto flash_3 '60 = 40mph, 84 = 55mph, 100 = 65mph
case 101 to 254 goto flash_2 '4 ride heights for 0 to 40, 40 to 55, 55 to 65 and 65+
endselect

flash_5:
if front < 170 then high b.2 'if adc is below 170 turn output 2 on
elseif front > 180 then low b.2 'adc count above 180 turn output 2 off
endif

if rear < 170 then high b.4 'if adc is below 170 turn output 4 on
elseif rear > 180 then low b.4 'adc count above 180 turn output 4 off
endif

if front > 180 then high b.3 'turn on output 3
endif
if rear > 180 then high b.5 'turn on output 5
endif
pause 1000 'pause for 1 sec
low 3 'turn off 3
low 5 'turn off 5
goto main 'start over

flash_1: 'from above
low b.2 'output 2 low
low b.3 'output 3 low
low b.4 'output 4 low
low b.5 'output 5 low
goto main 'start over

flash_2: 'from above
if front < 150 then high b.2 'if adc is below 150 turn output 2 on
elseif front > 160 then low b.2 'adc count above 160 turn output 2 off
endif

if rear < 150 then high b.4 'if adc is below 150 turn output 4 on
elseif rear > 160 then low b.4 'adc count above 160 turn output 4 off
endif

if front > 160 then high b.3
endif
if rear > 160 then high b.5
endif
pause 1000
low b.3
low b.5
goto main

flash_3: 'from above
if front < 130 then high b.2 'if adc is below 130 turn output 2 on
elseif front > 140 then low b.2 'adc count above 140 turn output 2 off
endif

if rear < 130 then high b.4 'if adc is below 130 turn output 4 on
elseif rear > 140 then low b.4 'adc count above 140 turn output 4 off
endif

if front > 140 then high b.3
endif
if rear > 140 then high b.5
endif
pause 1000
low b.3
low b.5
goto main

flash_4:
if front < 120 then high b.2 'if adc is below 130 turn output 2 on
elseif front > 130 then low b.2 'adc count above 140 turn output 2 off
endif

if rear < 120 then high b.4 'if adc is below 130 turn output 4 on
elseif rear > 130 then low b.4 'adc count above 140 turn output 4 off
endif

if front > 130 then high b.3
endif
if rear > 130 then high b.5
endif
pause 1000
low b.3
low b.5
goto main
I've read the posts reguarding PIXACE and automotive uses. This is more of an educational thing for me. Might try something in the future, but we'll see.

Thanks Ryan
 
Top