Shop OBEX P1 Docs P2 Docs Learn Events
Newbie desperately needs help to read 3 inputs to meet 7 cases — Parallax Forums

Newbie desperately needs help to read 3 inputs to meet 7 cases

NauketecNauketec Posts: 51
edited 2008-08-23 17:51 in General Discussion
I am using 1 BS2P as a main processor to ping room with an SRF10. If it meets 1 of 7 certain conditions it outputs it via 3 outputs (pins 5 - 6 - 7 ) to a second BS2P that controls an LED matrix I need code that would read the high / low states of the LED micro on pins 5 - 6 - 7 to then instruct the program to goto mode1, mode2, mode3, mode4, mode5, mode6, and mode7. If you can can help me with some detailed code that uses cases or branches or whatever It would make my week/month.

Thanks

Daniel

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-07-29 16:40
    Hi Daniel, take a look at the DIR and OUT instructions in Memory and Variables in the PBasic help file. These instructions allow you to monitor and control all or some of the Stamp I/O register with one instruction. In your instance you will be concerned with monitoring OUTB which are P4 P5 P6 and P7. In your final code , because P4 is not of interest you can use a mask to filter only the information you require. Your code may look similar to the following

    Led_output· VAR Nib
    Led_output=OUTB>>1 & 7·· 'shift the 4 bits·right 1 place and filter the lower 3 bits (P5 6 & 7)
    ON Led_output GOSUB mode_0,mode_1,mode_2.........etc

    Jeff T.


    Post Edited (Unsoundcode) : 7/29/2008 5:54:50 PM GMT
  • skylightskylight Posts: 1,915
    edited 2008-07-30 11:56
    Seems like the CASE statement would be ideal in this case(excuse the pun tongue.gif )
  • NauketecNauketec Posts: 51
    edited 2008-08-01 05:17
    Hey Jeff

    Thanks for the help but if you could just give me a little more of the layout I think I could pick it up more quickly.

    I need to read P0 P1 and P2 of the MAINIO to generate a value that can then either put into a series of CASE conditions or IF THEN ELSE statements that will in turn jump the program to:

    LED_MODE1
    LED_MODE2
    LED_MODE3
    LED_MODE4
    LED_MODE5
    LED_MODE6
    LED_MODE7

    There must also be something in these GOTOs or GOSUBs that will scan P0,P1, and P2 every so often to see if the value has changed so that the program can jump to the correct LED_MODE

    Thanks for your help it is really helping me to get my art piece done and to learn more

    Daniel
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-01 05:29
    Jeff basically gave you the answer. If you're using P0-P2, the equivalent expression would be "OUTA & 7" This would be the value found on I/O pins 0, 1, and 2 expressed as an integer from 0 to 7. You could use this in an ON ... GOTO like "ON OUTA & 7 GOTO LED_MODE1,...,LED_MODE7".

    If you just want to react to changes, you'll need a variable (declared as a nibble) to hold the previous value like this subroutine:
    check_pins:
      new_value = outa & 7
      if new_value = old_value then return
      old_value = new_value
      on new_value goto no_mode,led_mode1,led_mode2,...,led_mode7
    no_mode:
      return
    


    Each LED_MODEx routine would end with a RETURN.
  • NauketecNauketec Posts: 51
    edited 2008-08-17 03:44
    Hey Mike and Jeff

    Thanks for the help!

    I tried stiching together your suggestions but it does not seem to work for me.
    I think I may have left out something that might be a no-brainer to you guys but to a Newbie like me it might get past me.·I have attached·my working code with·a solution that·I can understand but I am sure that your solutions are far more elegant, faster, and shorter code.· I appreciate your help I am racing to a deadline and my friend that was assisting me with writing the code is not available, so I am on my own to learn a lot in very little time.

    The code has very clear notes at the heading·on where I am stuck.
    • I need to read the 4·DIP switches·and then go to run 1 of 15 program (prg 0 - prg15) selections
    • I need to get the single SRF10 sonar to work (you may not have experience with this)
    • I need to write code that will send 1 of 7 module selections out of 3 ports (P1 P2 P3)to be received by 3 ports (P0 P1 P2) on a second BS2P40

    Thanks again

    Daniel




    Post Edited (Nauketec) : 8/25/2008 5:29:51 AM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-08-18 15:11
    Hi Daniel, your pin assignments have changed from the original post. If it were possible to arrange the pin assignments as follows it would simplify things a little.

    ·'P0 OUT TO LED BS2P40 TO SEND WHICH SELECTED MODULE 0 - 7 TO RUN
    ·'P1 OUT TO LED BS2P40 TO SEND WHICH SELECTED MODULE 0 - 7 TO RUN
    ·'P2 OUT TO LED BS2P40 TO SEND WHICH SELECTED MODULE 0 - 7 TO RUN
    ·'P3 NOT USED

    ·'P10 NOT USED
    ·'P11 NOT USED

    ·'P12 DIP SWITCH #1 (BIN·1 COLUMN)PULLED HIGH WITH 4.7K RESISTOR
    ·'P13 DIP SWITCH #2 (BIN·2 COLUMN)PULLED HIGH WITH 4.7K RESISTOR
    ·'P14 DIP SWITCH #3 (BIN·4 COLUMN)PULLED HIGH WITH 4.7K RESISTOR
    ·'P15 DIP SWITCH #4 (BIN·8 COLUMN)PULLED HIGH WITH 4.7K RESISTOR

    Jeff T.
  • NauketecNauketec Posts: 51
    edited 2008-08-18 17:22
    Hey Jeff,

    Can't change pins - the board was made ( I now know that it should have been designed to make the code· easier but that is what learning is all about.· My problem is I have a deadline and must do the best I can to get the job done.· Thanks for any help you can offer.· I have been reading as much as I can buiut it is a lot to learn in a short time.

    What I am trying to do is have the first of two BS2P40 micros· scan the four dips (only once at power-up)on P10 P11 P12 P13 and from them set which of 15 programs to run.·I do this·in the· "Initialize:" section.

    The "Main:" section then·uses a Davantech·sonar SRF10 SCL on P4 and SDA on P5 to detect:
    • If there is a subject or not
    • If the subject is in range up to·5 feet
    • If the subject is between·5 feet and 2 feet
    • If the subject is between 5 feet and 2 feet for X amount of time
    • If the subject is under 2 feet

    This information is used for a series of IF THEN GOTOs or CASES to set

    P1 P2 P3 to values that send the information to a second BS240

    The information on the second (led control) micro is read on P0 P1 P2 on that chip

    where it runs 7 different LED pattern modes (mode0, mode1, mode2 .... mode7)

    It runs part of the led pattern then checks P0 P1 P2 for changes and returns to the same mode or updates to a new one.

    That is it but for a newbie its enough!· Thanks again for all your help!· My programmer is unavailable. I tried to hire a consultant but none were available on short notice.· So now I am staying up till 3:00am and greatly appreciating any help I can get here.

    Daniel


    Post Edited (Nauketec) : 8/25/2008 5:30:41 AM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-08-19 01:53
    Okay, if you deal with the dip switch first you can greatly simplify what you have. As the dip switch is assigned P10 to P13 those 4 inputs reside in the high byte of the INS register that is INH, they are also assigned where the highest bit is given the low value of the dip switch and the lowest·bit is given the high switch value.

    One solution would be to extract the 4 bits from INH and reverse them to give a number between 0 and 15, then use the ON..GOSUB instruction to jump to the "mode" routine. That would do away with all the IF..THENS you had in version 1.

    eg.

    Dip_Switch VAR Byte

    Dip_Switch=(INH REV· 8 >>2) & 15·· 'reverse the high byte of the INS register , shift the result right 2 places then AND that result with 15 to derive a number between 0 and 15

    ON Dip_Switch GOSUB mode_0,mode_1,mode_2...........etc

    Jeff T.
  • NauketecNauketec Posts: 51
    edited 2008-08-19 02:12
    Hey Jeff,

    The confusion is do you identify an input / output by pin number or by port number I was using P0 to P15 not pin numbers.

    for my applications on the first micro I need to read DIP switches on pins 15,16,17,18 (also known as ports P10,P11,P12,P13
    and set up 1-15 cases or if then gotos I also then need to send 1 to 7 conditions out pins 6,7,8 (also known as P1,P2,P3)

    on the second micro I need to read the data from the first by reading pins 5,6,7 (also know as P0,P1,P2)

    If you could help me to setup a way for those two apps to read them in bin or dec it would be a great help
    You can look at the code I included to see the long way I did it.

    Thanks

    Daniel
  • NauketecNauketec Posts: 51
    edited 2008-08-19 02:17
    Jeff I did not see your post when I sent mine.

    Thanks for the help

    the on Dip_switch Gosub mode1..... is what I am not so clear

    Will your code generate a number that I read and then for each dip switch position write on dipswitch = 0 gosub mode 0?

    Daniel
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-08-19 05:05
    Hi Daniel, the line "Dip_Switch=(INH REV· 8 >>2) & 15" reads the current state of the INH register manipulates the bits in such a way that the result will be a value between 0 and 15 dependent on the state of inputs P10 through P13 on gosub then takes that value (the dip switch value) and jumps to a routine that is dependent on that value. Look at the IDE help file and study the section on PBasic operators for manipulating bits nibs bytes and words, also look at the ON..GOSUB examples. Try using the code on its own adding a DEBUG instruction to read and verify the results.

    Jeff T.



    ·
  • NauketecNauketec Posts: 51
    edited 2008-08-23 17:51
    To all who have rendered me help on this task:

    Thanks a lot !· Your help has not only resolved the issue but has given me a great learning opportunity as well.

    I have moved my next task over to the basic stamp forum· please meet me there with your continued good will

    Thanks again

    Daniel

    end thread
Sign In or Register to comment.