So I'm trying to make a vibration motor pulse and an LED blink. I want them to pulse four times with a 60ms delay between pulses.
So far, I have this:
analogWrite(motorPin, 255); //Strobe the motor four times
digitalWrite(ledPin, HIGH); //Strobe the LED four times
delay(30); //With a short delay in between pulses
analogWrite(motorPin, 0);
digitalWrite(ledPin, LOW);
delay(30);
analogWrite(motorPin, 255);
digitalWrite(ledPin, HIGH);
delay(30);
analogWrite(motorPin, 0);
digitalWrite(ledPin, LOW);
delay(30);
analogWrite(motorPin, 255);
digitalWrite(ledPin, HIGH);
delay(30);
analogWrite(motorPin, 0);
digitalWrite(ledPin, LOW);
delay(30);
analogWrite(motorPin, 255);
digitalWrite(ledPin, HIGH);
delay(30);
analogWrite(motorPin, 0);
digitalWrite(ledPin, LOW);
delay(30);
It works, but because the future of this project involves heavy use of resources, I would like to avoid using the delay(ms) function anywhere in this code, especially since I can't let a simple physical notification of an event pause everything, even if it's only for 240ms.
I looked around, and decided using millis() might be my best bet.
The only code example I could find was http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay, but it doesn't suit my needs or help me very much. It's a looping state-sensitive thing.
Still, I tried, and I came up with this:
const int motorPin = 12; //vibration motor is attached to motorPin
const int ledPin = 13; //status LED is attached to ledPin
int ledState = LOW;
int motorState = 0;
long previousVibeMillis = 0;
long intervalVibe = 30;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (millis() - previousVibeMillis > intervalVibe)
{
previousVibeMillis = millis();
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
if (motorState == 0)
{
motorState = 255;
}
else
{
motorState = 0;
}
analogWrite(motorPin, motorState);
digitalWrite(ledPin, ledState);
}
}
That works alright, if I just want to strobe the motor and the LED on and off endlessly, but it won't let me control the finite number of times I want it to pulse (4 times per notification).
I tried using this in my main program, modified as such:
if (millis() - previousMillis > 30)
{
previousMillis = millis();
analogWrite(motorPin, 255);
digitalWrite(ledPin, HIGH);
}
if (millis() - previousMillis > 30)
{
previousMillis = millis();
analogWrite(motorPin, 0);
digitalWrite(ledPin, LOW);
}
It does not have the desired effect.
Does anybody have any ideas that will help me get a hold on this thing? All I want is (basically) for a button to be pressed, and as a result, an LED blinks four times, while simultaneously a vibration motor pulses four times. The idea is for this whole thing to later be in-synch with a sound effect (four chirps, hence four blinks and four vibration pulses)...but that's VERY far down the road, and absolutely not a priority.
The button-press code is already taken care of. I'm using the one-pin no-component touch sensor found here: http://www.arduino.cc/playground/Code/CapacitiveSensor, but slightly modified for my purposes (one pin, global access to the variables).
When somebody touches the button, I want the motor to pulse and light to blink, and I only want the cycle to occur once, until the button is released and pressed again. For some reason this is VERY hard for me to figure out. I've been wracking my brain for days, and making very little progress.
So basically, I need to know how to effectively mimic the delay() function, without actually causing a delay in code execution. Any ideas?
Also, found a workaround for the no-links-in-first-post-thing. Lucky me.