2016-03-25 16:47:20 -07:00
|
|
|
# Forever
|
|
|
|
|
2016-05-20 13:09:18 -07:00
|
|
|
Keep running part of a program
|
|
|
|
[in the background](/reference/control/in-background).
|
2016-03-25 16:47:20 -07:00
|
|
|
|
|
|
|
```sig
|
|
|
|
basic.forever(() => {
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Example: compass
|
|
|
|
|
2016-05-20 13:09:18 -07:00
|
|
|
The following example constantly checks the
|
|
|
|
[compass heading](/reference/input/compass-heading)
|
|
|
|
and updates the screen with the direction.
|
2016-03-25 16:47:20 -07:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
|
|
|
let heading = input.compassHeading()
|
|
|
|
if (heading < 45) {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showString("N")
|
2016-03-25 16:47:20 -07:00
|
|
|
} else if (heading < 135) {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showString("E")
|
2016-03-25 16:47:20 -07:00
|
|
|
}
|
|
|
|
else if (heading < 225) {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showString("S")
|
2016-03-25 16:47:20 -07:00
|
|
|
}
|
|
|
|
else {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showString("W")
|
2016-03-25 16:47:20 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Example: counter
|
|
|
|
|
2016-05-20 13:09:18 -07:00
|
|
|
The following example keeps showing the [number](/reference/types/number) stored in a global variable.
|
|
|
|
When you press button `A`, the number gets bigger.
|
2016-11-01 10:42:42 -07:00
|
|
|
You can use a program like this to count things with your @boardname@.
|
2016-03-25 16:47:20 -07:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
let num = 0
|
|
|
|
basic.forever(() => {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showNumber(num)
|
2016-03-25 16:47:20 -07:00
|
|
|
})
|
2016-03-29 21:17:57 -07:00
|
|
|
input.onButtonPressed(Button.A, () => {
|
2016-03-25 16:47:20 -07:00
|
|
|
num = num + 1
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2016-05-20 13:09:18 -07:00
|
|
|
### Competing for the LED screen
|
2016-03-25 16:47:20 -07:00
|
|
|
|
2016-05-20 13:09:18 -07:00
|
|
|
If different parts of a program are each trying
|
|
|
|
to show something on the LED screen at the same time,
|
|
|
|
you may get unexpected results.
|
2016-11-01 17:44:37 -07:00
|
|
|
Try this on your @boardname@:
|
2016-03-25 16:47:20 -07:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showNumber(6789)
|
2016-03-25 16:47:20 -07:00
|
|
|
})
|
|
|
|
input.onButtonPressed(Button.A, () => {
|
2016-12-20 12:31:25 -08:00
|
|
|
basic.showNumber(2)
|
2016-03-25 16:47:20 -07:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### See also
|
|
|
|
|
2016-06-14 17:20:45 -04:00
|
|
|
[while](/blocks/loops/while), [on button pressed](/reference/input/on-button-pressed), [in background](/reference/control/in-background)
|
2016-03-25 16:47:20 -07:00
|
|
|
|