How to run set of codes at a specified duration

Hello Everyone,

I am more knowledgeable on hardware and electronics, than software and programming… which is why I am seeking for someone who can help me with the codes. I have made a simple Volume Unit (VU) Meter composed of 12 LEDs connected to Arduino Uno and I have downloaded a code and have made it worked. However, aside from the VU Meter effect, I also need to insert a series of effects.

Given Code (see attachment):

  • Code for the VU Meter
  • Code for the First effect
  • Code for the Second effect
  • Code for the Third effect, and
  • Code for the Fourth effect

What I need is:

  • to run the VU Meter effects code for 2 minutes before going to the 1st effect
  • 1st effect will run for 30 seconds after the VU Meter before going to the 2nd effect
  • 2nd effect will run for 15 seconds before going to the 3rd effect
  • 3rd effect will run for 15 seconds before going to the 4th effect
  • 4th effect will run for 30 second before going back to the VU Meter
  • this process should continuously loop

Your help is greatly appreciated.

Thank you so much.

-Kairus

int music = A0;
int output,a;
int potval=A1;
int number_of_led[12] = { 2,3,4,5,6,7,8,9,10,11,12,13}; // Assign the pins for the leds
void setup()
{
for (a = 0; a < 12; a++) 
  pinMode(number_of_led[a], OUTPUT);
}

void loop()
{
potval=analogRead(A1);
output = analogRead(music);
potval=map (potval,0,1024,5,40);
output = output/potval;   

  if (output == 0)
   {
   for(a = 0; a < 12; a++)
     {
     digitalWrite(number_of_led[a], LOW);
     }
  }
 
  else
  {
   for (a = 0; a < output; a++)
    {
     digitalWrite(number_of_led[a], HIGH);
    }
   
    for(a = a; a < 12; a++)
     {
      digitalWrite(number_of_led[a], LOW);
   
     }
  }
}





//initializing a variable for digital pin 0 to 13

int led1 = 0;
int led2 = 1;
int led3 = 2;
int led4 = 3;
int led5 = 4;
int led6 = 5;
int led7 = 6;
int led8 = 7;
int led9 = 8;
int led10 = 9;
int led11 = 10;
int led12 = 11;
int led13 = 12;
int led14 = 13;



//Arduino 4 Effect code:-

#define t   30
#define t1  20
#define t2  100
#define t3  50

void setup() {
  // set up pins 0 to 13 as outputs
  for (int i = 0; i <= 13; i++) {
    pinMode(i, OUTPUT);
  }
  for(int i=0;i<=13;i++){
  digitalWrite(i, LOW);}
}
/////////////////////////////////////////////////////////////////////////////////Effect 1
void loop(){

 effect_1();
 effect_1();

 effect_2();
 effect_2();

 effect_3();
 effect_3();

 effect_4();
 effect_4();


  }
//left to right and right to left
void effect_1()
{
for(int i=0; i<=13; i++){
digitalWrite(i, HIGH);
delay(t1);
digitalWrite(i+1, HIGH);
delay(t1);
digitalWrite(i+2, HIGH);
delay(t1);
digitalWrite(i, LOW);
delay(t1);
digitalWrite(i+1, LOW);
delay(t1);
}
for(int i=13; i>=0; i--){
digitalWrite(i, HIGH);
delay(t1);
digitalWrite(i-1, HIGH);
delay(t1);
digitalWrite(i-2, HIGH);
delay(t1);
digitalWrite(i, LOW);
delay(t1);
digitalWrite(i-1, LOW);
delay(t1);
}
}
/////////////////////////////////////////////////////////////////////////////////Effect 2
void effect_2()
{
int count = 13; // keeps track of second LED movement

  // move first LED from left to right and second from right to left
  for (int i = 0; i <=13; i++) {
    clear();
    digitalWrite(i, HIGH);      // chaser 1
    digitalWrite(count, HIGH); // chaser 2
    count--;
    // stop LEDs from appearing to stand still in the middle
    if (count != 8) {
      delay(t2);
    }
  }

  // move first LED from right to left and second LED from left to right
  for (int i = 13; i >=0; i--) {
    clear();
    digitalWrite(i, HIGH);      // chaser 1
    digitalWrite(count, HIGH); // chaser 2
    count++;
    // stop LEDs from appearing to stand still in the middle
    if (count != 8) {
      delay(t2);
    }
  }
}

////////////////////////////////////////////////////////////////////////////////Effect 3

void effect_3()
{
for(int i=0; i<14; i++){
  digitalWrite(i, HIGH);
  delay(t3);
}
for(int i=0; i<14; i++){
  digitalWrite(i, LOW);
  delay(t3);
}


for(int i = 14; i>=0; i--){
  digitalWrite(i, HIGH);
  delay(t3);
}
for(int i = 14; i>=0; i--){
  digitalWrite(i, LOW);
  delay(t3);
}
}
  ///////////////////////////////////////////////////////////////////////////Effect 4
void effect_4()
{
for(int j = 0; j <= 13; j++){
  digitalWrite(j, HIGH);
  delay(t2);
  j=j+1;
  }
for(int j = 0; j <= 13; j++){
  digitalWrite(j, LOW);
  delay(t2);
  }

  for(int k = 13; k > 0; k--){
  digitalWrite(k, HIGH);
  delay(t2);
  k=k-1;
  }
for(int k = 13; k > 0; k--){
  digitalWrite(k, LOW);
  delay(t2);
  }
}

void clear(void)
{
  for (int i = 0; i <= 13; i++) {
    digitalWrite(i, LOW);
  }
}

sample.txt (3.66 KB)

@OP
Please read https://forum.arduino.cc/index.php?topic=148850.0 and pay special attention to point #7 about posting code using code tags.

OP’s code; it’s small enough to be inserted in a post

int music = A0;
int output,a;
int potval=A1;
int number_of_led[12] = { 2,3,4,5,6,7,8,9,10,11,12,13}; // Assign the pins for the leds
void setup()
{
for (a = 0; a < 12; a++) 
  pinMode(number_of_led[a], OUTPUT);
}

void loop()
{
potval=analogRead(A1);
output = analogRead(music);
potval=map (potval,0,1024,5,40);
output = output/potval;   

  if (output == 0)
   {
   for(a = 0; a < 12; a++)
     {
     digitalWrite(number_of_led[a], LOW);
     }
  }
 
  else
  {
   for (a = 0; a < output; a++)
    {
     digitalWrite(number_of_led[a], HIGH);
    }
   
    for(a = a; a < 12; a++)
     {
      digitalWrite(number_of_led[a], LOW);
   
     }
  }
}





//initializing a variable for digital pin 0 to 13

int led1 = 0;
int led2 = 1;
int led3 = 2;
int led4 = 3;
int led5 = 4;
int led6 = 5;
int led7 = 6;
int led8 = 7;
int led9 = 8;
int led10 = 9;
int led11 = 10;
int led12 = 11;
int led13 = 12;
int led14 = 13;



//Arduino 4 Effect code:-

#define t   30
#define t1  20
#define t2  100
#define t3  50

void setup() {
  // set up pins 0 to 13 as outputs
  for (int i = 0; i <= 13; i++) {
    pinMode(i, OUTPUT);
  }
  for(int i=0;i<=13;i++){
  digitalWrite(i, LOW);}
}
/////////////////////////////////////////////////////////////////////////////////Effect 1
void loop(){

 effect_1();
 effect_1();

 effect_2();
 effect_2();

 effect_3();
 effect_3();

 effect_4();
 effect_4();


  }
//left to right and right to left
void effect_1()
{
for(int i=0; i<=13; i++){
digitalWrite(i, HIGH);
delay(t1);
digitalWrite(i+1, HIGH);
delay(t1);
digitalWrite(i+2, HIGH);
delay(t1);
digitalWrite(i, LOW);
delay(t1);
digitalWrite(i+1, LOW);
delay(t1);
}
for(int i=13; i>=0; i--){
digitalWrite(i, HIGH);
delay(t1);
digitalWrite(i-1, HIGH);
delay(t1);
digitalWrite(i-2, HIGH);
delay(t1);
digitalWrite(i, LOW);
delay(t1);
digitalWrite(i-1, LOW);
delay(t1);
}
}
/////////////////////////////////////////////////////////////////////////////////Effect 2
void effect_2()
{
int count = 13; // keeps track of second LED movement

  // move first LED from left to right and second from right to left
  for (int i = 0; i <=13; i++) {
    clear();
    digitalWrite(i, HIGH);      // chaser 1
    digitalWrite(count, HIGH); // chaser 2
    count--;
    // stop LEDs from appearing to stand still in the middle
    if (count != 8) {
      delay(t2);
    }
  }

  // move first LED from right to left and second LED from left to right
  for (int i = 13; i >=0; i--) {
    clear();
    digitalWrite(i, HIGH);      // chaser 1
    digitalWrite(count, HIGH); // chaser 2
    count++;
    // stop LEDs from appearing to stand still in the middle
    if (count != 8) {
      delay(t2);
    }
  }
}

////////////////////////////////////////////////////////////////////////////////Effect 3

void effect_3()
{
for(int i=0; i<14; i++){
  digitalWrite(i, HIGH);
  delay(t3);
}
for(int i=0; i<14; i++){
  digitalWrite(i, LOW);
  delay(t3);
}


for(int i = 14; i>=0; i--){
  digitalWrite(i, HIGH);
  delay(t3);
}
for(int i = 14; i>=0; i--){
  digitalWrite(i, LOW);
  delay(t3);
}
}
  ///////////////////////////////////////////////////////////////////////////Effect 4
void effect_4()
{
for(int j = 0; j <= 13; j++){
  digitalWrite(j, HIGH);
  delay(t2);
  j=j+1;
  }
for(int j = 0; j <= 13; j++){
  digitalWrite(j, LOW);
  delay(t2);
  }

  for(int k = 13; k > 0; k--){
  digitalWrite(k, HIGH);
  delay(t2);
  k=k-1;
  }
for(int k = 13; k > 0; k--){
  digitalWrite(k, LOW);
  delay(t2);
  }
}

void clear(void)
{
  for (int i = 0; i <= 13; i++) {
    digitalWrite(i, LOW);
  }
}

Try here