source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/examples/ScratchSensorBoard/ScratchSensorBoard.ino

Last change on this file was 3, checked in by markus, 7 years ago

Setup project structure

File size: 1.5 KB
Line 
1// example sketch to turn your circuit playground into a picoboard
2// potentiometer is 'simulated' by tilting the cplay (accelerometer Z is reported)
3// pin #13 red LED is lit when data is sent to Scratch!
4
5#include <Adafruit_CircuitPlayground.h>
6
7#define SCRATCH_DATA_REQUEST  0x01
8#define SCRATCH_FW_VER        0x04
9
10void setup() {
11  while (!Serial);
12  Serial.begin(38400);
13  //Serial.println("Start!");
14
15  CircuitPlayground.begin();
16  pinMode(13, OUTPUT);
17}
18
19void loop() {
20  if (!Serial.available())
21    return;
22
23  char c = Serial.read();
24
25  if (c != SCRATCH_DATA_REQUEST)
26    return;
27
28  digitalWrite(13, HIGH);
29  // data is a request!
30  sendPacket(0xF, SCRATCH_FW_VER);
31  sendPacket(0x0, analogRead(A0));
32  sendPacket(0x1, analogRead(A1));
33  sendPacket(0x2, analogRead(A2));
34  if (CircuitPlayground.leftButton() || CircuitPlayground.rightButton()) {
35    sendPacket(0x3, 0x0);
36  } else {
37    sendPacket(0x3, 0xFFFF);
38  }
39  sendPacket(0x4, CircuitPlayground.temperature() * 10);
40  sendPacket(0x5, 1023 - CircuitPlayground.lightSensor());
41  float s = CircuitPlayground.mic.soundPressureLevel(10);
42  sendPacket(0x6, s);
43  float z = CircuitPlayground.motionZ();
44  z *= -50; // reverse direction
45  z += 512;
46  sendPacket(0x7, z);
47
48  digitalWrite(13, LOW);
49}
50
51void sendPacket(uint8_t channel, uint16_t data) {
52  uint8_t packet0, packet1;
53
54  channel &= 0xF;
55  if (data >= 1024) data = 1023;
56 
57  packet0 = 0x80 | (channel << 3) | ((data >> 7) & 0x7);
58  packet1 = data & 0x7F;
59
60  Serial.write(packet0);
61  //delay(1);
62  Serial.write(packet1);
63  //delay(1);
64}
Note: See TracBrowser for help on using the repository browser.