WANTED: Advice, comments, criticisms...
Steve in NM
Posts: 54
I know just enough to be dangerous. The code below works, works well, and works reliably. When I run it stand-alone, the motor runs much smoother than when the stamp·has to check for serial input.
My question is, can it be made more efficient? That is, can it be made to run fatser?
' {$STAMP BS1}
' {$PORT COM1}
LET DIRS = %11110000
SYMBOL·DELAY = B1
SYMBOL·X = B2
Start:
SERIN 2, N2400, ("!"), B2
IF B2 = 2 THEN CW_FAST
IF B2 = 8 THEN CW_SLOW
IF B2 = 4 THEN CCW_FAST
IF B2 = 6 THEN CCW_SLOW
PINS=%00000000· '=============== Turn off all pins to save power
GOTO START
CW_FAST:
DELAY=1 'step speed
'====================================== Run CW_FAST
PINS=%00110000 'I/O pins 0 and 1 high
PAUSE delay
PINS=%01100000 'I/O pins 2 and 3 high
PAUSE delay
PINS=%11000000 'I/O pins 3 and 4 high
PAUSE delay
PINS=%10010000 'I/O pins 3 and 0 high
PAUSE delay
GOTO START
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The reason we're all here, is because we're not all there.
My question is, can it be made more efficient? That is, can it be made to run fatser?
' {$STAMP BS1}
' {$PORT COM1}
LET DIRS = %11110000
SYMBOL·DELAY = B1
SYMBOL·X = B2
Start:
SERIN 2, N2400, ("!"), B2
IF B2 = 2 THEN CW_FAST
IF B2 = 8 THEN CW_SLOW
IF B2 = 4 THEN CCW_FAST
IF B2 = 6 THEN CCW_SLOW
PINS=%00000000· '=============== Turn off all pins to save power
GOTO START
CW_FAST:
DELAY=1 'step speed
'====================================== Run CW_FAST
PINS=%00110000 'I/O pins 0 and 1 high
PAUSE delay
PINS=%01100000 'I/O pins 2 and 3 high
PAUSE delay
PINS=%11000000 'I/O pins 3 and 4 high
PAUSE delay
PINS=%10010000 'I/O pins 3 and 0 high
PAUSE delay
GOTO START
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The reason we're all here, is because we're not all there.
Comments
1) Instead of relying on the SERIN directly for commands, maybe you could use an command-enable line. Drop it low to tell the stamp you're about to send a command via serial port, send the command, then pull it high again after. That way instead of the SERIN in the motor loop the stamp can just check the state of the enable line. That I think would be much quicker.
2) This wouldn't make it faster but it might make the motor's movements smoother: Only perform one step each time you call CW_FAST. That way it will be the same amount of time between steps instead of doing 4 steps quickly, then pausing.
#1 won't work. There's another part to this equation that I neglected to disclose: This is the RX half of a TX-RX radio-link combo.
#2 might help, IF I knew how to get back into the code where I left off. ???
The TX code is below. I hadn't considered that it could be faster. I had wondered if it was faster or more efficient way to transmit/receive the commands.
' {$STAMP BS1}
' {$PORT COM1}
Start:
IF PIN6 = 0 THEN RIGHT
IF PIN5 = 0 THEN LEFT
IF PIN4 = 0 THEN BACKWARD
IF PIN3 = 0 THEN FORWARD
GOTO Start
PULSOUT 0, 300 'Sync pulse for the receiver
· SEROUT 0, N2400, ("!", 5)
PAUSE 10
GOTO Start
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The reason we're all here, is because we're not all there.
' {$STAMP BS1}
' {$PORT COM1}
EEPROM (%00110000, %01100000. %11000000, %10010000)
SYMBOL DELAY = B1
SYMBOL X = B2
SYMBOL StepIndex = B3
LET DIRS = %11110000
LET StepIndex = 0
Start:
SERIN 2, N2400, ("!"), B2
IF B2 = 2 THEN CW_FAST
IF B2 = 8 THEN CW_SLOW
IF B2 = 4 THEN CCW_FAST
IF B2 = 6 THEN CCW_SLOW
PINS=%00000000 '=============== Turn off all pins to save power
GOTO START
CW_FAST:
DELAY=1 'step speed
'====================================== Run CW_FAST
READ StepIndex, PINS
StepIndex = (StepIndex + 1) // 4
PAUSE delay
GOTO START
I haven't worked with PBASIC 1.0 in a while... hope I got it right. Well I think you get the idea anyhow.
As for the Tx/Rx link, yeah that is going to put a damper on speed. The stamps aren't too good at multi-tasking.