• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Can i put my code(Led on and Led off) into an array?

Q&A Forum › Category: ESP32 › Can i put my code(Led on and Led off) into an array?
0 Vote Up Vote Down
Merkeven Suico asked 3 years ago

// LED 1
int led1 = 32;
unsigned long currentMillis;
unsigned long previoustime = 0;
long led1off = 1000;
long led1on = 500;
int ledState1 = 0;
// LED 2
int led2 = 33;
unsigned long currentMillis2;
unsigned long previoustime2 = 0;
long led2off = 1500;
long led2on = 500;
int ledState2 = 0;
// LED3
int led3 = 27;
unsigned long currentMillis3;
unsigned long previoustime3 = 0;
long led3off = 3000;
long led3on = 3000;
int ledState3 = 0;
// LED 4
int led4 = 14;
unsigned long currentMillis4;
unsigned long previoustime4 = 0;
long led4off = 2000;
long led4on = 2500;
int ledState4 = 0;
// LED 5
int led5 = 26;
unsigned long currentMillis5;
unsigned long previoustime5 = 0;
long led5off = 3000;
long led5on = 3000;
int ledState5 = 0;
// LED 6
int led6 = 34;
unsigned long currentMillis6;
unsigned long previoustime6 = 0;
long led6off = 3000;
long led6on = 3000;
int ledState6 = 0;
// MUSIC 7
int led7 = 19;
unsigned long currentMillis7;
unsigned long previoustime7 = 0;
long led7off = 200000;
long led7on = 3000;
int ledState7 = 0;
// SOUND SENSOR
int MIC = 25;
int sensorMIC = 12;
const int thershold = 50;

void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(MIC, OUTPUT);
pinMode(sensorMIC, INPUT);
Serial.begin (9600);
  }
 
void loop(){
currentMillis = millis();
if (((currentMillis – previoustime) >= led1on) && (ledState1 ==1))
{
ledState1 = !ledState1;
digitalWrite(led1, ledState1);
previoustime = currentMillis;
}
else if (((currentMillis – previoustime) >= led1off) && (ledState1 == 0))
{
ledState1 = !ledState1;
digitalWrite(led1, ledState1);
previoustime = currentMillis;
}
// led2
{
currentMillis2 = millis();
if (((currentMillis2 – previoustime2) >= led2on) && (ledState2 == 1))
{
ledState2 = !ledState2;
digitalWrite(led2, ledState2);
previoustime2 = currentMillis2;
}
else if (((currentMillis2 – previoustime2) >= led2off) && (ledState2 == 0))
{
ledState2 = !ledState2;
digitalWrite(led2, ledState2);
previoustime2 = currentMillis2;
  }
}
// LED 3
{
currentMillis3 = millis();
if (((currentMillis3 – previoustime3) >= led3on) && (ledState3 == 1))
{
ledState3 = !ledState3;
digitalWrite(led3, ledState3);
previoustime3 = currentMillis3;
}
else if (((currentMillis3 – previoustime3) >= led3off) && (ledState3 == 0))
{
ledState3 = !ledState3;
digitalWrite(led3, ledState3);
previoustime3 = currentMillis3;
  }
}
// LED 4
{
currentMillis4 = millis();
if (((currentMillis4 – previoustime4) >= led4on) && (ledState4 == 1))
{
ledState4 = !ledState4;
digitalWrite(led4, ledState4);
previoustime4 = currentMillis4;
}
else if (((currentMillis4 – previoustime4) >= led4off) && (ledState4 == 0))
{
ledState4 = !ledState4;
digitalWrite(led4, ledState4);
previoustime4 = currentMillis4;
 }
}
// LED 5
{
currentMillis5 = millis();
if (((currentMillis5 – previoustime5) >= led5on) && (ledState5 == 1))
{
ledState5 = !ledState5;
digitalWrite(led5, ledState5);
previoustime5 = currentMillis5;
}
else if (((currentMillis5 – previoustime5) >= led5off) && (ledState5 == 0))
{
ledState5 = !ledState5;
digitalWrite(led5, ledState5);
previoustime5 = currentMillis5;
  }
}
// LED 6
{
currentMillis6 = millis();
if (((currentMillis6 – previoustime6) >= led6on) && (ledState6 == 1))
{
ledState6 = !ledState6;
digitalWrite(led6, ledState6);
previoustime6 = currentMillis6;
}
else if (((currentMillis6 – previoustime6) >= led6off) && (ledState6 == 0))
{
ledState6 = !ledState6;
digitalWrite(led6, ledState6);
previoustime6 = currentMillis6;
  }
}
// MUSIC
{
currentMillis7 = millis();
if (((currentMillis7 – previoustime7) >= led7on) && (ledState7 == 1))
{
ledState7 = !ledState7;
digitalWrite(led7, ledState7);
previoustime7 = currentMillis7;
}
else if (((currentMillis7 – previoustime7) >= led7off) && (ledState7 == 0))
{
ledState7 = !ledState7;
digitalWrite(led7, ledState7);
previoustime7 = currentMillis7;
  }
}
// SOUND SENSOR
  int Soundsens=analogRead(sensorMIC);
  if (Soundsens>=thershold) {
    digitalWrite(MIC, LOW);
    delay(100);
  }
  else{
    digitalWrite(MIC,HIGH);
    }    
  }

2 Answers
0 Vote Up Vote Down
Best Answer
Merkeven Suico answered 3 years ago

Hello Sara,

First of all thank you for your response.
the code is fine but its to long. I want to make it short code using array is it posible?
if its posible can  u give me an example please.
note: the on and off  timing should be editable.

Regards,
keven

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Can you provide more details? What’s exactly the issue?
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.