Shop OBEX P1 Docs P2 Docs Learn Events
KNEX Motor Sensor for Basic Stamp Homework Board Microcontroller - Page 3 — Parallax Forums

KNEX Motor Sensor for Basic Stamp Homework Board Microcontroller

13»

Comments

  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:21
    How do I modify code from POST 6

    so that it works?
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:24
    Hal,

    That would be the case if you didn't account for both states of the sensor, but the logic table accounts for all states for both sensors. So unless there are exceptions, the on/off state should be accounted for in each case. You mentioned the code in post 6, which if I recall reverses the subroutines from the original code. However the moment a sensor is activated the code would do exactly as it did before the swap. It also ignores a sensor while in each loop. One loop ignores sensor1 while the other ignores sensor2. Because of this you can't be dependant on sensor2 for anything once you're in the second loop.



    With those conditions Hal's code should work. And that assumes you do wish to ignore the other sensor in each case as described above.

    Yes, i would like to ignore the other sensor in each case.

    But, I also want default ON for motor and led when plug and battery is pulled out of AC outlet and out of Homework Board, and put back in.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:33
    ' {$STAMP BS2}
    ' {$PBASIC 2.0}

    'Programmed in PBasic

    sensor1 VAR IN0
    sensor2 VAR IN15
    motor CON 8
    led CON 7

    INPUT 0
    INPUT 15
    OUTPUT 8
    OUTPUT 7

    start:
    HIGH motor
    HIGH led
    IF sensor2 = 1 THEN loop
    GOTO start

    loop:
    LOW motor
    LOW led
    IF sensor1 = 1 THEN start
    GOTO loop


    END




    Motor is default ON.

    But, as sensor2 is interrupted, LED dims a little, and POWER is half-On? Once sensor2 is not interrupted, normal operation ON.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-08-14 13:34
    Chris;
    The only purpose in reversing the two subroutines was to establish a "motor on" condition at startup. I do wholeheartedly agree that in any good program all possible sensor states need to be considered. However, it does appear that this particular application behaves more like a simple state machine. Perhaps within each state the status of both sensors could be accounted for in the event that the orderly flow of the trains is disrupted by a curious child or pet.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:38
    Chris and Hal,

    It finally works correctly.

    Here is the code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.0}


    'Programmed in PBasic

    sensor1 VAR IN0
    sensor2 VAR IN15
    motor CON 8
    led CON 7

    INPUT 0
    INPUT 15
    OUTPUT 8
    OUTPUT 7


    start:
    HIGH motor
    HIGH led
    IF sensor2 = 1 THEN loop
    GOTO start




    loop:
    LOW motor
    LOW led
    IF sensor1 = 1 THEN start
    GOTO loop






    END
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:40
    Thank you Hal and Chris for your help.

    I really appreciate all of the time that you two have spent helping me with this.

    Thank you very much.

    Project Complete!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 13:45
    KylePKing I am curious if you're still getting the "1/2 power" issue as that is a sign the code is jumping back and forth between both routines really fast, which is not the ideal solution.

    In that case Hal is correct that a state machine is in order. In a state machine you could set a flag until a condition was met on one sensor while maintaining your loop, however in all the descriptions I never got a clear indication of how that should work. So if it is working right that is great, however if it is jumping back and forth at some points there is a problem, it just looks like 1/2 power.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-08-14 13:46
    You need to modify Start: so that the jump to loop occurs when sensor 2 returns to a 0. Something like this:
    start:
    HIGH motor
    HIGH led
    IF sensor2 = 1 THEN start1
    GOTO start
    start1: 
    IF sensor2 = 0 then loop
    goto start1
    
    

    You might have to do something similar to the loop: routine.

    Also, you could time how long it takes the train to pass the sensor and insert an appropriate pause. That would prevent gaps in the train from pulsing the sensor.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:50
    Everything works fine!

    The 1/2 dim light is fixed.

    It did this because the two IR sensor pairs were not perfectly lined up. I corrected this.

    The code works!

    The motor default is on!

    The operation performs as desired!

    Thank you Hal and Chris.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-08-14 13:56
    Great to hear everything is working as planned. Perhaps you could show us a picture or two of the whole project, sounds like you put a lot of time into it. I would like to see what the whole thing looks like.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 14:06
    Here is a picture of the entire coaster.

    Kyle King Roller Coaster.jpg
    1024 x 1365 - 251K
  • Hal AlbachHal Albach Posts: 747
    edited 2014-08-14 14:13
    Thank you. Holy Cow! That thing is HUGE!!!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 14:40
    Very impressive! Okay, well glad you have working code for it now. :nerd:
Sign In or Register to comment.