Rampz
Well-known member
Hello All
Building a test rig for testing tilt switches, my best attempt at code below, having a few issues, atleast it mostly works firstly
1) I wanted to show in the terminal each time the tilt switch is operated on and off, but since i'm in a loop it shows multiple times, the switch controlling the motor and the led to display status of the tilt switch works nicely
2) Seems to be some issue where the test doesn't go for the required amount of microswitch activations that indicates reolutions of the test rig, as an example its set currently to 100 and test concludes around mid 50's, switch actions doesn't seem to be displaying number of tilt switch activations, something wrong
3) when test complete, eg number of revolutions done, i want to compare tilt switch activations against revolutions, i want to allow maybe addition 1 for error, if number are the same + 1 then go to PASS, if not go to FAIL
Building a test rig for testing tilt switches, my best attempt at code below, having a few issues, atleast it mostly works firstly
1) I wanted to show in the terminal each time the tilt switch is operated on and off, but since i'm in a loop it shows multiple times, the switch controlling the motor and the led to display status of the tilt switch works nicely
2) Seems to be some issue where the test doesn't go for the required amount of microswitch activations that indicates reolutions of the test rig, as an example its set currently to 100 and test concludes around mid 50's, switch actions doesn't seem to be displaying number of tilt switch activations, something wrong
3) when test complete, eg number of revolutions done, i want to compare tilt switch activations against revolutions, i want to allow maybe addition 1 for error, if number are the same + 1 then go to PASS, if not go to FAIL
Code:
' Test rig to test TILT switches, motor speed ccontrol to left of rig to set motor speed, will in time be replaced with PWM speed control. Button and associated led for setting up tilt switch under test position, button turns on motor and led operates as tilt switch operates. I did want to show on terminal when tilt swith operates but terminal shows that every loop not just when operated?
'Middle top button stops above loop and gets ready for start of test, resets counters, second press starts test which should be for a quantity of revolutions detected by the microswitch, reads correctly but duration of test is not in this case 100 microswitch operations.
'after test i need to compare revolutions against tilt switch activations allowing for an error of 1 or 2 and then go to either Pass or Fail, not working correctly
#picaxe 14m2
#no_data
#terminal 4800
'Constants
symbol INCH = PinC.2 'rotate motor to setup tilt switch
Symbol TESTswitch = PinB.3 'Switch on test
Symbol REVOswitch = PinC.4 'motor revolutions
Symbol START = PinB.5 'Start button
Symbol AXE134pin = C.1 'connected to screen input
symbol MotorRelay = B.1 'switch motor on and off
Symbol Mspeed = B.2 'motor speed output to mosfet
symbol led = B.4 'led to show tilt switch position for setup
Symbol Pressed = 1
symbol Released = 0
symbol REVOLUTIONS = 100
'Variables
symbol REVcount = W0 'count revolutions from microswitch of motor
symbol SWITCHcount = W1 'count activations of test switch
symbol TESTtime = W2 'quantity of motor revolutions needed to complete test
symbol COUNTdiff = b6 'difference between count allowed
symbol TOTALCount = W4 'total revolution count to test for fail
init:
pause 2000
SerTxd("Coding by Rampz", CR, LF )
SerTxd("Tilt Switch Test Rig", CR, LF )
SEROUT AXE134pin , N2400_4 , ( 254 , 128 ) 'set at first line
SEROUT AXE134pin , N2400_4 , ( "Code by Rampz")
low led
SEROUT AXE134pin , N2400_4 , ( 254 , 215 ) 'set at fourth line
SEROUT AXE134pin , N2400_4 , ( " " ) 'clear fourth line
REVcount = 0 'reset counter to 0
SEROUT AXE134pin , N2400_4 , ( 254 , 192 ) 'set at second line
SEROUT AXE134pin , N2400_4 , ( "Revolutions ", #REVcount, " ") 'display reset
SWITCHcount = 0 'reset counter to 0
SEROUT AXE134pin , N2400_4 , ( 254 , 148 ) 'set at third line
SEROUT AXE134pin , N2400_4 , ( "Switch Actions ", #SWITCHcount, " ") 'display reset
Setup: 'used to set switch under test in correct position
do
if TESTswitch = pressed then 'if switch on then turn led on
pause 100
SerTxd("Test switch on", CR, LF) 'trying to display when switch activated, not correct
high led
endif
if TESTswitch = released then 'if switched off then led off
pause 100
SerTxd("Test switch off", CR, LF) 'trying to display when switch off, not correct
low led
endif
if INCH = pressed then 'if switch is on turn on motor to aligh switch under test
pause 100
high MotorRelay
endif
if INCH = released then 'if switch off stop motor
pause 100
low MotorRelay
endif
loop until START = pressed 'if start button pressed exit loop an continue
pause 100
gosub BEGIN
BEGIN: 'reset counters and get ready to start the test
low MotorRelay
SerTxd("Motor Off", CR, LF)
Do : Loop Until START = Pressed ; Wait for push to turn motor on
Pause 200 'debounce
SEROUT AXE134pin , N2400_4 , ( 254 , 215 ) 'set at fourth line
SEROUT AXE134pin , N2400_4 , ( " " )
high MotorRelay
REVcount = 0 'reset counter to 0
SWITCHcount = 0 'reset counter to 0
COUNTdiff = 1 'difference allowed between REVcount and SWITCHcount
SerTxd("Motor On", CR, LF)
For TESTtime = 1 to REVOLUTIONS 'should count revolutions, i think its counting loops?
IF REVOswitch = Pressed then 'microswitch detecting revolutions
pause 100 'debounce
REVcount = REVcount + 1 'incrementing counter for each revoltion
TESTtime = TESTtime + 1 'incrementing counter for each revolution, so test concludes after quantity of revolutions
Endif
SerTxd("Revolutions", #REVcount,CR, LF )
SEROUT AXE134pin , N2400_4 , ( 254 , 192 ) 'set at second line
SEROUT AXE134pin , N2400_4 , ( "Revolutions ", #REVcount, " ")
IF TESTswitch = pressed then 'if button 1 pressed
pause 50 'debounce
SWITCHcount = SWITCHcount + 1
Endif
SerTxd("Switch Actions", #SWITCHcount,CR, LF )
SEROUT AXE134pin , N2400_4 , ( 254 , 148 ) 'set at third line
SEROUT AXE134pin , N2400_4 , ( "Switch Actions ", #SWITCHcount, " ")
TOTALcount = COUNTdiff + REVcount 'allowing an error of 1
IF TOTALcount > SWITCHcount then 'something wrong here too
Gosub Failed
endif
NEXT
Gosub Passed
Failed: 'should be at this point if the revolutions doesn't match test switch activations
SerTxd("Test switch FAILED",CR, LF )
SEROUT AXE134pin , N2400_4 , ( 254 , 215 ) 'set at fourth line
SEROUT AXE134pin , N2400_4 , ( "SWITCH FAILED" )
Gosub setup
Passed: 'should be at this point if the revolutions does match test switch activations
SerTxd("Test switch PASSED",CR, LF )
SEROUT AXE134pin , N2400_4 , ( 254 , 215 ) 'set at fourth line
SEROUT AXE134pin , N2400_4 , ( "SWITCH PASSED" )
Gosub setup
Attachments
-
495.7 KB Views: 34
-
485.5 KB Views: 36
Last edited: