// 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);
}
}
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