Homework 4: Keeping it real logical
due Wednesday, Feb. 14th ♥ at 9:30am
1) If you did not finish the “alarm system” exercise in class, first complete that.
(As a brief reminder: the task was to build a system with three digital inputs (buttons/switches). If any of the three of them, or any combination of them, are “open,” then an LED should be illuminated. If and only if all three are “closed,” the LED should be off.)
Some code hints, in case you don’t know where to start:
- The overall structure of your
loop()
should probably look something like:
- The AND logical operator is available to you in C; it is formed by using two ampersands,
&&
. For instance, to test if a variablea
isHIGH
AND variableb
isHIGH
, simply write anif()
statement like so:
- The OR logical operator is available to you in C as well; it is formed by using two pipe characters,
||
. (The pipe character is typedshift-backslash
, where the backslash is the key below the delete key on most keyboards, and looks like \.) To test if variablesc
ORd
areHIGH
, write:
- A final note about these logical operators: they can be combined as much as you want. Just use parentheses to group them logically. For instance:
2) Build a consensus-indicating voting system
You want to poll four people and ask them a variety of questions. Without divulging anybody’s vote, you want to know how broad of an opinion spread there is on the questions.
Each voter is given a button; they will push the button to indicate “yes,” or not push it to indicate “no.” (You can build your four buttons into the breadboard.)
There are three possible states all votes could be categorized into: 4 to 0, 3 to 1, or 2 to 2. We don’t care about the difference between (4 no’s and 0 yes’s) and (0 no’s and 4 yes’s), because we are only concerned about consensus or lack thereof.
Use four buttons and three indicator LEDs to build the consensus-indicating voting system:
- A green LED should light if and only if there is a unanimous opinion (regardless of whether it’s yes’s or no’s). Everyone agrees! Yay!
- A yellow LED should light if and only if there is a single “lone opinion” in the vote. Almost everyone agrees, but not quite. Nearly yay!
- A red LED should light if and only if the vote is split (two yes’s and two no’s). Such indecision!
See the code hints above if you’re having trouble getting started, see Joseph at his office hours on Tuesday, or email us for help. By now, the wiring should take you very little time, and you’ll just need to focus on getting the code right. Bring your complete system to class so we can test it out Wednesday!