Home Forums Crumble General Use a button to turn motor on/off/change direction

This topic contains 4 replies, has 4 voices, and was last updated by  Dave 1 year ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2318

    bcraigie
    Participant

    Here’s a wee project I knocked up today.

    Connect an on/off switch between C and + pad (use the normally off position if using a changeover switch)

    connect an LDR between A and + pad

    connect a motor on output 1.

    Connect your battery.

    When you start the program, nothing should happen. Now hold the button on until the motor starts then let it go (1000 iterations of a loop, which is about 2.5 seconds on my controller).

    Now if you press the switch for a short press, it will reverse the direction of the motor. Repeat and it will reverse it again.

    Hold the LDR into the light and the motor gets faster. Put it in the dark and the motor goes slower.

    Hold the button until the motor stops…

    You can hold the button again to start the motor again etc.

    It doesn’t serve any real purpose, but the code can be used to see one way to detect a long press on a button versus a short press. Also there’s a bit of switch de-bouncing (ie, when you start the motor, then let go, you don’t want it to immediately check for a short press, so it waits 3 seconds).

    If you keep holding the button on, the motor will start then stop then start every few seconds until you let go. A follow-up exercise for the novice experimenter could be to modify the code to ignore the pressed button until it is released following a long press. You can’t just simply loop until the button goes off, because then the LDR won’t be controlling the motor speed. So it needs a little thought. 😉

    Have fun. 🙂

    Brian

    Attachments:
    You must be logged in to view attached files.
    #3469

    MintTech
    Participant

    Hi Brian,

    I’m trying to make a little crumble project that will start the script when when a button on InputA is pressed and stop the script when the same button is pressed. I’m getting tied in knots trying to get it to work. I’ve got a wait until A is High at the beginning and an if A is high again later in the script that stops the motors but I can’t figure out the looping situation to get it back to the beginning to wait for the button to be pressed again.

    Any help would be much appreciated or let me know how you would approach it?

    Thanks

    MintTech

    #3474

    Joseph
    Keymaster

    Hi,

    The reason for the problem might be that when the “wait A is HI” block at the end of the loop is triggered, the program returns to the first “wait A is HI” block, which is immediately passed over as the button is probably still pressed. You could try adding a wait block just before the first wait block to allow time for the button to be released. Or, you could add “wait A is LO” blocks after each “wait A is HI” blocks to wait for the button to be released.

    Hope this helps,

    Joseph

    Attachments:
    You must be logged in to view attached files.
    #4104

    Dave
    Participant

    Hi
    I have a similar one, which works a two conveyor belt lego kit. The button controls the direction of one of the motors with just a short tap. A longer tap switches both off.

    Cheers

    #4105

    Dave
    Participant

    Another crack with a screenshot.

    Single button controlling two motors and two sparkles. Short press reverses one motor / toggles back and forth. Longer press stops everything.

    A number of variations are possible with button counts.

    Attachments:
    You must be logged in to view attached files.
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.