Shop OBEX P1 Docs P2 Docs Learn Events
Read state if INPUT in loop — Parallax Forums

Read state if INPUT in loop

FalconFalcon Posts: 191
edited 2011-07-29 23:39 in BASIC Stamp
Hello,
I am working on a BS2e–based Home Monitor system to report the condition of 10 sensors. I have the code working and reporting to a PINK (see 7_26_3) but I will need to free up some EEPROM space to add the next phase which will send emails for critical condition changes, and for future expansion.

I am using the following code to set a flag only if the condition has changed. This block of code represents 1 of 10 sensors so it has to be repeated and takes up too much memory. Plus it makes it more time consuming to change the number of sensors.

How can I use a loop that reads a different INPUT pin (0-9)each time? On the second line I want Current(t) to equal the state of IN0 - 9 as it loops through 10 times. As written belöw it seems to set Current(t) = the INPUT pin number instead of the INPUT pin state.



FOR t = 1 TO 10
Current(t) = INS(t-1) '**** read Sensor 1 state
temp = Current(t) '**** load temp with current Sensor state
IF Saved(t) = temp THEN '**** if Sensor saved = Sensor current = no change set to false
Flag(t) = 0 '**** no change ensure flag is cleared
ELSE
Flag(t) = 1 '**** Sensor changed states , set flag to true
Saved(t) = temp '**** save current state for compare next time through
ENDIF
NEXT


See attachment 7_26_3 for code that works without loop.
See attachment 7_26_5 for code with loop.

As always, all comments are welcome.

falcon

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-07-26 10:08
    You can't use INS because that's actually a word, so you have an array of words. You would use IN0(t-1) since IN0 is a bit and therefore you have an array of bits starting at IN0.

    You could use INS as long as you treat it as a 16-bit word. In that case, you'd isolate the individual bits with something like (INS >> (t-1)) & 1.
  • FalconFalcon Posts: 191
    edited 2011-07-27 04:01
    Well. I don't completely understand the (INS >> (t-1)) & 1 statement, but it worked. I'l have to study the syntax guide a bitmore.

    Any other suggestions for reducing the size of the code?

    Thanks,

    falcon
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-07-29 14:12
    Any other suggestions for reducing the size of the code?

    You might not like this BUT if you took out all your DEBUG statement out then your EEPRON space would be at 65% that about all I can tell right now the quick look that I had of your code just comment out the DEBUG statements

    When I write code for myself I use allot of DEBUG statements to make sure all of my routines work and went I start running out of EEPRON space I just comment out the DEBUG statements out when I do not need them any more

    I hope this helps
  • FalconFalcon Posts: 191
    edited 2011-07-29 23:39
    When I write code for myself I use allot of DEBUG statements to make sure all of my routines work and went I start running out of EEPRON space I just comment out the DEBUG statements out when I do not need them any more

    I hope this helps

    That does help. Thanks.
    That's kind of how I'm using the DEBUG statements too. When my code hangs I insert the DEBUG statements at different locations to see where it stops.

    I read the documentation for the BS2e and realized it had much more EEPROM. It took me a few tries but I finally got some of the sub routines moved to Slots 1 - 7, and the RUN commands placed in the correct locations. The code is a bit slow to load but that is a minor inconvienience. I now have space to continue development, but I have to be sure to keep all of the associated files in the same location on my computer.

    Another question regarding code minimization: Each time a critical sensor is changed I want to send several commands to the PINK. Two to update the variable, one to send an email. Since they are all "aimed" at the PINK, can they all be on the same line of code but separated by commas?
Sign In or Register to comment.