Due at the beginning of class on Thursday Sept. 27

(image from clipart.guru)

Assignment

In basketball, it’s possible to score one, two, or three points at a time. You and your friends want an easy scorekeeping system which is designed to tally the number of points that your team has. “If the other team sees this awesome system,” you think, “they will be so intimidated by how clever we are at building things that they’ll become terrible at basketball.” It’s a foolproof plan.

Your clever scoring system will consist of four buttons (described below), three indicator LEDs, and `Serial` feedback that specifies the chip’s internal timer, the last type of basket made, and the current score. Buttons:

1. The one point button. When pushed, add one point to the score, announce
`millis = xxxxx, free throw scored, x points`
and light a green LED for two seconds.

2. The two point button. When pushed, add two points to the score, announce
`millis = xxxxx, field goal scored, x points`
and light a yellow LED for two seconds.

3. The three point button. When pushed, add three points to the score and announce
`millis = xxxxx, three pointer scored, x points`
and light a red LED for two seconds.

4. The reset button. When pushed, announce
`reset`
and change the score to zero.

An example interaction, to illustrate the intended outcome: the user pushes the “reset” button about two seconds after the Arduino turns on, and the `Serial` feedback reads:

```millis = 2492, reset, 0 points
```

Then, about 10 seconds after the Arduino turns on, the user pushes the three point button. The red LED lights for two seconds, and the `Serial` feedback now reads:

```millis = 2492, reset, 0 points
millis = 10284, three pointer scored, 3 points
```

Note that there is no `Serial` feedback until a button is pushed. If, about 20 seconds after the Arduino was turned on, the user were to then push the two point button: the yellow LED would illuminate for one second, and the `Serial` feedback would read:

```millis = 2492, reset, 0 points
millis = 10284, three pointer scored, 3 points
millis = 20625, field goal scored, 5 points
```

A final important note: each button’s LED is supposed to light for two seconds after that button was pushed—but during that time, it’s important that the board is still able to receive a new button push on that, or any different button. This means that you can’t simply use a `delay()` to keep that LED on—you’ve got to use the chip’s on-board timer, called `millis()`, to help you out.

Deliverables

All deliverables are due at the start of class on Thursday 9/27:

1. A working machine which functions as described above, to be demonstrated at the start of class on Thursday 9/27
2. A schematic drawing of the machine’s wiring (printed or drawn on an 8-1/2 x 11” sheet of lined or blank paper)
3. Code submission to rzachari@andrew.cmu.education; subject line: “60-223 hw-6”, attachment name: “andrewid-hw6.ino”

Graded schematics and code will be returned to students on Tuesday 10/2.

Collaboration

Limited collaboration with your colleagues is permitted. You must build your own circuit and write your own code, but you can talk with other students to get advice and reason through the problem together. You are not permitted to copy anybody else’s code (whether that person is in this class or not). You are permitted to use any internet/book/etc. resource you’d like for guidance, so long as you do not copy code verbatim from such a source.

Feedback

points assignment
3 inputs wired properly (and using `INPUT_PULLUP` configuration)
3 outputs wired properly (current-limiting resistors)
3 schematic (legible, clean, and valid)
5 serial feedback (working as described above, and formatted properly)
6 code written cleanly, clearly, and with appropriate commenting practice
20 total

`millis()`

The Arduino has a built-in timer that begins counting as soon as the board gets power. It counts up in milliseconds, which means the number gets pretty big, pretty quickly. This timer is not affected by a `delay()` statement or any other thing going on in your code—it’s a very reliable value.

To find out precisely how many milliseconds it’s been since the chip turned on, you simply run the built-in function `millis()`, which will return the answer. For instance, if you want to find out where `millis()` is at this moment, simply run

``````Serial.println(millis());
``````

and there you go, you’ve got the value displayed.

The power of `millis()` is that by referring to this active timer, you can make things happen on a schedule without using `delay()` statements. This is a big deal! It means that multiple tasks can run concurrently on their own schedules, without interfering with each other.

See the Blink without blocking section of Code bites for some implementation details.

Using `millis()` to run a one-second timer: the event-loop model

Here is code, which we developed together in class, which outlines in short form how to use `millis()` to time a task:

``````unsigned long timer = 0;

void setup() {}

void loop() {
// something I want to run every second
if (millis() - timer >= 1000){    // greater than or equal!
// this happens once per second
timer = millis();
}
}
``````