In SX Microcontrollers, SX/B Compiler and SX-Key Tool, skynugget wrote: hey all, i have some code here that polls six buttons, then spits out a midi code that corresponds with the button pressed. After the serial string is sent, it lights up one of 6 led's also corresponding to the last button pressed. the code works great, but it's rather unevolved and longwinded. is there a better way to do this perhaps with a for next loop? thanks in advance! [code] Btn1 PIN RB.0 input PULLUP SCHMITT Btn2 PIN RB.1 input PULLUP SCHMITT Btn3 PIN RB.2 input PULLUP SCHMITT Btn4 PIN RB.3 input PULLUP SCHMITT Btn5 PIN RB.4 input PULLUP SCHMITT Btn6 PIN RB.5 input PULLUP SCHMITT SW1 PIN RB.6 input PULLUP SCHMITT LEDS PIN RC LED1 PIN RC.0 LED2 PIN RC.1 LED3 PIN RC.2 LED4 PIN RC.3 LED5 PIN RC.4 LED6 PIN RC.5 ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- open con 1 closed con 0 Baud CON "OT31250" ' for MIDI Channel CON 0 CtrChange CON $C0 | Channel ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- idx var byte tmpB1 VAR Byte ' for subs/funcs tmpB2 VAR Byte tmpW1 VAR Word ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Subroutine / Function Declarations ' ------------------------------------------------------------------------- WAIT_MS SUB 1, 2 ' replaces PAUSE TX_BYTE SUB 1 ' transmit a byte TX_STR SUB 2 ' transmit a string CNG_PGM SUB 1 ' Change MIDI program ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: idx = 4 high rc.idx Main: IF btn1 = closed THEN WAIT_MS 20 'debounce IF btn1 = closed THEN LOW LEDS CNG_PGM 0 WAIT_MS 1000 'allow for program load time HIGH LED1 DO WHILE btn1 = closed 'wait for button release WAIT_MS 20 LOOP ENDIF ENDIF IF btn2 = closed THEN WAIT_MS 20 'debounce IF btn2 = closed THEN LOW LEDS CNG_PGM 1 WAIT_MS 1000 'allow for program load time HIGH LED2 DO WHILE btn2 = closed 'wait for button release WAIT_MS 20 LOOP ENDIF ENDIF IF btn3 = closed THEN WAIT_MS 20 'debounce IF btn3 = closed THEN LOW LEDS CNG_PGM 2 WAIT_MS 1000 'allow for program load time HIGH LED3 DO WHILE btn3 = closed 'wait for button release WAIT_MS 20 LOOP ENDIF ENDIF IF btn4 = closed THEN WAIT_MS 20 'debounce IF btn4 = closed THEN LOW LEDS CNG_PGM 3 WAIT_MS 1000 'allow for program load time HIGH LED4 DO WHILE btn4 = closed 'wait for button release WAIT_MS 20 LOOP ENDIF ENDIF IF btn5 = closed THEN WAIT_MS 20 'debounce IF btn5 = closed THEN LOW LEDS CNG_PGM 4 WAIT_MS 1000 'allow for program load time HIGH LED5 DO WHILE btn5 = closed 'wait for button release WAIT_MS 20 LOOP ENDIF ENDIF IF btn6 = closed THEN WAIT_MS 20 'debounce IF btn6 = closed THEN LOW LEDS CNG_PGM 5 WAIT_MS 1000 'allow for program load time HIGH LED6 DO WHILE btn6 = closed 'wait for button release WAIT_MS 20 LOOP ENDIF ENDIF GOTO Main ' loop [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=394524 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2009 (http://www.dotNetBB.com)