Using programming to affect physical objects in real life is so much fun! Even a simple project can bring a lot of satisfaction and end up useful.
Here's some comments/questions about your code:
Why don't you stop the motors inside the open/closeBlinds() functions? A function should be a complete idea that does what its name implies. What you currently have could be dangerous - if you forget to add a motor [motorB] = 0 statement after calling the function, the motor will just keep running, wasting some power in the best case scenario, damaging itself or other objects in the worst.
To overcome the problem that @taxation_is_slavery mentioned (the blinds opening and closing rapidly when the light intensity is right around 50, going slightly above and below regularly) you could set a higher threshold for when to close the blinds and a lower for when to open them. Somewhere in the range of 45-55 you should be comfortable with the blinds being either open or closed, so if you open them at 45, they won't close again until the light intensity changes quite a bit - it has to go all the way up to 55. They will then stay closed until, once again, the light intensity's change will be significant, down to 45.
Final comment: you don't need the else statements after checking the sensor value. The motor is already set to 0, so there's nothing that needs to be done if the threshold hasn't been passed.
Okay, it took me some time to understand this because I am fairly new to programming but I see what you're getting at.
This is definitely something to think about. You're saying having a threshold that is just like a thin line right?
Thank you for adding this comment. I will have to try this out!
Do you not need a tolerance when opening and closing. I thought this was required to prevent feathering around a threshold. For water pumps the logic might be:
if WL > threshold:
#turn on pump
else if WL < threshold - 0.2:
#turn off pump
Once the pump has turned on, the water level needs to drop significantly in order to prevent the pump oscillating between on and off.
I never thought of feathering around the threshold.
Thanks!
I am fairly new to programming so these kinds of ideas are stepping into new learning grounds for me, thank you for that!
It may save a bit of wear on the motors, it might take a bit of tweaking to find a value that works for you. Other options to prevent it moving all the time could be to take an average over a number of measurements (e.g. 5 min) to ensure that the blinds don't move each time a cloud passes overhead. I'm sure like most programming things you will tweak it as you go and change the goal posts!
I dusted off my old time capsule and managed to find the video demonstration of the Robot in action: http://www.gfycat.com/AdventurousGlisteningEuropeanfiresalamander
The reason it closes when my finger is close to the sensor is because the red light from the light sensor reflects back into the sensor making a brighter reading.
I will try to find the video demonstration.
I also used AutoDesk Inventor 3D to create a small frame and blinds that worked quite well with the lego: http://www.autodesk.com/products/inventor/overview
In each function whether the blinds open or close I have a time delay: "wait1Msec (1000);" and on top of that I have another delay right before the variable "i" changes. This will prevent chaos, but maybe I should make the motors gradually increase in speed rather than jumping straight to the required speed.
18 comments
1 u/chrzan 18 Jun 2015 22:49
Using programming to affect physical objects in real life is so much fun! Even a simple project can bring a lot of satisfaction and end up useful.
Here's some comments/questions about your code:
Why don't you stop the motors inside the open/closeBlinds() functions? A function should be a complete idea that does what its name implies. What you currently have could be dangerous - if you forget to add a
motor [motorB] = 0statement after calling the function, the motor will just keep running, wasting some power in the best case scenario, damaging itself or other objects in the worst.To overcome the problem that @taxation_is_slavery mentioned (the blinds opening and closing rapidly when the light intensity is right around 50, going slightly above and below regularly) you could set a higher threshold for when to close the blinds and a lower for when to open them. Somewhere in the range of 45-55 you should be comfortable with the blinds being either open or closed, so if you open them at 45, they won't close again until the light intensity changes quite a bit - it has to go all the way up to 55. They will then stay closed until, once again, the light intensity's change will be significant, down to 45.
Final comment: you don't need the else statements after checking the sensor value. The motor is already set to 0, so there's nothing that needs to be done if the threshold hasn't been passed.
0 u/SolidBrowser [OP] 19 Jun 2015 03:48
Okay, it took me some time to understand this because I am fairly new to programming but I see what you're getting at. This is definitely something to think about. You're saying having a threshold that is just like a thin line right? Thank you for adding this comment. I will have to try this out!
1 u/blind-science 19 Jun 2015 02:25
Do you not need a tolerance when opening and closing. I thought this was required to prevent feathering around a threshold. For water pumps the logic might be:
Once the pump has turned on, the water level needs to drop significantly in order to prevent the pump oscillating between on and off.
0 u/SolidBrowser [OP] 19 Jun 2015 03:51
I never thought of feathering around the threshold. Thanks! I am fairly new to programming so these kinds of ideas are stepping into new learning grounds for me, thank you for that!
0 u/blind-science 19 Jun 2015 03:59
It may save a bit of wear on the motors, it might take a bit of tweaking to find a value that works for you. Other options to prevent it moving all the time could be to take an average over a number of measurements (e.g. 5 min) to ensure that the blinds don't move each time a cloud passes overhead. I'm sure like most programming things you will tweak it as you go and change the goal posts!
Enjoy
0 u/SolidBrowser [OP] 19 Jun 2015 04:07
Oh damn yet another thought provoking idea. Thanks! I feel like nothing is perfect and every project can be tweaked if you think about it.
1 u/blind-science 19 Jun 2015 04:10
https://xkcd.com/1319/
0 u/discopig 18 Jun 2015 20:40
That's some surprisingly short and sweet code. I'd love to see a video of it in action.
0 u/SolidBrowser [OP] 19 Jun 2015 03:36
I will try and find the video demonstrating the Robot in action.
2 u/SolidBrowser [OP] 20 Jun 2015 20:00
I dusted off my old time capsule and managed to find the video demonstration of the Robot in action: http://www.gfycat.com/AdventurousGlisteningEuropeanfiresalamander The reason it closes when my finger is close to the sensor is because the red light from the light sensor reflects back into the sensor making a brighter reading.
0 u/seeker 18 Jun 2015 21:34
Very interesting, I haven't come across Robot C before. What hardware is it running on?
1 u/chrzan 18 Jun 2015 22:17
Not OP, but I used it with Lego Mindstorms in a high school robotics course. Looks like it's also used on VEX and CORTEX.
0 u/SolidBrowser [OP] 19 Jun 2015 03:35
The program is run on an NXT 1.0 Programmable Brick: https://shop.education.lego.com/legoed/en-US/catalog/product.jsp?productId=5003402&isSimpleSearch=false&ProductName=NXT+Base+Set&ProductLine=LEGO+MINDSTORMS+Education+NXT
I will try to find the video demonstration. I also used AutoDesk Inventor 3D to create a small frame and blinds that worked quite well with the lego: http://www.autodesk.com/products/inventor/overview
0 u/taxation_is_slavery 18 Jun 2015 22:18
A cloudy day will cause problems for it though as it quickly turns off an on. You should have a minimum delay and/or another threshold.
1 u/SolidBrowser [OP] 19 Jun 2015 03:44
In each function whether the blinds open or close I have a time delay: "wait1Msec (1000);" and on top of that I have another delay right before the variable "i" changes. This will prevent chaos, but maybe I should make the motors gradually increase in speed rather than jumping straight to the required speed.
0 u/SolidBrowser [OP] 20 Jun 2015 20:01
Here is the Robot in action: http://www.gfycat.com/AdventurousGlisteningEuropeanfiresalamander