Video
Connecting the Device
I don't have one of the sensors I was asked about so I set about connecting my PC to the edge connector of the micro:bit instead, the idea being that I would type data into a serial terminal application like Putty. To make the connection, I used an adapter board which has USB at one end (for my PC) and 4 wires at the other acting as TX, RX, GND and 3.3V. I connected those wires to micro:bit edge connector pins 1, 0, GND and 3.3V respectively.
micro:bit code
My code had to do several things;
- map the serial interface to pins 1 and 0 instead of the usual USB port on the micro:bit
- read comma delimited strings from the serial interface
- convert each string into a number (I performed no validation in my prototype code btw)
- send the data over Bluetooth in the format that Bitty Data Logger would understand. See http://bittysoftware.blogspot.com/p/applications.html#bdl for more information and https://github.com/bittysoftware/bitty_data_logger_device_code for example code.
Here's what the code I wrote ended up looking like, first in JavaScript:
let reading = ""
function transmitSensorData() {
let pin_shifted = pin << 14;
let ev = pin_shifted | analog_value;
control.raiseEvent(
9030,
ev
)
}
bluetooth.onBluetoothConnected(() => {
basic.showString("C")
})
bluetooth.onBluetoothDisconnected(() => {
basic.showString("D")
})
let pin = 0
let analog_value = 0
basic.showString("OK")
serial.redirect(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate115200
)
basic.forever(() => {
reading = serial.readUntil(serial.delimiters(Delimiters.Comma))
pin = 1
analog_value = parseInt(reading)
basic.showNumber(analog_value)
transmitSensorData()
})
And in fact, here's the code in full:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.