Total beginner question How do I make my loop run more than 1 time?

Hi! As the header says. I’m a newbie on Arduino and programming. I want a welding sketch to appear on my model railroad and I have used an existing sketch and modified to my needs. I’m going to use a LDR hidden as a roof fan on the building and I want my sketch to run 6-10 times when something passes over the roof (my hand, or some one elses) then stop until next time something trigger the sketch. The next thing is the red LED doesn’t shut down, it still glows. Parts are 3 LED (white, blue and red) 3 220ohm resistors 1 LDR with a 10k resistor any Ideas whats wrong or whats missing?

[code]
// Adapted for Pro Mini by Tom Kvichak
// Simulates an Ark Welder, and other lighting in scene
// building lights 7, 8, 10, 11
// Arc Welder 2, 3, 4 = 3 is PWM
// Last Modified 06/16/2018

int ledWhite = 6;           // 330 Ohm / White LED for welder
int ledglowPin = 5;         // 330 Ohm / Red LED for welder
int ledBlue = 8;            // 330 Ohm / Blue LED for welder
int sensor1 = A2;           // Sensor of your choice
int sensor1Reading;         // For determining state
int wait = 1000;            // delay for building lights
int count;                  // Used in Weld procedure
int weld_count = 6;         // Times to run the weld procedure

void setup() {
  // Uncomment following line to change the random pattern
  //randomSeed(analogRead(0));    
  Serial. begin(9600);
  pinMode (ledWhite, OUTPUT);
  pinMode (ledBlue, OUTPUT);
  pinMode (ledglowPin, OUTPUT);
}

void loop() {
  // Test condition of sensor and sequence
  sensor1Reading = analogRead(sensor1);
 Serial.print("Light Reading ");
 Serial.println(sensor1Reading);
 delay(5000);

if (sensor1Reading  < 370)  {

 welder();
  }
}
void welder() {
  // Estabulish the random count of the LEDs
  count = random(10, 60);
  for (int i = 0; i < count; i++)
  {
    digitalWrite(ledWhite, HIGH);
    delay(random(60));
    digitalWrite(ledWhite, LOW);
    delay(random(200));
    digitalWrite(ledBlue, HIGH);
    delay(random(10));
    digitalWrite(ledBlue, LOW);
    delay(random(100));
  }
  // Glow starts after flash stops and then dims
  for (int j = 50; j > 0; j--) {
    analogWrite(ledglowPin, j);
    delay(100);
  }
  delay(random(800, 2000));
}
void shut_down() {
  // Turn off all LEDs
  digitalWrite(ledWhite, LOW);
  digitalWrite(ledBlue, LOW);
  analogWrite(ledglowPin, 0);
}

/code]

GrapeValleyPete:
I want my sketch to run 6-10 times when something passes over the roof

What does it do at the moment?

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

...R

Ah ty. Atm it just run one time and then stops

GrapeValleyPete:
Ah ty. Atm it just run one time and then stops

I don't have time to study your program right now - probably tomorrow.

It would help if you describe in more detail what happens - what happens one time? How do you get it to happen for another one time?

...R

Are you sure you are waiting long enough to see if it repeats? You have so many delays in there that one run through the complete sequence could easily take a minute or more.

But I can't see anything in there that is counting 6 to 10. Also you have a shut_down() function but it is never called.

Steve

Robin2:
I don't have time to study your program right now - probably tomorrow.

It would help if you describe in more detail what happens - what happens one time? How do you get it to happen for another one time?

...R

No problems. I have it all set up. When I put my hand over the LDR the sequence starts as its supposed to but it only run for 1 cycle. To get more cycles I have to keep my hand over the LDR. If I start the sketch and pull the LDR it runs until I disconnect the power.

slipstick:
Are you sure you are waiting long enough to see if it repeats? You have so many delays in there that one run through the complete sequence could easily take a minute or more.

But I can't see anything in there that is counting 6 to 10. Also you have a shut_down() function but it is never called.

Steve

Hi! 10 sec or 10mins waiting same result. How do I call the shut_down? My guess on the "problem" is that the
LDR start to sense brightness to soon, if that make any sense :slight_smile: The original sketch by Tom included a startup sequence with 4 LEDs light up a building first and then run the welding part 3times and after that total shut down. He also used some IR detection. In my case I don't have one and its too big to hide on the model.

/P

The red LED probably stays on a bit because this for loop:

   for (int j = 50; j > 0; j--)

stops before j==0, leaving the red LED at 1/255 duty cycle. Try changing it to:

   for (int j = 50; j >= 0; j--)

Does your serial monitor show the updated LDR sensor reading every 5-seconds (or so)?

GrapeValleyPete:
No problems. I have it all set up. When I put my hand over the LDR the sequence starts as its supposed to but it only run for 1 cycle. To get more cycles I have to keep my hand over the LDR. If I start the sketch and pull the LDR it runs until I disconnect the power.

Then the simplest way is when the LDR changes from light to dark to run a FOR loop for the number of iterations that you need.

...R

Problem solved! I had cut out too much from the original code, simple as that. I share the working one here. Thanks for all comments. /P
[code

// Adapted for Pro Mini by Tom Kvichak
// Simulates an Ark Welder, and other lighting in scene
// building lights 7, 8, 10, 11
// Arc Welder 2, 3, 4 = 3 is PWM
// Last Modified 06/16/2018

int ledWhite = 6;           // 330 Ohm / White LED for welder
int ledglowPin = 5;         // 330 Ohm / Red LED for welder
int ledBlue = 9;            // 330 Ohm / Blue LED for welder
int sensor1 = A2;           // Sensor of your choice
int sequence_started = 0;
int sensor1Reading;         // For determining state
int wait = 1000;            // delay for building lights
int count;                  // Used in Weld procedure
int weld_count = 12;         // Times to run the weld procedure

void setup() {
  // Uncomment following line to change the random pattern
  //randomSeed(analogRead(0));    
  Serial. begin(9600);
  pinMode (ledWhite, OUTPUT);
  pinMode (ledBlue, OUTPUT);
  pinMode (ledglowPin, OUTPUT);
}

void loop() {
  // Test condition of sensor and sequence
  sensor1Reading = analogRead(sensor1);
 Serial.print("Light Reading ");
 Serial.println(sensor1Reading);
 delay(500);

if (sensor1Reading  < 370)  {
  for (int x =1;x <weld_count;x++){
 welder();
  }
}
sequence_started = 0;
shut_down();
}
void welder() {
  // Estabulish the random count of the LEDs
  count = random(10, 60);
  for (int i = 0; i < count; i++)
  {
    digitalWrite(ledWhite, HIGH);
    delay(random(60));
    digitalWrite(ledWhite, LOW);
    delay(random(200));
    digitalWrite(ledBlue, HIGH);
    delay(random(10));
    digitalWrite(ledBlue, LOW);
    delay(random(100));
  }
  // Glow starts after flash stops and then dims
  for (int j = 50; j > 0; j--) {
    analogWrite(ledglowPin, j);
    delay(100);
  }
  delay(random(800, 2000));
}
void shut_down() {
  // Turn off all LEDs
  digitalWrite(ledWhite, LOW);
  digitalWrite(ledBlue, LOW);
  digitalWrite(ledglowPin, LOW);
}

[/code]

If you wanted the welder to run between 6-10 times (or 12 as you have it now) you could consider something like

if (sensor1Reading  < 370)  {
  weld_count = random(6,13);
  for (int x =0;x <weld_count;x++){
 welder();
  }
}

for a bit of variety.

Note that starting that for loop with x=1 will give you 1 less count.

Metallor:
If you wanted the welder to run between 6-10 times (or 12 as you have it now) you could consider something like

if (sensor1Reading  < 370)  {

weld_count = random(6,13);
  for (int x =0;x <weld_count;x++){
welder();
  }
}




for a bit of variety.

Note that starting that for loop with x=1 will give you 1 less count.

Great idea, ty :slight_smile: