Dear Pete,
I still really haven’t figured out how to get 3 different patterns like:
buzz-buzz-buzz-pause, buzz-buzz-buzz-pause, buzz-buzz-buzz-pause, etc.
long buzz-long buzz-long buzz-short pause-buzz, long buzz-long buzz-long buzz-short pause-buzz, etc.
…and a couple of other patterns.
I have created a state variable that changes based upon which SEGMENTED BUTTON is pressed. When I return to loop(), I have some conditionals that call the “buzzing” functions to vibrate each of the patterns. As long as the state variables don’t change, the “buzzing” functions should continue to repeat themselves.
When I press another SEGMENTED BUTTON, the pattern should change to whatever pattern is associated with the new button.
I am avoiding execution-halting delays and only using non-blocking delays.
void loop()
{
Blynk.run();
timer.run();
if(state == 1)program1(); //off
if(state == 2)program2(); //manual mode with duty cycle slider adjustment
if(state == 3)program3(); //program 1 - buzz-buzz-buzz-buzz-buzz....
if(state == 4)program4(); //program 2 - long buzz-long buzz-long buzz-pause
if(state == 5)program5(); //program 3 - short buzz-long buzz-short-buzz-pause
}
When I apply the “buzz”, I ramp up and then ramp down using the code. I basically use y=mx+b equation of a line. On the Y-axis, I used Ymax (=255) and Ymin (equal to let’s say 100). The period is anywhere from 50-500 ms depending on how fast I want the “buzz” pulses. This is a pseudo sine wave (linear approximation for simplification). I basically get a sawtooth pattern in the time domain. I might convert to a sine wave in the future, but this is good for now.
int vibrateMotor(void)
{
long m, b;
long x, y;
m = (Ymax-Ymin)/(period/2.0);
b = Ymin;
for(x=0; x<period/2.0; x++)
{
analogWrite(VIB, m * x + b); //increase pwm
Blynk.virtualWrite(V3, m * x + b); //sending to Blynk gauge
}
b = Ymax + m * period/2.0; //new y-intercept for decreasing pwm
for(x=period/2.0; x<period; x++)
{
analogWrite(VIB, -m * x + b); //decrease pwm
Blynk.virtualWrite(V3, -m * x + b); //sending to Blynk gauge
}
}
My simplest program is just a sawtooth pattern with no pause inbetween. The period seems corrupted. I tried 500ms, but this went extremely slow. So I kept decreasing until I got this to repeatedly vibrate. It works much better with a period of 50ms rather than 500ms.
int program3(void) //state=3, buzz-buzz-buzz-buzz-buzz-buzz,...
{
digitalWrite(LED_BUILTIN,HIGH);
period = 50.0; //this is 50L
timer.setInterval(period, vibrateMotor);
}
I then tried to make varying buzz periods back to back and tried a few things like this:
int program4(void) //state=4 buzz-buzz-buzz-pause, buzz-buzz-buzz-pause,...
{
if(x<3)
{
program4a(); //3 buzz cycles
x++;
}
if(x==3 && y<1) //1 pause cycle
{
program4b();
y++;
}
if(x==3 && y==1) //reset x and y
{
x=0;
y=0;
}
}
int program4a(void) //3 buzzes of this
{
period = 150;
magnitude = 255;
timer.setInterval(period, vibrateMotor);
}
int program4b(void) //...and one pause of this
{
period = 100;
magnitude = 0;
timer.setInterval(period, vibrateMotor);
}
I cannot seem to “break” out of the buzz-buzz-buzz functions. I am not really sure how to structure this.
Any suggestions would be greatly appreciated. Thank you.
–Neal