2017-05-26 06:01:00 +02:00
# Karel the LED
2017-09-07 22:42:08 +02:00
## ~avatar avatar
2017-05-26 06:01:00 +02:00
Help Karel make LED art!
2017-09-07 22:42:08 +02:00
## ~
2017-05-26 06:01:00 +02:00
**Karel the LED** can't turn right, but he can make some great LED art!
![](/static/mb/projects/karel/hi.png "Hi")
2017-11-28 04:14:03 +01:00
The goal of this activity is to download the JavaScript code given below onto a @boardname @.
Then USE the program to introduce new students to the @boardname @.
Students will not do the coding this time. They will be the users who get familiar with the board.
2017-05-26 06:01:00 +02:00
2017-09-07 22:42:08 +02:00
## How to play
2017-05-26 06:01:00 +02:00
* ``A button`` **Turn Left**
2017-11-28 04:14:03 +01:00
>Does not draw anything just changes the direction Karel (the flashing led) is facing
2017-05-26 06:01:00 +02:00
* ``B button`` **Move Forward**
2017-11-28 04:14:03 +01:00
>Moves Karel forward one step and leaves the LED on behind him
2017-05-26 06:01:00 +02:00
* ``shake`` **Jump**
2017-11-28 04:14:03 +01:00
>Moves Karel forward one step without leaving the LED on behind him
2017-05-26 06:01:00 +02:00
* ``A+B button`` **Hide Karel**
2017-11-28 04:14:03 +01:00
>Shows or hides Karel (the flashing LED), to be used once your artwork is complete
2017-05-26 06:01:00 +02:00
* ``Reset button`` **Clear the board and restart**
2017-11-28 04:14:03 +01:00
>Restart the program and clear the board
2017-05-26 06:01:00 +02:00
2017-11-28 04:14:03 +01:00
**Note:** There is no way to erase, except by restarting.
2017-05-26 06:01:00 +02:00
2017-11-28 04:14:03 +01:00
## Try drawing some patterns
2017-05-26 06:01:00 +02:00
2017-11-28 04:14:03 +01:00
See if you can make each pattern below using **A** , **B** , and **shake** . Once you have completed a challenge press **A** and **B** at the same time to hide Karel.
For patterns that you design, decide which LEDs you want to turn on and then make that design with Karel.
2017-05-26 06:01:00 +02:00
2017-11-28 04:14:03 +01:00
### Spiral
2017-05-26 06:01:00 +02:00
![](/static/mb/projects/karel/spiral.png "Spiral")
2017-11-28 04:14:03 +01:00
### Right turn
2017-05-26 06:01:00 +02:00
![](/static/mb/projects/karel/right-turn.png "Right turn")
2017-11-28 04:14:03 +01:00
### Eyes
2017-05-26 06:01:00 +02:00
![](/static/mb/projects/karel/eyes.png "Eyes")
2017-11-28 04:14:03 +01:00
### Smile
2017-05-26 06:01:00 +02:00
![](/static/mb/projects/karel/smile.png "Smile")
2017-11-28 04:14:03 +01:00
### Check
2017-05-26 06:01:00 +02:00
![](/static/mb/projects/karel/check.png "Check")
2017-11-28 04:14:03 +01:00
### First letter of your name
2017-05-26 06:01:00 +02:00
Figure out how to make the first letter of your name with the LEDs.
2017-11-28 04:14:03 +01:00
```sim
basic.forever(() => {
basic.showAnimation(`
# # # . . # # # . . # # # . . # # # . .
. . . . . # . . . . # . . . . # . . # .
. . . . . # . . . . # . . . . # . . # .
. . . . . # . . . . # . . . . # . . # .
. . . . . # . . . . # # # . . # # # . .
`)
basic.pause(1000)
basic.clearScreen()
basic.pause(1000)
})
```
### Your design!
Make something fun!
```sim
basic.forever(() => {
basic.showAnimation(`
# . . . . # . . . . # . . . . # . . . . # . . . . # . . . .
. . . . . # . . . . # . . . . # . . . . # . . . # # . # # #
. . . . . # . . . . # . . . . # . . . . # . . . # # . # . #
. . . . . # . . . . # . . . . # . . . . # . . . # # . . . #
. . . . . # . . . . # # # # # # # # # # # # # # # # # # # #
`)
basic.pause(1000)
basic.clearScreen()
basic.pause(1000)
})
```
2017-05-26 06:01:00 +02:00
Thanks for playing with Karel the LED!
2017-11-28 04:14:03 +01:00
## Coding Karel the LED
Copy this code into the JavaScript editor and then download it to the board.
2017-05-26 06:01:00 +02:00
```typescript
/**
* Karel the LED
*/
basic.forever(() => {
if (board.isKarelActive) {
led.toggle(board.karelX, board.karelY)
basic.pause(500)
}
})
input.onButtonPressed(Button.A, () => {
board.pressedA();
updateLeds();
})
input.onButtonPressed(Button.B, () => {
board.pressedB();
updateLeds();
})
input.onGesture(Gesture.Shake, () => {
board.shake();
updateLeds();
})
input.onButtonPressed(Button.AB, () => {
board.pressedAB();
updateLeds();
})
function updateLeds() {
for (let j = 0; j < 5 ; j + + ) {
for (let k = 0; k < 5 ; k + + ) {
if (board.ledState[j][k]) {
led.plot(k, j);
} else {
led.unplot(k, j);
}
}
}
}
const board = new Board();
enum Direction {
UP = 0,
LEFT,
DOWN,
RIGHT
}
class Board {
public isKarelActive: boolean;
public karelX: number;
public karelY: number;
public ledState: Array < Array < boolean > >;
private karelDirection: Direction;
constructor() {
this.isKarelActive = true;
this.karelX = 2;
this.karelY = 2;
this.karelDirection = Direction.UP;
this.ledState =[];
for (let i = 0; i < 5 ; i + + ) {
this.ledState.push([false, false, false, false, false]);
}
}
pressedA() {
if (!this.isKarelActive) {
return;
}
this.karelDirection = (this.karelDirection + 1) % 4;
}
pressedB() {
if (!this.isKarelActive) {
return;
}
this.ledState[this.karelY][this.karelX] = true;
this.moveKarel()
}
shake() {
if (!this.isKarelActive) {
return;
}
this.moveKarel()
}
private moveKarel() {
if (!this.isKarelActive) {
return;
}
switch (this.karelDirection) {
case Direction.UP:
2017-11-28 04:14:03 +01:00
if (this.karelY > 0) {
this.karelY -= 1;
}
break;
2017-05-26 06:01:00 +02:00
case Direction.LEFT:
2017-11-28 04:14:03 +01:00
if (this.karelX > 0) {
this.karelX -= 1;
}
break;
2017-05-26 06:01:00 +02:00
case Direction.DOWN:
2017-11-28 04:14:03 +01:00
if (this.karelY < 4 ) {
this.karelY += 1;
}
break;
2017-05-26 06:01:00 +02:00
case Direction.RIGHT:
2017-11-28 04:14:03 +01:00
if (this.karelX < 4 ) {
this.karelX += 1;
}
break;
2017-05-26 06:01:00 +02:00
}
}
pressedAB() {
this.isKarelActive = !this.isKarelActive;
}
}
```
2017-11-28 04:14:03 +01:00
## About the authors
2017-05-26 06:01:00 +02:00
2017-11-28 04:14:03 +01:00
This project was contributed by Dr. David Fisher a professor at [Rose-Hulman Institute of Technology ](https://www.rose-hulman.edu/academics/faculty/fisher-david-fisherds.html ). Dr. Fisher loves educational robotics and runs various outreach programming activities, including a summer camp, called [Connecting with Code ](https://connectingwithcode.org ), which gives a @boardname @ to each participant.