Controlling on / off cycle speed of a pair of vibration motors - no delay

I have a pair of vibration motors that alternately buzz. At the moment the on / off is managed by delay() which takes it's value from a potentiometer via an int potValue. The response to me turning the pot is slooow, so I'd like to ditch the delay (). The code is here -

int leftBuz = 3;
int rightBuz = 5;
int potPin = A0;
int potValue = 0;

void setup()

  pinMode(leftBuz, OUTPUT);
  pinMode (rightBuz, OUTPUT);
  pinMode (potPin, INPUT);
  Serial.begin (9600);

void loop()
  potValue = analogRead (potPin);
  delay (2);
  analogWrite(leftBuz, 153);
  delay (potValue);
  analogWrite(leftBuz, 0);
  delay (potValue);
  analogWrite (rightBuz, 153);
  delay (potValue);
  analogWrite (rightBuz, 0);
  delay (potValue);


All of the tutorials suggest millis which is about time, but my HIGH or LOW is all about values 0-1023 from a potentiometer, which is dynamic.
So I'm going for a 'quicker' button and a 'slower' button and then count button presses. This could give me something like a 0-9 integer called 'stepSpeed' or similar (the idea is to promote stepping in my neurology patients - I'm a physio).
But how do I take this number and use it to control the frequency of a HIGH to LOW change in two motors?
I'd love some ideas, or example code, I don't need the finished thing, thanks!!


Think about how you would do what you want the Arduino to do. Suppose that, instead of a few milliseconds, you wanted to operate the motors for hours at a time. How would YOU know when it was time to do something different? How would YOU know what to do next? You have a pencil, a pad of paper, a watch, and nothing else. (The Arduino has variables, an assignment operator, and millis().)

Thanks Paul. OK -
A. Check motors, turn the stepSpeed knob to 3 and check my watch (millis)
B. Read the newspaper, wander back and check if the knob has been turned up / down
If it has, compare the stepSpeed knob ( x 100 to give me milliseconds) to millis, and as long as it’s <, keep the left buzzer on and the right buzzer off.

-i.e. I think use an if statement to compare stepSpeed x 100 against millis???



If all you need is alternate ON/OFF, use the "Blink Without Delay" example in the IDE.

Instead of turning one output OFF/ON, add another to ON/OFF.

If you need a STOP interval.
Use the delay loop to increment a counter, 0, 1, 2, 3 in a loop.
Use to select which combination of vibmotor1 or 2 to run or stop.

Use your pot to adjust the "interval' variable.

Tom.. :slight_smile: