IMU Drawing Machine

Components List

Microcontroller:
- Arduino Nano 33 BLE*2
- Arduino Nano Shield Module*2

Input / Output
- Button*1

Power
- 9V Battery Module*2

Attachment
Band
Connector

If you do not have all the above components, please refer to Hardware page to get the kit.


Software

You can try the sketch on p5 editor, or you can download the code here.


Hardware

This example is using two hardware systems for inputs. One is controlling the position by IMU sensor, and the other one is using a button for triggering the sound.


IMU Input

About accessing IMU sensor from Arduino Nano 33 BLE, please refer to Get Started with Arduino


        /*
        FlexiDots Theremin
        https://studiohuahong.github.io/FlexiDots/examples/theremin.html
        */
        #include 
        #include  // change to Arduino_LSM6DS3.h for Nano 33 IoT or Uno WiFi Rev 2
        
        BLEService        imuService                 ("2A5A20B9-0000-4B9C-9C69-4975713E0FF2");
        BLECharacteristic accelerationCharacteristic ("2A5A20B9-0001-4B9C-9C69-4975713E0FF2", BLENotify, sizeof(float) * 3);
        
        void setup() {
          Serial.begin(9600);
          //  while (!Serial); // uncomment to wait for Serial port to be opened
        
          if (!IMU.begin()) {
            Serial.println("Failed to initialize IMU!");
            while (1);
          }
        
          if (!BLE.begin()) {
            Serial.println("Failed to initialize IMU!");
            while (1);
          }
        
          BLE.setLocalName("FlexiDots IMU Drawing Machine");
          BLE.setDeviceName("FlexiDots IMU Drawing Machine");
          BLE.setAdvertisedService(imuService);
        
          imuService.addCharacteristic(accelerationCharacteristic);
        
          BLE.addService(imuService);
          BLE.advertise();
        }
        
        void loop() {
          if (BLE.connected()) {
            if (accelerationCharacteristic.subscribed() && IMU.accelerationAvailable()) {
              float acceleration[3];
              // x, y, z
              IMU.readAcceleration(acceleration[0], acceleration[1], acceleration[2]);
              accelerationCharacteristic.writeValue(acceleration, sizeof(acceleration));
            }
          }
        }
    

Button Imput

Connect the button on D2 pin.


        /*
        FLEXIDOTS Theremin Button
        https://studiohuahong.github.io/FlexiDots/get-started/get-started-arduino-p5.html
      */
      
      #include 
      
      const int ledPin = LED_BUILTIN; // set ledPin to on-board LED
      const int buttonPin = 2; // set buttonPin to digital pin 4
      
      BLEService        imuService                 ("2A5A20B9-0000-4B9C-9C69-4975713E0FF2");
      //BLECharacteristic accelerationCharacteristic ("2A5A20B9-0001-4B9C-9C69-4975713E0FF2", BLENotify, sizeof(float) * 3);
      BLEByteCharacteristic buttonCharacteristic("2A5A20B9-0002-4B9C-9C69-4975713E0FF2", BLERead | BLENotify);
      
      void setup() {
        Serial.begin(9600);
        //while (!Serial); //disable this line, I have no idea why they want this line
      
        pinMode(ledPin, OUTPUT); // use the LED as an output
        pinMode(buttonPin, INPUT); // use button pin as an input
      
        // begin initialization
        if (!BLE.begin()) {
          Serial.println("starting Bluetooth® Low Energy module failed!");
          while (1);
        }
      
        // set the local name peripheral advertises
        BLE.setLocalName("FlexiDots Theremin Button");
        BLE.setDeviceName("FlexiDots Theremin Button");
        // set the UUID for the service this peripheral advertises:
        BLE.setAdvertisedService(imuService);
      
        // add the characteristics to the service
        imuService.addCharacteristic(buttonCharacteristic);
      
        // add the service
        BLE.addService(imuService);
      
        buttonCharacteristic.writeValue(0);
      
        // start advertising
        BLE.advertise();
      
        Serial.println("Bluetooth® device active, waiting for connections...");
      }
      
      void loop() {
        // poll for Bluetooth® Low Energy events
        BLE.poll();
      
        // read the current button pin state
        char buttonValue = digitalRead(buttonPin);
      
        // has the value changed since the last read
        bool buttonChanged = (buttonCharacteristic.value() != buttonValue);
      
        if (buttonChanged) {
          // button state changed, update characteristics
          buttonCharacteristic.writeValue(buttonValue);
        }
      
        if (buttonValue == 1){
          digitalWrite(ledPin, HIGH);
        }else{
          digitalWrite(ledPin, LOW);
        }
      }