In SX Microcontrollers, SX/B Compiler and SX-Key Tool, robotworkshop wrote: Doesn't seem verbose at all. Actually it's a lot shorter than what i'm trying to use in the program i'm working on at the moment. I'm going to go through and optimize mine a bit but want to get it working right first. I'm having a problem with it and I think I just need to take a step back or have another set of eyes look over the main routine. It is for an SX48 acting as a co-processor for my robot project. The ISR in the background is handing the reception of the serial commands which it buffers using your serial code. It also keeps track of an encoder for the motor. What i'm trying to do which is a bit different from your code above is to check to see if any serial data has been received. If some has come in then it will deal with it and then go on to do some other processing before going back to the main loop again. If no data has been received then it goes ahead and does it's processing and loops back to main to check for serial data and so on. If this works out the way I hope it may be a useful technique to incorporate into other SX/B programs. Any suggestions for the code below? The weird part is that it doesn't seem to fall through and do the regular processing unless I keep sending some data to it. It's almost as if my check to see if any characters are in the Serial receive buffer isn't working right. Besides that all the other routines seem to work ok. If I uncomment the first couple lines in the main routine it just displays the serial data on a set of 8 leds as a test. That part works perfect now. I had problems with that which were caused by extra Timer Interrupts but that was fixed by masking them off at the beginning of the program. Below is a section of the code where the main loop is: ' Enable ISR to start accepting serial data active = True ' Serial RX disabled until this flag is True HdBsy = False ' Tell Host we're ready for action! actmov = False ' We're not in an active movement. Main: 'Leds=RX_BYTE 'GOTO Main IF rxBufCnt = 0 THEN GOTO Motor_Control ENDIF ' cmdChar should be non-zero for a mutli-byte command IF cmdChar = 0 THEN char = RX_BYTE IF char = "C" THEN ' 67 GOSUB CLR_FLAGS GOTO Motor_Control ENDIF IF char = "G" THEN ' 71 GOSUB GO_TARGET ' Goto position encTarget GOTO Motor_Control ENDIF IF char = "H" THEN ' 72 GOSUB HOME_HEAD ' Set encTarget to 0 then set actmov = True GOTO Motor_Control ENDIF IF char = "I" THEN ' 73 GOSUB INIT_HEAD ' Initialize head to point forward and set encValue & encTarget to 0 GOTO Motor_Control ENDIF IF char = "F" THEN ' 70 cmdChar = char ' Set LEDs near wheels GOTO Motor_Control ENDIF IF char = "L" THEN ' 76 cmdChar = char ' Set LED's on front (8 LEDs) GOTO Motor_Control ENDIF IF char = "S" THEN ' 83 cmdChar = char ' Set new encTarget postion GOTO Motor_Control ENDIF ' We must have received a junk command. Just flash the LEDs for now. GOSUB COMM_ERR ELSE IF rxBufCnt > 0 THEN IF char = "F" THEN ' 70 GOSUB FOOT_LEDS cmdChar = 0 GOTO Motor_Control ENDIF IF char = "L" THEN ' 76 GOSUB Chest_Leds cmdChar = 0 GOTO Motor_Control ENDIF ENDIF IF rxBufCnt > 1 THEN char = RX_CHKBYTE IF char = "S" THEN ' 83 GOSUB Set_Target cmdChar = 0 GOTO Motor_Control ENDIF ENDIF ENDIF Motor_Control: IF actmov = True THEN ' Check encoder & remaining distance. ' Reached position? If so, stop movement and reset flag encOffset = encTarget - encValue IF encOffset.15 = True THEN encOffset = -encOffset ' Use 2's complement to get ABS value ENDIF IF encOffset < 2 THEN actmov = False TIMER1 TIMER LOW Spd1 HdBsy = False ELSE ' We're should be moving so adjust and update pwm encOffset = encTarget - encValue IF encOffset.15 = True THEN Dir1 = 1 ' If the difference is negative we should move left encOffset = -encOffset ' Use 2's complement to get ABS value ELSE Dir1 = 0 ENDIF ' Figure out how fast to move m1Spd = 750 ' Slow IF encOffset > 50 THEN m1Spd = 925 ' Medium ENDIF IF encOffset > 100 THEN m1Spd = 1110 ' Fast ENDIF TIMER1 PWM, m1Spd, dutyCycle ENDIF ENDIF ' check to see if head moved on it's own (or someone moved it) ' if more that allowable variance ' setup new movement and set flag ' if this has happened more than X then set head moved flag ' IF encTarget = encValue THEN ' GOTO Done ' ENDIF encOffset = encTarget - encValue IF encOffset.15 = True THEN encOffset = -encOffset ENDIF IF encOffset > 10 THEN actmov = True HdBsy = True ENDIF Done: GOTO Main ' Go back and do it all again. ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=252933#m253501 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2008 (http://www.dotNetBB.com)