Shop OBEX P1 Docs P2 Docs Learn Events
Please help new guy with some simple code? — Parallax Forums

Please help new guy with some simple code?

Hard TimesHard Times Posts: 7
edited 2005-06-16 14:10 in BASIC Stamp
Thanks in advance to all that try to get me going down the correct path. I have been trying for weeks to get a simple piece of code working right, and it's all to no avail. I have had no training on how to program, but I can mostly slug it out by the seat of my pants. Until now!··I won't bother to show you guys·the code I've come up with·that·won't work. Frustration is very frustrating! The BS2 control I'm after is very easy to describe:

·1.There is one single pole Switch that will be the only external input. Call it IN1.·(Off·= LOW and On =HIGH)
·2. There are only two outputs that need to change from LOW to HIGH and back. Call them Pin 2 and Pin 3. (LOW condition shall be the default.)
·3. When IN1 is LOW, Pin 2 and Pin 3 shall be LOW and stay there until IN1 goes HIGH (Switch to ON)
·4. When IN1 goes HIGH, Pin 2 shall go HIGH and Pin 3 shall stay LOW.
·5. (Now here comes the difficult part for me) A "timing" piece of code needs to monitor IN1. If IN1 goes·LOW again before 10 consecutive·seconds has elapsed, then revert to (3. above), reset the timer to zero, and wait until·(4. above)·to happen. Do this for·each IN1=High condition
·6.· Now, satisify the "if happened·"condition of·IN1·= HIGH for at least 10 consecutive·seconds·before it·went LOW
·7. For the next and all subsequent IN1= HIGH conditions (because 10 consecutive·seconds did elapse)· Pin 3 will go·HIGH and Pin 2 shall stay LOW
·8. Because 10 consecutive·seconds has and did occur for Pin 2·, Pin 3 = HIGH·shall be the only responce to IN1 = HIGH until power down of the whole circuit.

·I hope this explaination of my conditions make sense.·If not, then let's try this.· I need to have S1 control
·LED 1 each time S1 is thrown. If, however, LED 1 is ever on 10 consecutive seconds or longer, then S1·will only control LED 2. LED 1 will stay off until the end of time (or at least a reset/power down occurs)·because it lit once for those 10 consecutive seconds.

That sounds better perhaps. I know most of you guys think this is a very simple piece of code to get right.· One day I·might think so too! I'm wondering if the POLLIN command·will be the secret answer?
Thank You,
Hard Times (Old man learning new Tricks)




·

Comments

  • K de JongK de Jong Posts: 154
    edited 2004-10-01 19:19
    Hi Hard Times,

    You can use a PAUSE 10000 command, but meanwhile you will not be able to have the Stamp do something else.

    If you change the description of behaviour for the program a bit you should be able to meet both the desirable and the feasible smile.gif.

    You have to deal with single tasked microcontroller, the single tasking is limiting in this case.

    Regards,

    Klaus
  • Hard TimesHard Times Posts: 7
    edited 2004-10-01 19:42
    Klaus, this is what I've been fighting. The 10000 pause will not allow me to see a IN1 go LOW until the pause is over.· I really could get·OK results·by using a 1000 pause, then checking for a LOW at IN1, then back to a PAUSE 1000 etc,etc. But I can't figure out how to do the counting of the 10 1 sec·PAUSES. Perhaps I should add, therefore , that I don't need "continious" monitoring of IN1. Interogation of IN1·could be up to every 2 seconds and my application will still be adaquate for my solution. Does this change your opinion?

    Thanks.
  • Larry~Larry~ Posts: 242
    edited 2004-10-01 19:52
    something like this will check every 250Msec

    for i = 0 to 40
    pause 250
    if pin 1 = 1 then something
    next i
  • K de JongK de Jong Posts: 154
    edited 2004-10-01 20:25
    You can rephrase the following piece of your requirement lis:

    -1
    "If IN1 goes LOW again before 10 consecutive seconds has elapsed, then revert to (3. above), reset the timer to zero, and wait until (4. above) to happen. Do this for each IN1=High condition"

    To the following:

    -2
    " IF IN1=LOW at the end of the 10 second period then......"

    The meaning is the same but phrase 2 is much more easy to implement in PBasic.

    Regards,

    Klaus
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-10-01 21:06
    I guess my advice would involve adding another part to the mix...If you used a DS1302, you could write a loop to check the elapsed time while checking the input.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-10-01 22:08
    Here is one way to do it...

    timer var word
    LOW 2   ' make outputs
    LOW 3
    DO
       out2 = in1   ' p2 follows the input
       timer=timer+1 * in1  ' resets when in1=0
       PAUSE 10   ' 10 millisecond
    LOOP UNTIL timer>1000   ' ten seconds  
    DO      '  pin p2 stays high
    LOOP UNTIL in1=0  ' until p1 goes back low
    LOW 2    ' p2 low, now
    DO
       out3 = in1   ' pin p3 follows the input
    LOOP  ' forever
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Hard TimesHard Times Posts: 7
    edited 2004-10-01 23:06
    Tracy, WOW!!

    This is exactly what I was looking for.· You are cool.gif·DA MAN!!··I can tell you I was not even close with the stuff I was coming up with.· I don't think I would have ever come close to your simple and elegant code! This works as exactly as advertised.·

    Many, many honest and sincere Thank You's.

    Hard Times (Old Man who has learned a new Trick today!)
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-10-04 01:38
    Well, thanks, and let me return the compliment. You stated your problem clearly in detail and then again in an "executive summary". That is always the first step to asking for clear help on something, and also, I am sure others will agree, a great first step toward writing your own program. You will soon pick up the tools and tricks you need to get the program written in PBASIC.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ReneRene Posts: 6
    edited 2004-10-04 01:48
    Hi group,

    With an ADC0831, I can read the voltage from 0 to 5 volts, but I want
    to read the battery voltage of my· snowmobile by using a resistor as a
    voltage divisor to read at least up to 20 volts, so I want to get the decimal
    number ( 0 to 255 ) to separate the 20 volts and then putting some math ratio
    to to display the voltage onto a serial LCD i.e. 255 / 20 = 12 units per volts,
    but it does not work·so, how ca I do it,?


    Thank you
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2005-06-16 00:32
    I would also like to know how to this
    With an ADC0831, I can read the voltage from 0 to 5 volts, but I want
    to read the battery voltage of my snowmobile by using a resistor as a
    voltage divisor to read at least up to 20 volts, so I want to get the decimal
    number ( 0 to 255 ) to separate the 20 volts and then putting some math ratio
    to to display the voltage onto a serial LCD i.e. 255 / 20 = 12 units per volts,
    but it does not work so, how ca I do it,?


    samsam
  • kb2hapkb2hap Posts: 218
    edited 2005-06-16 01:00
    Lets see what you got so far. sounds good though. you would need at least 2 resistors for that to work. then its setting up some math to deal with the ratios between the voltages and the 255 steps. what is it doing/not doing?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    DTQ
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-06-16 14:10
    Hello,

    ·· Yes, two resistors are required, and it might be slightly easier on the math to adjust the range for 10 steps per volts, or .1 volts/step.· And by the way, you should start a new thread when posting your new questions.· Adding them to the end of someone else's unrelated thread might cause them to go un-noticed by many.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.