Hi,
I have a problem with my program. I am programming on an Arduino Nano and I want to let a motor vibrate for 2 vibrations of 250 milliseconds with a pause of 250 milliseconds in between. The problem is that the motor does not stop after 2 vibrations, while (to my knowledge) I did state that it should do that in the for loop. How do I stop the loop after 2 vibrations
const int motorPin = 3;
unsigned long seconds = 1000;
void setup() {
// put your setup code here, to run once:
pinMode (motorPin, OUTPUT);
}
void loop() {
uint32_t period = 1 * 750L;
for(uint32_t tStart = millis(); (millis()-tStart) < period; ){
digitalWrite(motorPin, HIGH);
delay (250);
digitalWrite(motorPin, LOW);
delay(250);
}
}
Put this at the end of the loop function...
while ( true );
Many ways to do that. If that's all you want to do, take it out of loop() and put it in setup(). If you want to leave it in loop, declare a static byte equal to the number of times you want to repeat an action and then enclose the code in a while loop,
static byte repeat = 2;
while(repeat)
{
// your code here;
repeat--;
}
DKWatson:
Many ways to do that. If that's all you want to do, take it out of loop() and put it in setup(). If you want to leave it in loop, declare a static byte equal to the number of times you want to repeat an action and then enclose the code in a while loop,
static byte repeat = 2;
while(repeat)
{
// your code here;
repeat--;
}
if I do this it doesn't work
static byte repeat = 2;
while(repeat)
{
digitalWrite(motorPin, HIGH); //do vibrate
delay (250); //250 milliseconds
digitalWrite(motorPin, LOW); //don't vibrate
delay(250); //250 milliseconds
repeat--;
}
}
it doesn't work
What does it do ?
What do you see if you print the value of repeat ?
Please post your complete program