Saturday, April 3, 2010

challenge #6

For this challenge, we had to create a program that can detect multiple edges in a single thread. This means that we had two switches connected to two separate lights, which we wanted to toggle every time we pressed the corresponding switch . At first, we thought this would be an easy program to construct. We thought that by just putting "waituntil" and "waituntil not" in our program to detect rising edges, the switches would toggle the lights. This worked, but only if you followed the order that the switches were placed on the program. We weren't able to choose which light we wanted to toggle at any given point because if we didn't follow the order on our program, the thread would just get stuck. This was very problematic, so we decided to find solution. Wemake two separate programs for each switch but we wouldn't be able to run these two programs simultaneously without using the "wand" tool on PicoBlocks. The "wand" tool allows for two different threads to run simultaneously. However, the "wand" tool allows to run only two programs that the same time. So we had to find a another solution that would work for however many programs.

In order to do this, we used the text language in PicoBlocks to create variables and conditionals without grabbing control.


With Professor Turbak, we used the text language to create variables and four new blocks (block edge 1, block edge 2, block test-edge 1, block test-edge 2), which can you see in the picture above. The block edge 1 and 2 detect if the switches have been pressed by checking the previous and current states. If the program sees that the previous state and current states are different, it indicates an edge and it toggles. The block test edge 1and 2 detect these edges in a forever loop and waits for switch bounces. When pressing the block test-edge1 and block test-edge 2 blocks, they each toggled a light.
Now that we saw the format on the text language, we combined these two actions into one block (block test-edge 12) This is the program that we came up with:


The program looks relatively simple, but it took us a bit of time to decipher the text language and to understand exactly what was going on.

This is a video of our program in use:

.



No comments:

Post a Comment