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

KNEX Motor Sensor for Basic Stamp Homework Board Microcontroller

2

Comments

  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 15:30
    Hal Albach wrote: »
    That is not what I see in your program.
    loop:
    LOW motor
    IF sensor1 = 1 THEN start
    GOTO loop
    
    start:
    HIGH motor
    IF sensor2 = 1 THEN loop
    GOTO start
    
    

    This is your original program. You have given us a picture of the track layout indicating that SENSOR 1 is on the lift hill downslope and SENSOR 2 is the buffer zone marker, opposite from what I conjectured earlier. Per the above quote from your recent post you state that Sensor 1 stops the motor and Sensor 2 starts the motor. But your program clearly states that when Sensor 1 = 1 then the motor is started, and when Sensor 2 = 1 then the motor is stopped.

    Given the information you provided this is how I see things running:

    1. Somehow the first train is caused to go down the lift hill downslope towards Sensor 1, upon passing the sensor the motor is turned on.
    2. Train 2 is pulled up and hopefully makes the crest before train 1 hits sensor 2 and turns off the motor.
    3. Train 1 passes Sensor 2 and turns off the motor and proceeds on to the bottom of the lift hill
    4. Train 2 passes Sensor 1 and the motor turns on and Train 1 is lifted up the hill.

    Is this what you intended?


    My mistake.

    The original code is correct. Switch sensor 1 and sensor 2 on the picture.

    Sorry.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 15:34
    Hal Albach wrote: »
    That is not what I see in your program.
    loop:
    LOW motor
    IF sensor1 = 1 THEN start
    GOTO loop
    
    start:
    HIGH motor
    IF sensor2 = 1 THEN loop
    GOTO start
    
    

    This is your original program. You have given us a picture of the track layout indicating that SENSOR 1 is on the lift hill downslope and SENSOR 2 is the buffer zone marker, opposite from what I conjectured earlier. Per the above quote from your recent post you state that Sensor 1 stops the motor and Sensor 2 starts the motor. But your program clearly states that when Sensor 1 = 1 then the motor is started, and when Sensor 2 = 1 then the motor is stopped.

    Given the information you provided this is how I see things running:

    1. Somehow the first train is caused to go down the lift hill downslope towards Sensor 1, upon passing the sensor the motor is turned on.
    2. Train 2 is pulled up and hopefully makes the crest before train 1 hits sensor 2 and turns off the motor.
    3. Train 1 passes Sensor 2 and turns off the motor and proceeds on to the bottom of the lift hill
    4. Train 2 passes Sensor 1 and the motor turns on and Train 1 is lifted up the hill.

    Is this what you intended?

    1. is correct.

    2. Train 2 is pulled up lift hill only after Train 1 hits Sensor 1.

    3. Train 2 lift hill downslope and hits Sensor 2 (Thus Motor OFF).

    Repeats over and over again.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 15:40
    Okay, based on your truth table here is the code that should work for your application, including adding an LED to P12 via a 220-470 ohm resistor. It will come on when the motor is on and go off when the motor is off. If your condition was based on a single value I would have used a SELECT...CASE statement. The following code isn't the most efficient, but it is structured to help you understand the correlation between my truth table and the logic needed to realize that via code. I hope this helps.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    sensor1   PIN   0
    sensor2   PIN   15
    motor     PIN   8
    led       pin   12
    
    
    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led
    
    
    DO
      IF sensor1 = 0 AND sensor2 = 0 THEN
        LOW motor
        LOW led
      ELSEIF sensor1 = 1 AND sensor2 = 0 THEN
        LOW motor
        LOW led
      ELSEIF sensor1 = 0 AND sensor2 = 1 THEN
        HIGH motor
        HIGH led
      ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
        HIGH motor
        HIGH led
      ENDIF
    LOOP
    


    Thank you.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-08 15:49
    Please let me know if the program works as expected.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 15:58
    Ok.

    I just need to buy a part to connect the homework board to the computer.

    I need to buy a female DB9 Serial connector.

    Can I buy from your website?

    I am using a HomeWork Board - Serial
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 16:17
    Here is your code. I revised it a little.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    sensor1 PIN 0
    sensor2 PIN 15
    motor PIN 8
    led pin 7


    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led


    DO
    IF sensor1 = 0 AND sensor2 = 0 THEN
    HIGH motor
    HIGH led
    ELSEIF sensor1 = 1 AND sensor2 = 0 THEN
    HIGH motor
    HIGH led
    ELSEIF sensor1 = 0 AND sensor2 = 1 THEN
    LOW motor
    LOW led
    ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
    HIGH motor
    HIGH led
    ENDIF
    LOOP




    Sensor 1 turns motor on.
    Sensor 2 turns motor off

    I want motor on by default.
    So, is this right?
    IF sensor1 = 0 AND sensor2 = 0 THEN
    HIGH motor
    HIGH led

    I want Sensor 1 to take importance over Sensor 2.
    So, is this right?
    ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
    HIGH motor
    HIGH led
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-08 16:21
    Here is what you need to connect a serial HomeWork Board to a PC if you need USB. http://www.parallax.com/product/28031

    As for the code. You should probably try the code as I posted it before making any modifications. The ON/OFF states were based on your feedback of the sensor states. If those are accurate it should run as you want. Making changes without understanding the code might cause unpredictable results. If the code doesn't work as it is supposed to then the truth table was not correctly filled out.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 16:21
    Okay, based on your truth table here is the code that should work for your application, including adding an LED to P12 via a 220-470 ohm resistor. It will come on when the motor is on and go off when the motor is off. If your condition was based on a single value I would have used a SELECT...CASE statement. The following code isn't the most efficient, but it is structured to help you understand the correlation between my truth table and the logic needed to realize that via code. I hope this helps.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    sensor1   PIN   0
    sensor2   PIN   15
    motor     PIN   8
    led       pin   12
    
    
    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led
    
    
    DO
      IF sensor1 = 0 AND sensor2 = 0 THEN
        LOW motor
        LOW led
      ELSEIF sensor1 = 1 AND sensor2 = 0 THEN
        LOW motor
        LOW led
      ELSEIF sensor1 = 0 AND sensor2 = 1 THEN
        HIGH motor
        HIGH led
      ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
        HIGH motor
        HIGH led
      ENDIF
    LOOP
    


    Should LED pin be upper-cased or lower-cased? pin or PIN
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-08 16:23
    The editor will uppercase keywords automatically when it is pasted into the editor.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 16:35
    I will let you know how the code works. First, I need the cord. So, I ordered it from your website and should be receiving it in the mail hopefully soon.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-08 19:18
    When creating and running the new code, does PBasic automatically overwrite the old code with the new code when the "run" button is clicked?
  • Hal AlbachHal Albach Posts: 747
    edited 2014-08-08 20:40
    Yes, the EEPROM is erased and the new program is stored.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-13 22:04
    Chris and Hal,

    The program works. But, I would like the motor to turn off after sensor 2 is hit and wait for the motor to turn back on when sensor 1 is hit. As it is now, motor only turns off when sensor 2 is hit. Goes right back on after train leaves sensor 2 and does not wait for train to reach sensor 1 before motor is right back on.


    - Kyle
  • KylePKingKylePKing Posts: 44
    edited 2014-08-13 22:20
    Here is the code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    sensor1 PIN 0
    sensor2 PIN 15
    motor PIN 8
    led PIN 7


    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led


    DO
    IF sensor1 = 0 AND sensor2 = 0 THEN
    HIGH motor
    HIGH led
    ELSEIF sensor1 = 1 AND sensor2 = 0 THEN
    HIGH motor
    HIGH led
    ELSEIF sensor1 = 0 AND sensor2 = 1 THEN
    LOW motor
    LOW led
    ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
    HIGH motor
    HIGH led
    ENDIF
    LOOP
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 08:00
    That logic diagram has all possible states for the sensors and motors. You would have to update that to adjust the program logically. I used that to write the program rather than try to guess based on descriptions of motors versus trains versus sensors. Can you update that table?
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 09:07
    =============================== = sensor 1 = sensor 2 = motor = = 0 = 0 = ? = = 1 = 0 = ? = = 0 = 1 = ? = = 1 = 1 = ? = ===============================

    motor
    on
    on
    off
    off
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 09:08
    at the risk of sounding old-school, you know what would really help, even over a diagram of the track and sensors? A truth table that shows the states of the sensors and the status of the motors for all states. With that most of us could write a new program in just a few minutes.
    ===============================
    = sensor 1 = sensor 2 = motor =
    =    0     =    0     =   ?   =
    =    1     =    0     =   ?   =
    =    0     =    1     =   ?   =
    =    1     =    1     =   ?   =
    ===============================
    

    just just replace the question marks with what the motor status (on/off) should be in relation to these sensor conditions and a single loop program could be written in a just a few lines to handle this.

    motor
    on
    on
    off
    off
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 09:13
    That logic diagram has all possible states for the sensors and motors. You would have to update that to adjust the program logically. I used that to write the program rather than try to guess based on descriptions of motors versus trains versus sensors. Can you update that table?

    Hi Chris,

    The motor turns off when sensor2 is hit. But,it does not remain off after IR beam is no long disrupted. Why? HOW do I fix this?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 09:36
    It looks like you've changed the code so it no longer matches the logic table. The following code matches your new criteria.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    
    sensor1   PIN   0
    sensor2   PIN   15
    motor     PIN   8
    led       PIN   12
    
    
    
    
    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led
    
    
    
    
    DO
      IF sensor1 = 0 AND sensor2 = 0 THEN
        high motor
        high led
      ELSEIF sensor1 = 1 AND sensor2 = 0 THEN
        high motor
        high led
      ELSEIF sensor1 = 0 AND sensor2 = 1 THEN
        low motor
        low led
      ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
        low motor
        low led
      ENDIF
    LOOP
    
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 09:59
    It looks like you've changed the code so it no longer matches the logic table. The following code matches your new criteria.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    
    sensor1   PIN   0
    sensor2   PIN   15
    motor     PIN   8
    led       PIN   12
    
    
    
    
    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led
    
    
    
    
    DO
      IF sensor1 = 0 AND sensor2 = 0 THEN
        high motor
        high led
      ELSEIF sensor1 = 1 AND sensor2 = 0 THEN
        high motor
        high led
      ELSEIF sensor1 = 0 AND sensor2 = 1 THEN
        low motor
        low led
      ELSEIF sensor1 = 1 AND sensor2 = 1 THEN
        low motor
        low led
      ENDIF
    LOOP
    


    But the issue arises because the motor stays on, stops only when sensor2 is interrupted, then stays on indefinitely. I need (after sensor2 is interrupted) motor to stay off for at least 30 seconds (time to get to sensor1 to initiate motor again). Motor does not stay off after sensor2 is interrupted. How do I make it stay off?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 10:46
    Unless I missed something this 30-second thing was not mentioned previously and your original code did not account for it either. That requirement now complicates the process because now you need a time reference in seconds to handle that and you don't have that. I can't help wonder if the logic table is wrong for what you want/need?
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 11:25
    Unless I missed something this 30-second thing was not mentioned previously and your original code did not account for it either. That requirement now complicates the process because now you need a time reference in seconds to handle that and you don't have that. I can't help wonder if the logic table is wrong for what you want/need?

    The 30 second time is just approximate. The time for the code is not necessary.


    I would like for train1 to hit sensor2 and turn motor off.


    Then,


    I would like train1 to hit sensor 1 and turn motor on.


    From the logic table, only on or off for the sensor when IR is hit.

    I want the motor off after sensor2 is hit. Please stay off.

    Then,

    I want motor on after sensor1 is hit. Please stay on.

    Then,

    I want motor off after sensor2 is hit.

    Then,

    I want motor on after sensor1 is hit.

    Repeat.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 11:38
    I'm sorry. I am having trouble understanding what you're trying to do. Each solution I come up with seems to generate new criteria making it so I cannot ever meet the goal.

    I look at things logically, but the control logic seems to keep changing. By default the table should work if the states are properly defined. Based on your answers the code does what you requested of the motor for each set of states of the pins. So I can only assume the answers to the table are wrong or I am misinterpreting your request.

    Can anyone else clarify?
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 11:45
    How do I run the code so that:

    sensor2 is hit and motor stays off until sensor1 is hit.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 12:51
    The problem is that your requirement defies logic in a sense. Your original code did that, I believe. But you wanted it to be on default, but given the sensor states you require that isn't possible. Again, I may be missing something with all the changes, however can't have it both ways. It can't be "ON" by default if the code says that it should be "OFF" when a sensor says so. I will have to look back through everything to see if I missed something.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-08-14 12:54
    It looks like the problem might be that in the DO LOOP all sensor conditions are being evaluated constantly, and not waiting in a particular state until a train has crossed a sensor. That's why in the modified program the motor shuts off only while Sensor #2 is interrupted, but as soon as the train passes the sensor the first part of the Do Loop is satisfied and the motor turns back on. I think what Kyle is looking for is that when train 1 passes sensor 2 (on the downhill) the motor is to shut off and remain off until the train eventually passes over sensor 1, which turns the motor back on. At that point train 2 is lifted and allowed to roll down to repeat train 1 procedure. It is train 1 passing sensor 1 that allows train 2 to proceed safely.
    I tend to think that the original program, as modified in Post 6 should work just fine, since it does wait for the changes. And it also addresses the original issue and starts the motor when power is first applied.
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:07
    Hal Albach wrote: »
    It looks like the problem might be that in the DO LOOP all sensor conditions are being evaluated constantly, and not waiting in a particular state until a train has crossed a sensor. That's why in the modified program the motor shuts off only while Sensor #2 is interrupted, but as soon as the train passes the sensor the first part of the Do Loop is satisfied and the motor turns back on. I think what Kyle is looking for is that when train 1 passes sensor 2 (on the downhill) the motor is to shut off and remain off until the train eventually passes over sensor 1, which turns the motor back on. At that point train 2 is lifted and allowed to roll down to repeat train 1 procedure. It is train 1 passing sensor 1 that allows train 2 to proceed safely.
    I tend to think that the original program, as modified in Post 6 should work just fine, since it does wait for the changes. And it also addresses the original issue and starts the motor when power is first applied.

    Yes, you and Chris are correct.

    I have tried to swtich the two commands from Post 6.

    However, PBasic says that "loop:" needs a DO preceeding.

    How do I modify the code?
  • KylePKingKylePKing Posts: 44
    edited 2014-08-14 13:10
    Here is my code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    sensor1 PIN 0
    sensor2 PIN 15
    motor PIN 8
    led pin 7

    INPUT sensor1
    INPUT sensor2
    OUTPUT motor
    OUTPUT led



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

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

    END
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 13:13
    LOOP is a reserved word in PBASIC Syntax 2.5 so you must either revert back to 2.0 which you were using before or change the word loop to something else.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 13:15
    Hal Albach wrote: »
    It looks like the problem might be that in the DO LOOP all sensor conditions are being evaluated constantly, and not waiting in a particular state until a train has crossed a sensor. That's why in the modified program the motor shuts off only while Sensor #2 is interrupted, but as soon as the train passes the sensor the first part of the Do Loop is satisfied and the motor turns back on. I think what Kyle is looking for is that when train 1 passes sensor 2 (on the downhill) the motor is to shut off and remain off until the train eventually passes over sensor 1, which turns the motor back on. At that point train 2 is lifted and allowed to roll down to repeat train 1 procedure. It is train 1 passing sensor 1 that allows train 2 to proceed safely.
    I tend to think that the original program, as modified in Post 6 should work just fine, since it does wait for the changes. And it also addresses the original issue and starts the motor when power is first applied.

    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.
    KylePKing wrote: »
    How do I run the code so that: sensor2 is hit and motor stays off until sensor1 is hit.

    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.
Sign In or Register to comment.