Help please, For a servo operated mousetrap.

The bear

Senior Member
Hi Everyone,
Ideally to transform operation of servo into Led flashes:
Like 4 operations = 4 flashes and some mice.
The servo part is working as expected.
Don't hold back with any criticisms, hopefully the bear can take it.


Rich (BB code):
 #Picaxe 08M2 ;14M2 ;20M2  
#No_data
'#No_table
#Com 3
#Terminal 4800

'Sertxd( "Created on ", Ppp_datetime, Cr, Lf )

Symbol counter = w0
Symbol ledflag = C.1 ;To show its been operated
'Symbol


Do
Inc w0
   Pause 1000
  If counter > 1 Then
    High ledflag
     w1 = w0' * 2
    Write w1, Word w2
   Sertxd (Cr, Lf,#w1,"  " )
  Endif
  If pinC.3 = 1 Then 'Press a switch

Read w1, Word w2

Sertxd (Cr, Lf,#w1," " ) 'To determine how many operations.
;--------------------------------------------------------------------------------
Count C.4, 5000, w1 'Not working, no suprise!
Sertxd (Cr, Lf,#w1," " )

Endif
Loop
 

erco

Senior Member
Wish to help but I have no idea what you are doing or asking for. Mice? At least your servo is operating well, even though I can find no SERVO commands in your code.
 

hippy

Senior Member
Likewise it's not clear to me what the code is intended to achieve. Points I would make are -

It's best not to mix raw variable names (w0) and SYMBOL defined names (counter) in a program, use one or the other throughout.

It's not clear why you are writing to Eeprom, and why you are using 'w1' as the address to write to, and why you are setting that to 'w2' / 'counter' which overwrites the value you read with your COUNT command.

When 'pinC.3' is zero, presumable some switch not activated, your loop will repeat very quickly and you will be continually writing to Eeprom, could potentially wear that out in short order.
 

The bear

Senior Member
OK. Thanks for starters. apologies for the confusion. All I need now is some earache from Tex!
The servo part of the program is working, I didn't include it for that reason. I will now. The object of my previous code (#1), is to add it onto the Servo code, to ascertain how many operations of the servo compared to mice caught. It's a 'walk the plank' job, triggered by a PIR.
Rich (BB code):
;;Mouse PIR Servo v1.3 20.10.19 (v1.2 Cleaned up)
#Picaxe 14M2 ;20M2 08M2 ; 162 Bytes, 97 without sertxds
#No_data
'#No_table
#Terminal 4800
#Com 3

Sertxd(" Mouse PIR Servo v1.3 20.10.19 ",13,10)
Symbol ser_vo = B.2 'And Servopos
Symbol Pir = pinC.3
Symbol twoledflag = B.1 'Green
Symbol fiveledflag = B.3 'Red
Symbol counter = b0


init:
Pause 50000 'Wait for PIR to settle
Do
If pir = 1 Then  'PIR triggered
Servo ser_vo,90 ; start; Servo Drops 'was100
Inc b0
'Sertxd(" Mouse PIR Servo Down ",13,10)

Pause 2000 'was 3000
Servopos ser_vo,50 ;Return Servo to start position.

'Sertxd(" Mouse PIR Servo Horiz ",13,10)

'----------------------------------------------------------------
If counter = 2 Then     'New code request to replace this: <<<<<<<<<<<<<<<<<<<<<
High twoledflag
Endif
If counter = 5 Then
High fiveledflag
Low twoledflag
Endif
'------------------------------------------------------------
Pause 30000 'was 40000 'Wait for PIR to settle
Endif 'Takes ~ 20secs
Loop
 

The bear

Senior Member
It's best not to mix raw variable names (w0) and SYMBOL defined names (counter) in a program, use one or the other throughout.
Hi hippy, It's not too clear to me about mixing symbols and raw variables, a simple example would be appreciated. You are correct, ON/OFF switch when operated, leds are grounded. I have not used the Write & Read commands before, pretty obvious eh.
 
Last edited:

hippy

Senior Member
It's not too clear to me about mixing symbols and raw variables, a simple example would be appreciated
The basics are to 'not do so'. Either of these would be acceptable -
Code:
Do
  If pinC.3 = 1 Then
    Inc b0
    SerTxd( "Count is now ", #b0, CR, LF )
  End If
Loop
Code:
Symbol counter = b0
Do
  If pinC.3 = 1 Then
    Inc counter
    SerTxd( "Count is now ", #counter, CR, LF )
  End If
Loop
But, if you use 'b0' in one place and 'counter' in another it's not so clear, for example ...
Code:
Symbol counter = b0
Do
  If pinC.3 = 1 Then
    Inc b0
    SerTxd( "Count is now ", #counter, CR, LF )
  End If
Loop
There is nothing immediately obvious - looking at where it's done - that the 'counter' value you are showing is the 'b0' you just incremented. It's not clear if you intended to increment 'counter' or something else.

And, by using both, the reader has to remember 'b0' and 'counter' are the same.

"Smith entered the room, concealed himself. Mr Bad entered, turned on the light. Immediately 008 sprung into action". Is Smith that 008 ? You have to go back in the book and check if you can't remember.
 

lbenson

Senior Member
Re latest code, how does "counter" get to 2, and then move on to 5?

You might wish to try your code in the simulator and see what happens as you toggle C.1 (after reducing the pause lengths so you can see what happens without long waits).
 

The bear

Senior Member
Re latest code, how does "counter" get to 2, and then move on to 5?
@ Ibenson, It does work, if it operates twice, the green led lights, when pinC.3 = 1. When its operated (Inc ed) five times the red led comes on & the green led goes off. Its just a crude indicator. It gave me satisfaction that it worked.
BUT I would like to move on from that. Tested on the PE6 stimulator and for real.
 

lbenson

Senior Member
It does work
It works because b0 (which is incremented) has also been defined as the symbol, "counter"--hippy's suggestion (a good one) is that you not mix variable names and symbols--because it is easy later to forget that they're supposed to be the same and repurpose one--with confounding results.
 

mikeyBoo

Senior Member
hi Bear,
I built a squirrel trap a while back out of 4in PVC drain pipe. Since it was a "seat of the pants" project I didn't go to too much trouble making a scaled drawing.
However, if you look at the attached .jpeg, you can see how it works (no Picaxe needed).
It uses a small 12v solenoid triggered by a 9v battery. Since it cuts off as soon as the gate drops, the battery lasts a long time.
I used the board illustrated in this link to drive it
https://picaxeforum.co.uk/threads/cheap-easy-motion-detectors-night-light-controllers-picaxe-or-stand-alone.28233/

The flasher version (of above link) works great to scare off critters (deer, mice, etc.).
However, squirrels are smarter than other animals (everyone knows this if you've watched SpongeBob) so sometimes you just have to catch them.

You could use a Picaxe (via an opto) to monitor switch voltage & send a message or flash LEDs when you've caught a mouse.
That would make things easier on the Picaxe side. (You could diode-OR numerous traps to the same Picaxe.)
23276Good luck & post your project when you're done.
 

techElder

Well-known member
Hey, bear! That code looks good. Pay more attention to your SYMBOLS and you'll be fine.

I'm being nice! :D
23277
 
Top