Vibration pattern

Maybe you want to add some vibration pattern to your BI2 toy? With the Blynk app you can easily add a slider to control the speed of the vibration motor. But how do you implement a sinus mode?

First, you have to introduce a way to remember that you are in this sinus mode. this is easily done by introducing a boolean variable which remember the state (mode) you are in.

bool sinusMode = 0;

Then you use another timer. At each tick, the speed of the motor is adjusted according to the sinus function. The passed time is stored in a variable t which is increased after each tick.

Now you introduce two more variables which set the minimal and maximal motor speed within the interval 1..1024

Next you have to add a timer event which describes what is to do at each tick of the timer. The timer event calculates the actual speed with the sinus function using the passed time and minimal and maximal speed. Then t is increased.

int motorPin=12; // M2
double t=0; // time ticks
bool sinusMode = 0;

int offsetVib = 150; //minimal motor speed (interval 1..1024)
int maxVib = 450; // maximal motor speed (for 1.5V motors)
void myTimerEvent2()
{ // update motor speed when in sinus mode
  if (sinusMode) {
    double a = sin(t);
    double x = (a+1)/2*(maxVib-offsetVib)+offsetVib;
    analogWrite(motorPin,x);
    t=t+0.15; //change of speed
  }
}

In the Blink app you must insert a button for changing to sinus mode. Use virtual pin 1 and switch mode.

 

The app has to tell the BI2 which mode is selected. On any button press the corresponding function for virtual pin 1 will be activated. It just set the mode off when on and vice versa.

BLYNK_WRITE(V1) // toggle motor to sinus mode and back
{ 
  if (sinusMode) {
    sinusMode=false;
    analogWrite(motorPin,0);
    } 
    else {
      sinusMode=true;
    }
}

Here is the complete Arduino code, you can upload to your Arduino board (or BI2).

Questions? Ask jacardano@gmail.com (upps, it is 2021?!)

/*************************************************************
bodyinteraction.org
program for reading IMU data, setting LED color and seting 
sinus mode for motor control
*/
// include this library in the Arduino library manager
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 1
// LED data pin is connected to pin?
#define DATA_PIN 14

// Define the array of leds
CRGB leds[NUM_LEDS];
int wave;

// Library for IMU MPU9250
#include 
MPU9250_asukiaaa mySensor;
float aX, aY, aZ, mDirection, pitch, roll, yaw;

#include 
#include 

// Auth Token in the Blynk App.

char auth[] = "Insert your Blink token";

// Your WiFi credentials.
char ssid[] = "Insert your SSID";
char pass[] = "Insert password";

BlynkTimer timer;

void myTimerEvent1()
{ // read acceleration data and send to smartphone via Blink
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();
  // read gyroscope update
  mySensor.magUpdate();
  mDirection = mySensor.magHorizDirection();
  // calculate pitch, roll, yaw (raw approximation)
  float pitch = 180 * atan (aX/sqrt(aY*aY + aZ*aZ))/M_PI;
  float roll = 180 * atan (aY/sqrt(aX*aX + aZ*aZ))/M_PI;
  float yaw = 180 * atan (aZ/sqrt(aX*aX + aZ*aZ))/M_PI;
  // send data to app via virtual ports, e.g. virtual pin V2 is set to pitch
  Blynk.virtualWrite(V2, pitch);
  Blynk.virtualWrite(V3, roll);
  Blynk.virtualWrite(V4, yaw);
  Blynk.virtualWrite(V5, mDirection);
}

#include 
int motorPin=12;
double t=0; // time ticks
bool sinusMode = 0;

int offsetVib = 150; //minimal motor speed (interval 1..1024)
int maxVib = 450; // maximal motor speed (for 1.5V motors)
void myTimerEvent2()
{ // update motor speed when in sinus mode
  if (sinusMode) {
    double a = sin(t);
    double x = (a+1)/2*(maxVib-offsetVib)+offsetVib;
    analogWrite(motorPin,x);
    t=t+0.15; //change of speed
    Serial.print("t=");Serial.print(t);Serial.print("  a=");Serial.print(a);Serial.print("  x=");Serial.println(x);
  }
}

BLYNK_WRITE(V0) // set RGB color values which are transmitted from the app as V0 (virtual pin 0)
{ 
  int i = param[0].asInt();
  int j = param[1].asInt();
  int k = param[2].asInt();
  leds[0].setRGB(j,i,k);
  FastLED.show();
}


BLYNK_WRITE(V1) // toggle motor to sinus mode and back
{ 
  if (sinusMode) {
    sinusMode=false;
    analogWrite(motorPin,0);
    } 
    else {
      sinusMode=true;
    }
}

void setup()
{
  Serial.begin(115200);
  //initialize LED
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  // setup i2c connection to IMU, setup IMU
  Wire.begin(4, 5); //sda, scl
  mySensor.setWire(&Wire);
  mySensor.beginAccel();
  mySensor.beginMag();

  Blynk.begin(auth, ssid, pass); // init Blink
  timer.setInterval(1000, myTimerEvent1); // timer for LED color selection
  timer.setInterval(80, myTimerEvent2); // timer for sinus curve motor speed 
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: