Skip to content

Commit

Permalink
day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepMartiElias committed Dec 15, 2023
1 parent ca25362 commit f606667
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ hide:
1. UP RIGHT DOWN LEFT

!!! example annotate "16. Let's play! (1)"
[Day 16](solutions/16/16.md)
1. Can I be the controller?

!!! warning annotate "17. How AI imagines me? (1)"
Expand Down
57 changes: 57 additions & 0 deletions docs/solutions/16/16.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Day sixteen:

Let's play a game! Yesterday you transformed your Barduino in a keyboard, now let's use it to play a game. Program yoou board to be able to use the arrow keys with th touchsensors (or use the code below) and play a game like [snake.io](snake.io) with it.

!!! danger "Arduino Tips"
Like yesterday, if you use our code, you have to change the *USB Mode: USB-OTG (Tiny USB)* setting under *Tools* in Arduino.
![arduino usb otg](../../images/OTGMode.png)
Using this mode, you have to use *Boot Mode* to program the board (Press *Boot 00* button while reseting). When programming the board may change the *Port* number, so to programmed back you may need to change the port.

## Possible solution!

### Arduino code

```c++
#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;

unsigned long values[] = {0,0,0,0};
int direction[] = {KEY_UP_ARROW,KEY_RIGHT_ARROW,KEY_DOWN_ARROW,KEY_LEFT_ARROW};

int dir = 4;

unsigned long threshold = 40000;

void setup() {
Keyboard.begin();
USB.begin();
}

void loop() {
unsigned long max = 0;
int max_i;
for (int i = 0; i < 4; i++) {
values[i] = touchRead(i + 4);
if (values[i] > max) {
max = values[i];
max_i = i;
}
}
//Serial.println("");
if (max>threshold && dir!=max_i) {
dir = max_i;
Keyboard.releaseAll();
Keyboard.press(direction[dir]);
} else if (max <= threshold) {
dir = 4;
Keyboard.releaseAll();
}
}
```

## Hero shot

<video controls autoplay loop style="display: block; margin: auto;">
<source src="../../../video/day16.mp4" type="video/mp4">
</video>
Binary file added docs/video/day16.mp4
Binary file not shown.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav:
- Day 13: solutions/13/13.md
- Day 14: solutions/14/14.md
- Day 15: solutions/15/15.md
- Day 16: solutions/16/16.md
- Solution template:
- template.md

Expand Down

0 comments on commit f606667

Please sign in to comment.