Saturday, April 20, 2013

Works-Like Model: Lights

On Tuesday, I tested how many LEDs we could light up with the cricket. At first, I found that it could light up up to 20 LEDs while still being pretty bright:


But then I ran into a problem. The on8 and on7 methods that Robbie wrote for us only work for one port at once, so we couldn't have port 7 and 8 on at the same time. To work around this, I made a block program that turns each port on for 2 ms at a time, switching back and forth. This means that they can both be on at the same time, but they will only be half as bright.
With both ports on the single LED connected to the yellow wires (port 7) is pretty bright:
 Here the green wires connect to port 8 and the yellow to port 7, so the bottom seven lights connect to port 8 and the top single light is port 7
But when only port 7 is running, it is REALLY bright:

I decided that 12 LED's is a good number to be bright while still having many lights in each group. On Wednesday, I asked Robbie about the ports not turning on at once, and he produced a magical solution (I say magical because changing the last '2' to a '4' in all the methods means very little to me).
Turns voltage high and low for ports 6, 7,  and 8
Excellently, we can now have multiple groups of light at full power at the same time.
I also wired the LED's into three groups of 12, as we want.

Next, I needed to have a way to change the brightness of the LED's. This can be done by having them only be on for a certain fraction of the time, cycling too quickly for the eye to see. I wanted an easy way to set the lights to brighter and dimmer levels, and I decided on levels 0-10, where the number indicates how many milliseconds out of ten it will be illuminated. I created blocks for each port that will go through one 10ms cycle of light and dark:
In the process, I learned very valuable things from Robbie about the way the Logo language works, for example the only mutable variables are the global ones, (which seem to be capped at 14 variables, problematically), and :num is only a number, not a variable within the method, so it cannot be set to another value. 

But I wanted all three lights to be on at once, at different levels. With this system, because it uses long waits, only one light can be cycling at once. Therefore, I made another block that will do the same thing, but with all three lights at once, by waiting 1ms ten times, and turning each group on or off at the appropriate moment. I ran into a few fencepost errors with levels 0 and 10 and was sad at the lack of >= (and comments), but came to a final version that worked:
Now, all three groups can be at at different levels.

Now the lights needed to be interactive. I connected a light sensor to each of ports 1, 2, and 3, so that each group of lights can have a corresponding sensor. 
I wrote a program that will proportionally control each group of lights, increasing the power to each group when the light to its sensor decreases. The lights will be completely off at an arbitrary sensor value "midlight," here, 300, or slightly dimmer than the ambient light in the room. 
Now, the lights can be brightened and dimmed by covering or shining light on the three sensors:



But it seems silly to have an arbitrary value involved. 
Now, the light at which the LED's will not turn on is set by two things. "Avglight" gives the average light the sensors are seeing when the program is started, i.e. the ambient light in the room. We want the lights to turn on to a certain level at the ambient light. This is taken care of by a new variable "offset" which is the desired light power at "avglight" times the gain. Finally, "midlight," the light at which the LEDs do not turn on, is avglight-offset. This works by making sure that the "error" at the ambient light level (aka "offset") sets the light to the level specified in "offset." Here that level is 2, so the lights will go to level 2 when the sensors are reading the ambient light level as measured when the device was turned on. So, whether the device is turned on in a bright or dim room, it will adjust its range to be appropriate to the surrounding light. 

No comments:

Post a Comment