Shop OBEX P1 Docs P2 Docs Learn Events
Need Help with simple programming (beginner) - Page 2 — Parallax Forums

Need Help with simple programming (beginner)

2»

Comments

  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-03 15:18
    lol my schematic is pretty much Smile, but everything is connected correctly as i tested it with my other program that didnt hold the order of the lights, basically i have 330 ohm resistors for each led and 10ks for each input.
  • SapphireSapphire Posts: 496
    edited 2013-05-03 16:33
    Good. I think the problem is the program never gets to the BRANCH statement if the input is constantly on.

    Try this: change
    IF IN8 = 0 THEN intersectionA
    

    to
    IF IN8 = 0 THEN GOSUB intersectionA
    

    and add GOSUB to the other 3 as well.


    Then in intersectionA change
    IF pressedA = 1 THEN start
    

    to
    IF pressedA = 1 THEN RETURN
    

    and change
    GOTO start
    

    to
    RETURN
    

    and do the same for the other 3.

    This makes intersectionX a subroutine, and after putting the input in the list, it will return to check the others. When all 4 are done, the BRANCH should run as intended.
  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-03 20:26
    well that did seem to fix the problem it looks like, i havnt tested every route yet because I have to go, but I will later when I get back, if this is a complete fix then you are my hero :) thanks for all the help!
    I love programming, the only problem is I havnt never had a class for it or anyone actually teach me a specific language fully so i dont know much, but doing things like this and getting examples is the best way to learn and it will help in my future engineering job so thanks.
  • SapphireSapphire Posts: 496
    edited 2013-05-03 20:56
    You're welcome! Glad I could help.

    Engineering is a great career. And knowing some programming will come in very handy in that field.
  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-06 12:23
    ok well i didnt test it out completely until now, it doesnt glitch up like it used to but theres still a slight problem with order, lets say i press down the input lever connected to output 3 and hold it, then simultaneously press down the input connected to output 2 and also output 1 right after eachother and hold them as well. the program will have to output 3's light and i release it, it waits 3 seconds, and should go to the second one in order that i have pressed down (or am pressing down since i have both 2 and 1 pushed down at this moment) but it circles to number 1 and that light goes on instead of 2 even though i pushed 2 second. this i believe is because after i push the first one and hold it, no other action is taking place in the prgram until i release it, once i release it, it reads from top down and sees that number 1 is pushed before number 2, this was my original problem of it not holding order
  • SapphireSapphire Posts: 496
    edited 2013-05-06 14:15
    I thought about that over the weekend, but since you handn't mentioned it, I wasn't sure you were testing for it.

    The reason that happens is the PAUSE 3000 statements. During that 3 second time, the inputs aren't scanned. So if you press a lever or two, the program doesn't see that until the end of the 3 second time, and then it orders them in the list based on the sequence in your code.

    To fix that, first add a label before the the "IF IN8 = 0 THEN GOSUB intersectionA" statement, say start0:
    start0:
    IF IN8 = 0 THEN GOSUB intersectionA
    IF IN7 = 0 THEN GOSUB intersectionB
    IF IN6 = 0 THEN GOSUB intersectionC
    IF IN5 = 0 THEN GOSUB intersectionD
    


    Then remove all 4 of the PAUSE 3000 statements. Immediately following each PAUSE 3000 statement is an IF statement that jumps back to the start of that state. Change them to jump to the new start0 label, like this:
    IF IN8=0 THEN start0
    
    In four places, change the stX to start0, leave the rest alone. Each jump goes back to the new label in the start routine.

    Now the program will scan the inputs all the time, and the light will stay on until the lever is released.
  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-07 02:10
    ok ill do that, tomorrow is when its all due so i wont have much time for anything else.. if that doesnt fix the problem is there anything else that might be the problem? i cant test it out right now, so im just preparing for the worst and i might be able to make a few last adjustments to the program hah..thanks for all your help though, it really is great to have help.. this is the final code, i just nulled out the pauses

    list VAR Byte(4)
    pressedA VAR Byte
    pressedB VAR Byte
    pressedC VAR Byte
    pressedD VAR Byte
    index VAR Byte


    INPUT 5
    INPUT 6
    INPUT 7
    INPUT 8
    OUTPUT 0
    OUTPUT 1
    OUTPUT 2
    OUTPUT 3
    START:
    LOW 0
    LOW 1
    LOW 2
    LOW 3
    start0:
    IF IN8 = 0 THEN GOSUB intersectionA
    IF IN7 = 0 THEN GOSUB intersectionB
    IF IN6 = 0 THEN GOSUB intersectionC
    IF IN5 = 0 THEN GOSUB intersectionD
    BRANCH list, [start,st0,st1,st2,st3]
    GOTO start


    st0:
    HIGH 0
    'PAUSE 3000
    IF IN8=0 THEN start0
    pressedA=0 ' clear the flag
    list(0)=list(1) ' shift the list
    list(1)=list(2)
    list(2)=list(3)
    list(3)=0 ' clear the last entry
    index = index-1 ' decrement the index
    GOTO start


    st1:
    HIGH 1
    'PAUSE 3000
    IF IN7=0 THEN start0
    pressedB=0
    list(0)=list(1)
    list(1)=list(2)
    list(2)=list(3)
    list(3)=0
    index = index-1
    GOTO start


    st2:
    HIGH 2
    'PAUSE 3000
    IF IN6 = 0 THEN start0
    pressedC=0
    list(0)=list(1)
    list(1)=list(2)
    list(2)=list(3)
    list(3)=0
    index = index-1


    GOTO start
    st3:
    HIGH 3
    'PAUSE 3000
    IF IN5 = 0 THEN start0
    pressedD=0
    list(0)=list(1)
    list(1)=list(2)
    list(2)=list(3)
    list(3)=0
    index = index-1
    GOTO start


    intersectionA:
    IF pressedA = 1 THEN RETURN
    list(index)= 1
    index = index + 1
    pressedA = 1
    RETURN


    intersectionB:
    IF pressedB = 1 THEN RETURN
    list(index) = 2
    index = index + 1
    pressedB = 2
    RETURN




    intersectionC:
    IF pressedC = 1 THEN RETURN
    list(index) = 3
    index = index + 1
    pressedC = 3
    RETURN




    intersectionD:
    IF pressedD = 1 THEN RETURN
    list(index) = 4
    index = index + 1
    pressedD = 4
    RETURN
  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-07 11:44
    ok well i got to school and tested the prgram, but what happened is only one light came on (whischever one I pressed) and then nothing happened after that. The prgram then wouldnt work anymore after the first light came on.. so im not sure what happens
  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-07 12:18
    nvm i tested it with the correct wiring lol.. nd it works fine but stull doesnt hold order.. idk what to do anymore im not presenting it because its not complete id rather just keep working on it..
  • SapphireSapphire Posts: 496
    edited 2013-05-07 14:23
    I see one problem in the program. In each of the intersectionX routines, you check if pressedX = 1 and RETURN if so. But in all but intersectionA, you don't set pressedX to 1.

    For example, in intersectionB, you set pressedB = 2 but test for pressedB = 1. This won't work.

    Try changing all of them to pressedX = 1 where you set it to match what you test for. Only the list(index) must be 1, 2, 3 or 4 so we know what is in the list.

    PressedX is just a flag, 0 or 1, telling us if that intersection is already in the list.


    In your test, did you release the lever after the light came on? The way the program is structured, the light will stay on until the lever is released, then it should go to the next lever that was pressed. After all levers are released, all the lights should go out.
  • CelestialDemonCelestialDemon Posts: 20
    edited 2013-05-07 17:47
    yah ill try that.. and yes I make it resemble a 4 way intersection as if in real life. 4 cars come up to it, all levers get pressed in an order and I release them in the order that I pressed them to see if the lights

    correlate with who has the "right away" as it should be based on who got there first aka which lever I pushed first. so idk, i feel like its possible, but once i push a light. say i hold it down for 5 seconds, while

    that light is being pushed does the program actually run past that loop? its acting as if it doesnt since it cant see which ones are 2nd 3rd and 4th because its stuck on keeping that light on? Ive heard that

    basic stamp cant do two things at once, i wonder if there is something with that.
  • SapphireSapphire Posts: 496
    edited 2013-05-07 18:13
    The change we made by jumping to start0 makes the program keep looking at the inputs (levers) all the time, even when a light is on. Before that change, it would only look at the inputs after the PAUSE 3000, which was about once every 3 seconds. What you have now will keep track of the inputs, provided you make the change to all the pressedX = 1.

    There are a few thing you can do to verify that.

    1. Add a DEBUG statement that shows when you jump to start0 so you know the scan is happening.
    2. Add a DEBUG statement each time a pressedX is set to 1 to confirm that it was indeed set.
    3. Add a DEBUG statement that prints out the list( ) order so you can confirm the inputs were queued up.

    The Stamp can only do one thing at a time, but it can do them quickly, and this provides the ability to make it appear to do many things at once. Since all of the steps in your program are short and quick, it is actually doing a lot but you can't see it because the only indications you have are the lights and they change only when the lever is pressed and released.
Sign In or Register to comment.