Shop OBEX P1 Docs P2 Docs Learn Events
How to combine two programs into one? Please help - Thank you — Parallax Forums

How to combine two programs into one? Please help - Thank you

guy20sanjoseguy20sanjose Posts: 13
edited 2011-10-05 21:12 in BASIC Stamp
Anyone know how to combine these two programs into one program so it can be download to the BS2e ?

Program 1:
' {$STAMP BS2e}

'
' P4 -> ULN2003.1, ULN2003.16 -> Phase 1 (Black)
' P5 -> ULN2003.2, ULN2003.15 -> Phase 2 (Orange)
' P6 -> ULN2003.3, ULN2003.14 -> Phase 3 (Brown)
' P7 -> ULN2003.4, ULN2003.13 -> Phase 4 (Yellow)
'
[ I/O Definitions ]

Phase VAR OUTB ' phase control outputs


'
[ Constants ]


'
[ Variables ]

idx VAR Byte ' loop counter
stpIdx VAR Nib ' step pointer
stpDelay VAR Byte ' delay for speed control


'
[ EEPROM Data ]

Steps DATA %0011, %0110, %1100, %1001


'
[ Initialization ]

Setup:
DIRB = %1111 ' make P4..P7 outputs
stpDelay = 10 ' set step delay


'
[ Program Code ]

Main:
FOR idx = 1 TO 1//4 ' one revolution
GOSUB Step_Fwd ' rotate clockwise
NEXT
PAUSE 1000 ' wait 1/2 second
FOR idx = 1 TO 1//4 ' one revolution
GOSUB Step_Fwd1 ' rotate counter-clockwise
NEXT
PAUSE 1000 ' wait 1/2 second
GOTO Main
END


'
[ Subroutines ]

' Turn stepper clockwise one full step (7.5 degrees)

Step_Fwd:
stpIdx = stpIdx + 1 // 4 ' point to next step
GOTO Do_Step


' Turn stepper counter-clockwise one full step (7.5 degrees)

Step_Fwd1:
stpIdx = stpIdx + 1 // 4 ' point to previous step
GOTO Do_Step


' Read new step data and output to pins

Do_Step:
READ (Steps + stpIdx), Phase ' output new phase data
PAUSE stpDelay ' pause between steps
RETURN

Program 2

' RelativeHumidityReading.bs2
' Displays relative humidity in the Debug Terminal or the Parallax Serial LCD.
LCD PIN 0 ' Serial output to LCD
time VAR Word
humidity VAR Word
LcdBaud CON 84 ' Baud rate of LCD
RHconstant CON 12169 ' Relative Humidity Constant * 10
LcdCls CON $0C ' Clear LCD (use PAUSE 5 after)
LcdCR CON $0D ' Move pos 0 of next line
LcdBLon CON $11 ' Backlight on
LcdBLoff CON $12 ' Backlight off
LcdOff CON $15 ' LCD off
LcdOn1 CON $16 ' LCD on; cursor off, blink off
LcdLine1 CON $80 ' Move to line 0, position 0
LcdLine2 CON $9A ' Move to line 1, position 5
HIGH Lcd ' Setup serial output pin
PAUSE 100
SEROUT Lcd, LcdBaud, [LcdOn1] ' Initialize LCD
PAUSE 250
SEROUT Lcd, LcdBaud, [LcdBLon] ' Turn Backlight on
PAUSE 5
SEROUT Lcd, LcdBaud, [LcdCls] ' Clear LCD
PAUSE 5
DO
HIGH 7
PAUSE 1
RCTIME 7, 1, time
time = time * 10
humidity = (time - RHconstant) / 24
' Debug Display:
DEBUG HOME, "Relative Humidity = ", DEC humidity, "%"
' LCD Display:
SEROUT Lcd, LcdBaud, [LcdLine1, "RelativeHumidity",
LcdLine2, DEC humidity, "%" ]
PAUSE 100

IF humidity < 40 THEN
HIGH 13
PAUSE 2000

IF humidity > 40 THEN
LOW 13
PAUSE 1000
ENDIF
ENDIF

LOOP

Thanks so much

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-10-04 19:57
    What is it you are trying to accomplish? You need to put all the variables at the top and make sure you don't have doubles then, you group the code to do what you want, removing any extra bits. The two codes don't seem to have anything in common.
  • bsnutbsnut Posts: 521
    edited 2011-10-05 02:17
    I agree with Franklin. What are you trying to do? From what I see, I think you are trying to control a stepper motor based on humidity.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-10-05 05:41
    guy20,
    Maybe you're not clear on how to run these from different banks?
    At long last (!), they put a link to that great N&V article along with the other resources on the BS2e page --
    http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol3/col/nv87.pdf
  • guy20sanjoseguy20sanjose Posts: 13
    edited 2011-10-05 07:05
    Franklin wrote: »
    What is it you are trying to accomplish? You need to put all the variables at the top and make sure you don't have doubles then, you group the code to do what you want, removing any extra bits. The two codes don't seem to have anything in common.

    Thanks guys,
    I’m trying to run a stepper, read a humid sensor and control a water pump from the reading of humid sensor. What codes I should use when I combining them?
    Thanks
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-10-05 09:15
  • guy20sanjoseguy20sanjose Posts: 13
    edited 2011-10-05 17:59

    Thanks so much, I don't know why the program below only run the Main- End , but not the DO LOOP ? Anybody has any idea?

    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    '
    ' P4 -> ULN2003.1, ULN2003.16 -> Phase 1 (Black)
    ' P5 -> ULN2003.2, ULN2003.15 -> Phase 2 (Orange)
    ' P6 -> ULN2003.3, ULN2003.14 -> Phase 3 (Brown)
    ' P7 -> ULN2003.4, ULN2003.13 -> Phase 4 (Yellow)
    '
    [ I/O Definitions ]

    Phase VAR OUTB ' phase control outputs

    LCD PIN 0 ' Serial output to LCD

    '
    [ Constants ]

    LcdBaud CON 84 ' Baud rate of LCD
    RHconstant CON 12169 ' Relative Humidity Constant * 10
    LcdCls CON $0C ' Clear LCD (use PAUSE 5 after)
    LcdCR CON $0D ' Move pos 0 of next line
    LcdBLon CON $11 ' Backlight on
    LcdBLoff CON $12 ' Backlight off
    LcdOff CON $15 ' LCD off
    LcdOn1 CON $16 ' LCD on; cursor off, blink off
    LcdLine1 CON $80 ' Move to line 0, position 0
    LcdLine2 CON $9A ' Move to line 1, position 5

    '
    [ Variables ]

    idx VAR Byte ' loop counter
    stpIdx VAR Nib ' step pointer
    stpDelay VAR Byte ' delay for speed control

    time VAR Word
    humidity VAR Word
    '
    [ EEPROM Data ]

    Steps DATA %0011, %0110, %1100, %1001


    '
    [ Initialization ]

    Setup:
    DIRB = %1111 ' make P4..P7 outputs
    stpDelay = 10 ' set step delay

    HIGH Lcd ' Setup serial output pin
    PAUSE 100
    SEROUT Lcd, LcdBaud, [LcdOn1] ' Initialize LCD
    PAUSE 250
    SEROUT Lcd, LcdBaud, [LcdBLon] ' Turn Backlight on
    PAUSE 5
    SEROUT Lcd, LcdBaud, [LcdCls] ' Clear LCD
    PAUSE 5

    '
    [ Program Code ]

    Main:
    FOR idx = 1 TO 1//4 ' one revolution
    GOSUB Step_Fwd ' rotate clockwise
    NEXT
    PAUSE 1000 ' wait 1/2 second
    FOR idx = 1 TO 1//4 ' one revolution
    GOSUB Step_Fwd1 ' rotate counter-clockwise
    NEXT
    PAUSE 1000 ' wait 1/2 second
    GOTO Main
    END

    DO
    HIGH 9
    PAUSE 1
    RCTIME 9, 1, time
    time = time * 10
    humidity = (time - RHconstant) / 24
    ' Debug Display:
    DEBUG HOME, "Relative Humidity = ", DEC humidity, "%"
    ' LCD Display:
    SEROUT Lcd, LcdBaud, [LcdLine1, "RelativeHumidity",
    LcdLine2, DEC humidity, "%" ]
    PAUSE 100

    IF humidity < 40 THEN
    HIGH 13
    PAUSE 2000

    IF humidity > 40 THEN
    LOW 13
    PAUSE 1000
    ENDIF
    ENDIF

    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2011-10-05 18:13
    The program is doing what you're telling it to do. The initialization stuff falls through to the Main section where it goes up to the GOTO, then back to Main again. There's no way it can get to the DO / LOOP portion
  • guy20sanjoseguy20sanjose Posts: 13
    edited 2011-10-05 21:12
    Mike Green wrote: »
    The program is doing what you're telling it to do. The initialization stuff falls through to the Main section where it goes up to the GOTO, then back to Main again. There's no way it can get to the DO / LOOP portion

    In this case, what should I do to combine these two programs? What LOOP I should try? Thanks
Sign In or Register to comment.