# repeat a function a specified number of times when triggered by motion sensor

Hi,
I am a beginner and I was hoping to find some help with my project. I have an LED sequence that I would like to loop a certain number of times - maybe 10 - when triggered by a motion sensor. My current code is posted below, I am sure there are other areas where the code could be improved or consolidated but for now I am most interested in figuring out how to repeat the twinkle function 10 times after the motion sensor is activated.
thank you very much

int calibrationTime = 30;
int value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14, value15;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 47; //the digital pin connected to the PIR sensor’s output

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9;
int ledPin9 = 10;
int ledPin10 = 11;
int ledPin11 = 12;
int ledPin12 = 13;
int ledPin13 = 44;
int ledPin14 = 45;
int ledPin15 = 46;

long time = 0;

int periode = 2000;

int displace1 = 200;
int displace2 = 400;
int displace3 = 600;
int displace4 = 800;
int displace5 = 1000;
int displace6 = 1200;
int displace7 = 1400;
int displace8 = 1600;
int displace9 = 1800;
int displace10 = 2000;
int displace11 = 2200;
int displace12 = 2400;
int displace13 = 2600;
int displace14 = 2800;
int displace15 = 3000;

/////////////////////////////
//SETUP
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
pinMode(ledPin14, OUTPUT);
pinMode(ledPin15, OUTPUT);

digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate
Serial.print(“calibrating sensor “);
for (int i = 0; i < calibrationTime; i++) {
Serial.print(”.”);
delay(1000);
}
Serial.println(" done");
Serial.println(“SENSOR ACTIVE”);
delay(50);
if (digitalRead(pirPin) == HIGH) {

for (int x = 0; x < 10; x++) {
digitalWrite(13, HIGH); // set the LED on
delay(13); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(13); //
}
}
}

////////////////////////////
//LOOP
void loop() {

if (digitalRead(pirPin) == HIGH) {

twinkle(10);

if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("—");
Serial.print(“motion detected at “);
Serial.print(millis() / 1000);
Serial.println(” sec”);
delay(50);
}
takeLowTime = true;
}

if (digitalRead(pirPin) == LOW) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
digitalWrite(ledPin10, LOW);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin14, LOW);
digitalWrite(ledPin15, LOW);

if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print(“motion ended at “); //output
Serial.print((millis() - pause) / 1000);
Serial.println(” sec”);
delay(50);
}
}
}

void twinkle(int count)
{
while ( count-- )
{
time = millis();
value1 = 28 + 27 * cos(-2 * PI / periode * (displace1 - time));
value2 = 28 + 27 * cos(-2 * PI / periode * (displace2 - time));
value3 = 28 + 27 * cos(-2 * PI / periode * (displace3 - time));
value4 = 28 + 27 * cos(-2 * PI / periode * (displace4 - time));
value5 = 28 + 27 * cos(-2 * PI / periode * (displace5 - time));
value6 = 28 + 27 * cos(-2 * PI / periode * (displace6 - time));
value7 = 28 + 27 * cos(-2 * PI / periode * (displace7 - time));
value8 = 28 + 27 * cos(-2 * PI / periode * (displace8 - time));
value9 = 28 + 27 * cos(-2 * PI / periode * (displace9 - time));
value10 = 28 + 27 * cos(-2 * PI / periode * (displace10 - time));
value11 = 28 + 27 * cos(-2 * PI / periode * (displace11 - time));
value12 = 28 + 27 * cos(-2 * PI / periode * (displace12 - time));
value13 = 28 + 27 * cos(-2 * PI / periode * (displace13 - time));
value14 = 28 + 27 * cos(-2 * PI / periode * (displace14 - time));
value15 = 28 + 27 * cos(-2 * PI / periode * (displace15 - time));
analogWrite(ledPin1, value1); // sets the value (range from 0 to 255)
analogWrite(ledPin2, value2); // sets the value (range from 0 to 255)
analogWrite(ledPin3, value3); // sets the value (range from 0 to 255)
analogWrite(ledPin4, value4); // sets the value (range from 0 to 255)
analogWrite(ledPin5, value5); // sets the value (range from 0 to 255)
analogWrite(ledPin6, value6); // sets the value (range from 0 to 255)
analogWrite(ledPin7, value7); // sets the value (range from 0 to 255)
analogWrite(ledPin8, value8); // sets the value (range from 0 to 255)
analogWrite(ledPin9, value9); // sets the value (range from 0 to 255)
analogWrite(ledPin10, value10); // sets the value (range from 0 to 255)
analogWrite(ledPin11, value11); // sets the value (range from 0 to 255)
analogWrite(ledPin12, value12); // sets the value (range from 0 to 255)
analogWrite(ledPin13, value13); // sets the value (range from 0 to 255)
analogWrite(ledPin14, value14); // sets the value (range from 0 to 255)
analogWrite(ledPin15, value15); // sets the value (range from 0 to 255)
}
}

``````int value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14, value15;
``````

When you find yourself putting numbers on the ends of your variable names, you need to learn about arrays. When you have 15 of them, you're way overdue for a lesson in arrays. Why repeat the same code 15 times when you could write it once?

how to repeat the twinkle function 10 times

Have you ever heard of a for loop? I would go learn about what that is if I were you.

I have used arrays before, I didn't think about them when writing this for whatever reason but I will update the code now. I had tried using a for loop last night but it didn't seem like it was working - I got it working now though. I haven't done any arduino programming for over a year and I never was too great at it in the first place so a lot of my problems are silly oversights. Thank you for your help.

``````      delay(13);              // wait for a second
``````

A lack of comments is bad. Wrong comments are even worse.

``````      Serial.print(millis() / 1000);
``````

millis() returns an unsigned long (a sort of an int). 1000 is an int. In C/C++, int arithmetic confuses some people. For example, 999/1000 will be 0.

This can be easily fixed by putting a decimal point after the 1000. Even better (because it makes things more obvious) is to put “.0” after “1000” so that “1000” becomes “1000.0” . Obviously, do not include the double quote marks.

Me, I would be inclined to put millis()/1000.0 into a function, perhaps calling it seconds() and returning a rounded value (perhaps an unsigned long). It might look like:

``````unsigned long seconds() {return 0.5+millis()/1000.0 ;}
``````

I realize that these are not the areas of most interest, but I think that Delta_G already had the best comments on the areas of most interest.