pxt-calliope/docs/device/data-analysis/generating.md
Amerlander 918af4f3ac
Bump V3.0.22 (#110)
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

Co-authored-by: Juri <gitkraken@juriwolf.de>
2020-09-08 02:04:25 -07:00

112 lines
3.0 KiB
Markdown

# Generating data
## Sensor values
Most of the data you want to record probably comes from values measured by the sensors. The sensor values are read using ``||input:Input||`` blocks. They return to your program the current value measured by the sensor.
```block
serial.writeValue("accelX", input.acceleration(Dimension.X))
```
```block
serial.writeValue("heading", input.compassHeading())
```
```block
serial.writeValue("light", input.lightLevel())
```
```block
serial.writeValue("forceX", input.magneticForce(Dimension.X))
```
## Pin data
External sensors and devices connected to the board are read using the ``||pins:Pins||`` blocks. Here are some examples of reading the pins and reporting measurements from devices:
### Soil moisture
```block
let moisture = pins.analogReadPin(AnalogPin.P0)
serial.writeValue("moisture", moisture)
```
### Closed door detector
```block
pins.onPulsed(DigitalPin.P0, PulseValue.Low, () => {
serial.writeValue("DoorClosed", 1)
})
```
### I2C humidity sensor
```block
let humidity = pins.i2cReadNumber(61, NumberFormat.Int8LE)
serial.writeValue("humidity", humidity)
```
## Human events
Sometimes we want to track human interactions with a device. These events might be button presses, gestures, or pin touches.
### Example: Button tracker
Run this example in the editor and switch to the Data Viewer. Watch it plot your button presses like pulse events in the chart.
```blocks
let buttonValue = 0
basic.forever(() => {
if (input.buttonIsPressed(Button.A)) {
buttonValue = 1
} else {
buttonValue = 0;
}
serial.writeValue("ButtonA", buttonValue)
basic.pause(200)
})
```
Writing an additional value creates another stream that will appear in a separate chart. Adding and event for another button press, we can plot pulses on a second chart.
```blocks
let buttonValue = 0
basic.forever(() => {
if (input.buttonIsPressed(Button.A)) {
buttonValue = 1
} else {
buttonValue = 0;
}
serial.writeValue("ButtonA", buttonValue)
if (input.buttonIsPressed(Button.B)) {
buttonValue = 1
} else {
buttonValue = 0;
}
serial.writeValue("ButtonB", buttonValue)
basic.pause(200)
})
```
## Time and timestamps
A timestamp marks when you read a value or detect that an event happens. These are commonly written along with other data values as an additional data item. The data viewer will create timestamps for values it sees when you write them. If you are downloading and saving data from the Data Viewer, it creates the timestamps for you and you don't need to add them.
If you need your own time values, you can make them from the **running time** of the board.
```block
input.onGesture(Gesture.Shake, () => {
serial.writeValue("shaken", input.runningTime())
})
```
Also, you can write your own custom CSV with your own timestamp:
```blocks
serial.writeLine("timestamp,temp,light")
basic.forever(() => {
serial.writeNumbers([input.runningTime(), input.temperature(), input.lightLevel()])
basic.pause(30000)
})
```