Help from picaxe experts

BrunoCH

Member
Dear Picaxe experts, I have a small problem with my program. I want to read an RC signal that is written to the eeprom at a certain position to switch an LED on or off, at the beginning it works fine, but after a short time the LEd doesn't switch on anymore at the programmed point. if i switch off the picaxe and start it again, it works again for a while. i suspect that the eeprom content is lost or overwritten. what can i do?
(attached the program)

Thanks Bruno

Prog:

#picaxe 08M2
setfreq m32 ;Takt intern 32Mhz
symbol Var_a = w1
symbol Var_1 = w10

pause 1000

if pinC.3 = 0 then goto puls
if pinC.3 = 1 then goto impuls_messen


impuls_messen:
pulsin C.4,1,Var_a ; misst Puls an C.4 in Variable Var_a
write Var_a,w1
if pinC.3 = 0 then goto puls
if pinC.3 = 1 then goto impuls_messen

puls:
read Var_a,w1
pulsin C.4,1,Var_1 ; misst Puls an C.4 in Variable w10
if Var_a < Var_1 then goto ein
if Var_a > Var_1 then goto puls

ein:
high C.2 ; Ausgang C.1 auf High setzen
pause 200 ; Pause
low C.2 ; Ausgang C.1 auf Low setzen
pause 6000 ; Pause
goto puls ; loop zurueck zu puls
 

lbenson

Senior Member
Welcome to the forum.

Not sure I can answer your question, but here are some thoughts. First, the code can be shortened, since if pinC.3 doesn't have one value (0 or 1) then it must have the other, so this should do the same as the code you posted.
Code:
#picaxe 08M2
setfreq m32 ;Takt intern 32Mhz
symbol Var_a = w1
symbol Var_1 = w10

pause 1000

if pinC.3 = 0 then goto puls

impuls_messen:
  pulsin C.4,1,Var_a ; misst Puls an C.4 in Variable Var_a
  write Var_a,w1
  if pinC.3 = 1 then goto impuls_messen

puls:
  read Var_a,w1
  pulsin C.4,1,Var_1 ; misst Puls an C.4 in Variable w10
  if Var_a > Var_1 then goto puls

ein:
  high C.2 ; Ausgang C.1 auf High setzen
  pause 200 ; Pause
  low C.2 ; Ausgang C.1 auf Low setzen
  pause 6000 ; Pause
  goto puls ; loop zurueck zu puls
The biggest issue is, once you have gotten to "puls", how do you ever get out?

Next, what is the range of values which may be read with "pulsin C.4,1,Var_a"? You're reading into a word variable (possible values 0-65525), and then using that value as an address to write to eeprom, which can have addresses of 0-255 on an 08M2. If the address you write to is greater than 255, the address you write to will be Var_a//256, so may not be what you expect.

In addition, Var_a is defined as W1, so when you execute "write Var_a,w1" you are always writing the value in W1 to the address specified by W1, which seems sort of pointless unless you just want to know that you have received that value, and so its location becomes non-zero.

You are also doing no debouncing of pinC.3, so you may be getting multiple cycles for what you have thought is a single press. You might want to try something like this:
Code:
if pinC.3 = 0 then 
  pause 200 ' or maybe 25 if no SETFREQ M32
  do while pinC.3=0: loop ' wait until button is released
  goto puls
endif
Finally, do you really have a need for "setfreq m32"?

Perhaps it would help if you could explain in more detail what you are trying to do and what kinds of values you expect to be input.

You might do well to step through your code in the simulator to see exactly how it progresses with various inputs for your pulsin commands and button presses.
 
Last edited:

BrunoCH

Member
thank you very much for the quick response lbenson.
I know it's a bit complicated, the idea is to have an extendable headlight on a model and to switch it on and off by storing the ON position once into the eeprom with a button on C.3.
The value of the pulse is about 1-2 ms with a 50ms pause (RC signal), so the switch on can be selected according to the servo position. 32Mhz are only one attempt and not necessary. I am still a little bit preloaded because I have always programmed PIC controllers with the Basic from IL-Lehmann and did not have to care about addresses, the addresses were automatically assigned correctly by the Basiccompiler. But I want to continue with the PICAXE and learn something new.
I will try to get the values 0-255 to the right address and debounce the button.
THANK YOU VERY MUCH I will get back to you ....

Bruno

Translated with www.DeepL.com/Translator (free version)
 

AllyCat

Senior Member
Hi,

Yes, all of the above, but I'd also be looking particularly at:
Code:
impuls_messen:
pulsin C.4,1,Var_a    ; misst Puls an C.4 in Variable Var_a
write Var_a,w1  ...
if pinC.3 = 1 then goto impuls_messen
If pinC.3 = 1 then you have a tight loop with several potential issues: If PULSIN completes within about 4 ms then you will not comply with the specified 5 ms write cycle time for the WRITE process . At 32 MHz, the byte value (which is all that's stored by the write command) will overflow with any pulse longer than about 300 us. Also, if the delay or pulse length exceeds about 80 ms then the command will "Timeout", returning a zero (Word) value which you are not testing for.

Finally, beware that the EEPROM (Data) memory has a minimum "Byte Endurance" of 100,000 Write cycles. It might be much larger, but if your pulses occur every 50 ms and pinC.3 = 1 then that memory byte could "wear out" in not much more than an hour ! :(

Cheers, Alan.
 
Last edited:

BrunoCH

Member
Hi, Alan,
thanks for the info. the button is only pressed for a short time but i see that also "for a short time" some write cycles happen.
i do the same with the Basic from Lehmann and the oldest switch is running for over 10 years now.
i'm afraid that i can't use the PICAXE for this. unfortunately i don't know the original name of the PIAXE. until now i've used a PIc 16F628. i'll try to describe the eeprom only once when i press the button.

THANK YOU ALAN

Translated with www.DeepL.com/Translator (free version)
 
Top