Tagged: button control, endif, loops, motors
- This topic is empty.
-
AuthorPosts
-
June 6, 2015 at 10:45 pm #2318
bcraigie
ParticipantHere’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:
May 5, 2016 at 11:47 am #3469MintTech
ParticipantHi 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
May 9, 2016 at 6:37 am #3474Joseph
KeymasterHi,
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:
January 19, 2017 at 5:11 pm #4104Dave
ParticipantHi
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
January 19, 2017 at 5:15 pm #4105Dave
ParticipantAnother 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:
March 21, 2018 at 5:15 pm #6939CoraDias
ParticipantHi…i am a new user here. I also faced the same problem. I think you should try addingg a wait block just before the first wait block to allow time for the button to be released.
-
AuthorPosts
- You must be logged in to reply to this topic.