Shop OBEX P1 Docs P2 Docs Learn Events
Basic I/O code — Parallax Forums

Basic I/O code

MGMG Posts: 9
edited 2006-10-16 18:01 in BASIC Stamp
I just started reading the Stamp syntax reference and I haven't got my board and chip as yet to debug/test my code.

Basically what i'm trying to do is to have the chip output a "W" (or the ASCII for W) whenever it receives a high input signal (or '1') from an external device. Here's what i have so far:

' {$STAMP BS2}
' {$PBASIC 2.5}

tmill PIN 0
PCout PIN 15

INPUT tmill ' set treadmill signal to input pin 0
OUTPUT PCout ' set PCout signal to pin 15
letterW CON "W"

#IF tmill = 1
#THEN PCout = letterW
#ENDIF

Will this work?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-16 17:20
    A couple of things ...
    1) The "#" statements are all conditional compilation statements. They don't make the Stamp do anything. They're mostly used to tell the compiler to include some statement and exclude others to help with making the same source program work on several different Stamp models. Look at the examples in the manual.

    2) Even if you left off the "#"s, your program does check pin 0 for a high level, but it does it only once, when the program starts. After the IF/ENDIF, your program (and the Stamp) will stop. That's always true at the end of a program.

    3) When you write "PCout = letterW", that means to take the binary value of letterW which is %01010111 and copy it to pin 15. Since pin 15 is a single bit, the copy (assignment) operation uses only the least significant bit (a one). I don't think that's what you want.

    4) I'm guessing, but you probably want to connect the stamp to your PC and signal from the Stamp to the PC using a serial COM port on the PC. There are two things you have to deal with:
    a) The PC uses a signalliing convention called RS232 which uses a negative or zero voltage to indicate a one and a positive voltage, possibly as high as 12V, to indicate a zero. This will rapidly destroy the Stamp that it's connected to, at least that particular I/O pin.
    b) A serial COM port uses a particular sequence of ones and zeros to send/receive whole bytes each of which is considered to be a character.
    The Basic Stamp Manual discusses both issues in the section describing the SERIN and SEROUT statements. Read about those and, if you have further questions, feel free to ask. You might also read about the DEBUGIN and DEBUG statements. These work like SERIN and SEROUT, but use the built-in RS232 to logic level converter that the Stamp uses for programming and debugging.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-10-16 17:29
    MG,
    ·
    ·· No that won’t work.· You’re using # signs in front of the commands and there’s no loop so the program would just end anyway.· Try the following…This will keep sending the “W” every pass through the loop as long as the input is still high.· If you need the code to wait until the line goes low before sending anymore that is a quick adjustment.· Note: you didn't specify a baud rate and the PIN declaration suggests you meant to use the programming port (16), but P15 will just use the highest I/O pin.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    tmill   PIN   0
    PCout   PIN   15
    Baud    CON   84
    Do
      IF tmill THEN
        SEROUT PCout, Baud, [noparse][[/noparse]"W"]
      ENDIF
    loop
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • MGMG Posts: 9
    edited 2006-10-16 18:01
    Thanks for the pointers Mike and Chris!
Sign In or Register to comment.