Vibration motor increasing amplitude

Hello everybody,

I am working on a prototype for a haptic device for laparoscopic surgery and I am using small coin vibration motors (ERM) to simulate touches. Now I am working on a scene where you can pick up a cube (using Leap Motion). The motors’ amplitude (pulses) should increase as you push harder in the cube. Arduino receives a value from 0-255 from Unity and the value increases proportionally with the collision. I am now using the following code:

motorIntensity = map(receivedValues[2], 0, 255, 0, 200);
  if (receivedValues[2] > 0) {
    for (int i = 0; i <= motorIntensity; i = i + 30) {
      analogWrite(middlefinger, i);
      delay(10);
    }
    for (int i = motorIntensity; i >= 0; i = i - 30) {
      analogWrite(middlefinger, i);
      delay(10);
    }
  }
  else {
    analogWrite(middlefinger, 0);
  }

I notice that during this loop, the Arduino doesn’t read out the Serial input which leads to a delay and missing information (not real-time).

I was wondering if anyone could help me with a code or a function which makes sure that everything is real-time? I know it’s kind of a complicated situation…

Thank you in advance :smiley:

Lot

Get rid of the delays and the for loops. Both are blocking. Let loop() do the looping. The several things at a time tutorial might help.