Almost none of this is working.....WHY?!?!?!

buntay

Senior Member
Hello again friends,

I am about losing my mind on this one cause the code should work but virtually none of it is correct.
What am I missing?

Heres what I am trying to do,

I am working with RPM's using the "count" method, I have a proximity sensor connected to a 12v coil of a relay with a 10k resistor keeping the input pin low till pulled high by the 5v going through the switch on relay. all voltages have been checked and re-checked and then checked again
I would like to take 4 rpm readings and give me the average of those 4 readings.

here is the code and resulting data. chip is a 20x2
Code:
'picaxe 20x2

setfreq m8


main:

w0=0                                 'start with a word 0 value of 0
for b0=1 to 4                        'start a loop of 4
sertxd ("count of 4   ",#b0,cr,lf)   'tell me which count of 4 I am on
count  b.6, 4700, w0                 'count a switch and place in word 0
sertxd ("value ",#w0,cr,lf)          'give me the value of the count
w1= w0 + w0                          'take word 0 and add it to the previous word 0
sertxd ("new value   ",#w1,cr,lf)    'give value of word 1    
next b0                              'end of loop

w2 = w1/4                            'once the loop is finished divide word 1 to give me average of the 4 counts
sertxd ("average   " ,#w2,cr,lf)      'tell me the average and line feed

sertxd (cr,lf)
sertxd (cr,lf)
sertxd (cr,lf)
sertxd (cr,lf)
sertxd (cr,lf)
sertxd (cr,lf)

goto main

'THIS IS THE RESULTING TRANSMITTED DATA

'count of 4   1        'never changes from one
'value  233         '@ a little more than the 4 seconds value it should be around 12-16 (I can hear the relay ticks and in one second its about 3-4)
'new value   466       'is always double of the wrong rpm
'average   116         'the average of wrong rpm

What am I missing?
Thanks for any input

Buntay
 

nick12ab

Senior Member
w0 uses the same memory space as b0 and b1 so when you use w0 in that pulsin command in the loop which uses b0, you mess up the loop.
 

srnet

Senior Member
w0=0 'start with a word 0 value of 0
for b0=1 to 4 'start a loop of 4

Had you realised that variable w0 is also variables b0 and b1 ?

See manual 2 page 10.
 

SAborn

Senior Member
Its not a problem with the way you are sending the data out, but why not just send one string of data as it makes it easier to read.

Example:-

w0=0 '.......... 'start with a word 0 value of 0
for b7=1 to 4 '............ 'start a loop of 4
count b.6, 4700, w0 '........ 'count a switch and place in word 0
w1= w1 + w0 '........... 'take word 0 and add it to the previous word 0

sertxd ("count of 4 = ",#b7," value = ",#w0," new value = ",#w1,cr,lf)

next b7

Also note :- w1 = w0 + w0 .........was wrong and should have been as in the example above.

You will also need to add ..... w1 = 0 ....at the start of each loop as you have for w0 = 0
 

buntay

Senior Member
Thanks for the replies that solved my loop problem.....(need more coffee, should have caught that :rolleyes:)
but counts are still off and loop number still not correct and w1 is also still all wrong. here is new transmitted data.

count of 4 tell me which count of 4 I am on...1,2,3,or 4 0
value give me the value of the count 231 should be 3-4
new value give value of word 1 based on w0+w0 462


count of 4 tell me which count of 4 I am on... 1,2,3,or 4 231
value give me the value of the count 241 should be 3-4
new value give value of word 1 based on w0+w0 482


count of 4 tell me which count of 4 I am on... 1,2,3,or 4 241
value give me the value of the count 235 should be 3-4
new value give value of word 1 based on w0+w0 470


count of 4 tell me which count of 4 I am on... 1,2,3,or 4 235
value give me the value of the count 234 should be 3-4
new value give value of word 1 based on w0+w0 468


average 117
 

buntay

Senior Member
Thanks for the replies that solved my loop problem.....(need more coffee, should have caught that :rolleyes:)
but counts are still off and loop number still not correct and w1 is also still all wrong. here is new transmitted data.

ok everything good except my counts are still off, they should be around 3 or 4
 

buntay

Senior Member
ok everything good except my counts are still off, they should be around 3 or 4


a little easier to understand the data coming out. in the end all I need is the rpm this is all for debugging the code :)

count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 0
value give me the value of the count 243 should be 3-4
new value give value of word 1 based on w0+w0 243


count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 243
value give me the value of the count 244 should be 3-4
new value give value of word 1 based on w0+w0 487


count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 244
value give me the value of the count 271 should be 3-4
new value give value of word 1 based on w0+w0 758


count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 15
value give me the value of the count 252 should be 3-4
new value give value of word 1 based on w0+w0 1010


average 252
 

Technical

Technical Support
Staff member
The count command is just reporting what is coming in from the sensor - have you actually checked this signal is a clean 'bounce/noise' free signal as expected using an oscilloscope?
 

buntay

Senior Member
The count command is just reporting what is coming in from the sensor - have you actually checked this signal is a clean 'bounce/noise' free signal as expected using an oscilloscope?
Do not have a scope, but I did not take that into concideration, I now remember reading that one needs to be carefull using a relay as an input.

but if that is true why can I divide the count by 12 and have the count be on target?
count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 0
value give me the value of the count 214 should be 3-4 * the 4.7 second count = 15=18
new value give value of word 1 based on w1+w0 214
count /12 = 17

count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 214
value give me the value of the count 220 should be 3-4 * the 4.7 sec count= 15-18
new value give value of word 1 based on w1+w0 434
count /12 = 18

count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 220
value give me the value of the count 224 should be 3-4 * the 4.7 sec count= 15-18
new value give value of word 1 based on w1+w0 658
count /12 = 18

count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = 224
value give me the value of the count 206 should be 3-4 * the 4.7 sec count= 15-18
new value give value of word 1 based on w1+w0 864
count /12 = 17

average 216
 

buntay

Senior Member
In an effort to keep everybody upto speed here is the code as it sits used on my last post.

Code:
'picaxe 20x2

setfreq m8


main:

w0=0
w1=0                                                                                                                'start with a word 0 value of 0
for b8=1 to 4                                                                                                       'start a loop of 4
sertxd ("count of 4 tell me which count of 4 I am on... 1,2,3,or 4 but = ",#b0,cr,lf)                               'tell me which count of 4 I am on
count  b.6, 4700, w0                                                                                                'count a switch and place in word 0
sertxd ("value give me the value of the count   ",#w0,"   should be 3-4 * the 4.7 sec count= 15-17",cr,lf)          'give me the value of the count
w7= w0/12
w1= w1 + w0                                                                                                         'take word 0 and add it to the previous word 0
sertxd ("new value give value of word 1 based on w1+w0  ",#w1,cr,lf)                                                'give value of word 1 based on w0+w0    
sertxd ("count /12 = ",#w7)
sertxd (cr,lf)
sertxd (cr,lf)


next b8                                                                                                             'end of loop

w2 = w1/4                                                                                                           'once the loop is finished divide word 1 to give me average of the 4 counts
sertxd ("average   " ,#w2,cr,lf)                                                                                    'tell me the average and line feed


sertxd (cr,lf)
sertxd (cr,lf)
sertxd (cr,lf)
sertxd (cr,lf)

goto main
 

hippy

Ex-Staff (retired)
In an effort to keep everybody upto speed here is the code as it sits used on my last post.
Golden rule number one - simplify.

Just have the bare minimum needed to test whatever you are trying to test ...

Do
Count B.6, 4700, w0
SerTxd( "Count B.6, 4700 = ", #w0, CR, LF )
Loop

Then you can also test the B.6 input by using PWMOUT at 1kHz on C.5, connect the B.6 input to C.5 to sanity check it is working as expected ...

PwmOut PWMDIV4, C.5, 249, 500 ' 1kHz

Run that and you should see a counted value of around 4700.
 

lewisg

Senior Member
In an effort to keep everybody upto speed here is the code as it sits used on my last post.
Golden rule number two - use symbols.
Code:
'picaxe 20x2

symbol wCount = w0                                                      'receives output of sensor
symbol wAvCnt = w1                                                      'accumulates wCount and results of average operation
symbol wWhat  = w7                                                      'wCount divided by 12  (what?)
symbol bLoop  = b8                                                      'loop counter

symbol sensor = B.6                                                     'define sensor pin here to make changes easy


start:

    setfreq m8
    pause 1000                                                          'need a pause
    sertxd ("bLoop", 9, "wCount", 9, "wAvCnt", 9, "wWhat", 13,10)       'makes a nice header


main:

    wAvCnt = 0                                                          'reset accumulator, wCount gets overwritten each loop

    for bLoop = 1 to 4
        count  sensor, 4700, wCount
        wWhat = wCount / 12
        wAvCnt = wAvCnt + wCount
        sertxd (#bLoop, 9, #wCount, 9, #wAvCnt, 9, #wWhat, 13,10)       'spit results of this loop
    next bLoop

    wAvCnt = wAvCnt / 4                                                 'get average
    sertxd ("average = " ,#wAvCnt, 13,10)                               'spit average
    
    sertxd (13,10)                                                      'blank line
    sertxd ("bLoop", 9, "wCount", 9, "wAvCnt", 9, "wWhat", 13,10)       'new nice header

goto main
Using symbols takes no more space or runtime but makes your code easier to read and reduces errors several ways:

1. If you can read it you make fewer mistakes

2. By looking at the symbols listing you can easily see conflicts.

3. By working with symbols you don't mess up as much in code blocks since you are working with something you assigned.

4. Makes changing variable locations easier and less error prone since you only have to change in one place. Notice how you used b0 in the 4th line in the main main: block. This was left over from your original post and never updated. What you are getting is part of w0, not the loop counter.

5. Good use of symbols can make you code almost self documenting for simple projects like this.


While I left your original variable locations there is a school of thought here that is a good idea. Start your byte vars from b0 and your word vars from the top down. So if you really are programming this into a X2 chip it would be:
Code:
symbol bLoop  = b0	
symbol wCount = w25
symbol wAvCnt = w26
symbol wWhat  = w27
Make sense?
 

inglewoodpete

Senior Member
Golden rule number two - use symbols.

While I left your original variable locations there is a school of thought here that is a good idea. Start your byte vars from b0 and your word vars from the top down. So if you really are programming this into a X2 chip it would be:
Code:
symbol bLoop  = b0	
symbol wCount = w25
symbol wAvCnt = w26
symbol wWhat  = w27
Make sense?
A man after my own heart. That makes great sense.

I use a similar naming convention, extended as follows, with the exception that I leave b0/b1 free wherever possible to use for bit variables. When you're in the middle of redeveloping a 1,000 line program, it helps to know the type of each symbol.

inglewoodpete's roolz:
bLoop - a byte variable
wCount - a word variable
tStatus - a bit variable
cStartValue - a constant
rStore - a RAM location
eLookup - an EEPROM location
iSwitch - an input pin
oLED - an output pin​
 

Goeytex

Senior Member
@Buntay You have received excellent advice so far and should take note.

Let me add.

A relay usually has a delay time from 5ms - 20ms both on and off. It takes from 5 to 20 ms for the contacts to close and from 5 to 20 ms for the contacts to open. That means an effective maximum frequency from around 25 cps to 100 cps depending upon the relay. Of course most relays will wear out very fast at these speeds. In process control a general guideline is that a relay should not activate more than 1 time every 5 seconds to extend its working life.

Also the contacts WILL bounce, giving false readings. This will usually be anywhere from 2 to 10 pulses before the contacts fully close and stabilize. Try putting a 100nf capacitor across the relay contacts to filter the signal so that the Picaxe does not see the bouncing. Assuming a N.0. relay, and 5v on the common contact, there should be a resistor pulling down the output contact. 10K should work ok.
 

Attachments

Last edited:

SAborn

Senior Member
The best thing you can do is get rid of the relay, if you need full isolation use a opto coupler in replacement of the relay, or a simple NpN transistor as in the circuit below should work more reliable without all the switching noise of the relay contacts.

More details on the proximity sensor would help with better options.

NPN transistor circuit.JPG
 

Goeytex

Senior Member
More good advise.

I was hoping the OP would come to that conclusion oh his own, giving the switching characteristics
of a relay.

Yet we do not know why a relay is being used in the first place. Maybe a high current load ?
We also don't know the expected switching frequency and other details.

A detailed schematic would be very helpful.

Buntay Previously Asked ...
but if that is true why can I divide the count by 12 and have the count be on target?
Answer: Because the contacts are bouncing about 12 times before settling down.
 
Last edited:
Top