Control of 2 LED effects with 9 diodes controlled by a microphone using an IR controller //it works// the code is below//

Hello
I'm a programming beginner and I'm trying to learn from an arduino textbook and the internet.

I decided to make a stroboscopic light for the party controlled by music (microphone) with multiple effects that I switch between them using the IR controller, but I'm stuck on switching using the controller.

I found some code on google that can be used to switch the lighting effects, but the code does not work as I need it to.
I expected that when I pressed the IR_BUTTON_1 button, the flashing would turn on from the center to the edge
and when pressing the IR_BUTTON_2 button again from the edge to the center, and the LEDs will switch according to the sound intensity, but it does not work.
the effects of the session switch on but do not respond to the sound.
The microphone starts to respond to the sound and the LED flashes as soon as I hold down button 1 or 2

Thanks for all the help and advice, hopefully the solution will be simple

(sorry for the translation, google translator, CZ>EN)

#include <IRremote.h>                        
#define IR_RECEIVE_PIN 13
#define soundpin A0
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
#define LED9 10
#define IR_BUTTON_1 12
#define IR_BUTTON_2 24

int sound;


void setup() 
{
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN);
  pinMode(soundpin, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
}
void loop() 
  {
  if (IrReceiver.decode()) {
    IrReceiver.resume();
    int command = IrReceiver.decodedIRData.command;
    switch (command) {
      case IR_BUTTON_1:

sound=analogRead(soundpin);   // this samples the sound constantly
if((sound)>250)
 {
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);
}  
else if((sound)>200)
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,LOW);
}
else if((sound)>150)
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,HIGH);
digitalWrite(LED8,LOW);
digitalWrite(LED9,LOW);
}
else if((sound)>100)
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,LOW);
digitalWrite(LED8,LOW);
digitalWrite(LED9,LOW);
}
else if((sound)>50)
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
digitalWrite(LED8,LOW);
digitalWrite(LED9,LOW);
}
else
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
digitalWrite(LED8,LOW);
digitalWrite(LED9,LOW);
}
Serial.println(soundpin);
delay(25);
break;


      case IR_BUTTON_2:

sound=analogRead(soundpin);   // this samples the sound constantly
if((sound)>250)
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);
}  
else if((sound)>200)
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,LOW);
digitalWrite(LED6,HIGH);
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);
}
else if((sound)>150)
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);
}
else if((sound)>100)
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);
}
else if((sound)>50)
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
digitalWrite(LED8,LOW);
digitalWrite(LED9,HIGH);
}
else
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);
digitalWrite(LED8,LOW);
digitalWrite(LED9,LOW);
}
Serial.println(soundpin);
delay(25);
break;
}
}
}

With your code open in the Arduino IDE press Control-T and this will autoformat your code. That will line all the blocks up so you can easily see what's nested where. In this way you will see that the part of the code with all the if statements that react to sound are inside the if statement for there being a signal from the remote. So that code can only run if there is a signal from the remote.

Instead, the if statement that gets the command from the remote should just save the command into a variable and then the switch case part should be outside of that if statement so it runs on every loop whether there is a new signal from the remote or not.

thank you, now I finally know where the problem is, but I have no idea how to solve it xD

Please can you help, I don't know how to deal with this at all :cold_sweat:

Who wrote that code?

me and google :see_no_evil:

In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.

This line (twice):

Serial.println(soundpin); // print the ATMEGA328P pin number

should be this (twice):

Serial.println(sound); // print the value of the input

The way your code works is this:

When the A0 input is < 50, and B1 is pressed, no LEDs light
When the A0 input is < 50, and B2 is pressed, no LEDs lights

When the A0 input is 50 - 100, and B1 is pressed, the inside 1 LED lights
When the A0 input is 50 - 100, and B2 is pressed, the outside 1 LED lights (two LEDs)

When the A0 input is 100 - 150, and B1 is pressed, the inside 3 LEDs light
When the A0 input is 100 - 150, and B2 is pressed, the outside 2 LEDs lights (four LEDs)

When the A0 input is 150 - 200, and B1 is pressed, the inside 5 LEDs light
When the A0 input is 150 - 200, and B2 is pressed, the outside 3 LEDs lights (six LEDs)

When the A0 input is 200 - 250, and B1 is pressed, the inside 7 LEDs light
When the A0 input is 200 - 250, and B2 is pressed, the outside 4 LEDs lights (eight LEDs)

When the A0 input is > 250, and B1 is pressed, all 8 LEDs light
When the A0 input is > 250, and B2 is pressed, all 8 LEDs lights

BUT... the ADC on pin A0 has a range of 0 to 1023 bits... and you are only using lighting the LEDs with a 200 bit range (50 to 250)... so in this simulator, I changed your code to use the 1023 bit range split into six groups (0 - 170, 170 - 340, 340 - 510, 510 - 680, 680 - 850, 850 - 1023):

Files for WOKWI.COM

sketch.ino
// https://forum.arduino.cc/t/led-control-using-an-ir-controller/1147015/
// https://wokwi.com/projects/370016583076653057

#include <IRremote.h>
#define soundpin A0

// int LED [] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
#define LED9 10
#define IR_BUTTON_2 24 // IR remote button "2"
#define IR_BUTTON_1 48 // IR remote button "1" for the simulation (was 12)
#define IR_RECEIVE_PIN 13

int sound;

void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN);
  pinMode(soundpin, INPUT);

  // for (int i; i < 10; i++) {
  //   pinMode(LED[i], OUTPUT);
  // }

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
}
void loop()
{
  // if (IrReceiver.decode()) { // remove this line from the IF(), but leave the command
    IrReceiver.decode();
    IrReceiver.resume();
    int command = IrReceiver.decodedIRData.command;
    switch (command) {
      case IR_BUTTON_1:
        sound = analogRead(soundpin); // this samples the sound constantly
        // if ((sound) > 250)
        if (sound >= 850) // eight LEDs
        {
          digitalWrite(LED1, HIGH);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, HIGH);
          digitalWrite(LED6, HIGH);
          digitalWrite(LED7, HIGH);
          digitalWrite(LED8, HIGH);
          digitalWrite(LED9, HIGH);
        }
        // else if ((sound) > 200)
        else if ((sound >= 680) && ( sound < 850))
        {
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, HIGH);
          digitalWrite(LED6, HIGH);
          digitalWrite(LED7, HIGH);
          digitalWrite(LED8, HIGH);
          digitalWrite(LED9, LOW);
        }
        // else if ((sound) > 150)
        else if ((sound >= 510) && (sound < 680))
        {
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, LOW);
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, HIGH);
          digitalWrite(LED6, HIGH);
          digitalWrite(LED7, HIGH);
          digitalWrite(LED8, LOW);
          digitalWrite(LED9, LOW);
        }
        // else if ((sound) > 100)
        else if ((sound >= 340) && (sound < 510))
        {
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, LOW);
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, HIGH);
          digitalWrite(LED6, HIGH);
          digitalWrite(LED7, LOW);
          digitalWrite(LED8, LOW);
          digitalWrite(LED9, LOW);
        }
        // else if ((sound) > 50)
        else if ((sound >= 170) && (sound < 340))
        {
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, LOW);
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, HIGH);
          digitalWrite(LED6, LOW);
          digitalWrite(LED7, LOW);
          digitalWrite(LED8, LOW);
          digitalWrite(LED9, LOW);
        }
        else
        {
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, LOW);
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          digitalWrite(LED6, LOW);
          digitalWrite(LED7, LOW);
          digitalWrite(LED8, LOW);
          digitalWrite(LED9, LOW);
        }
        //  Serial.println(soundpin); // print ATMEGA328P pin number
        Serial.println(sound);  // print value read on "sound" pin
        delay(25);
        break;


      case IR_BUTTON_2:
        sound = analogRead(soundpin); // this samples the sound constantly
        // if ((sound) > 250)
        if ((sound >= 850))
        {
          digitalWrite(LED1, HIGH);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, HIGH);
          digitalWrite(LED6, HIGH);
          digitalWrite(LED7, HIGH);
          digitalWrite(LED8, HIGH);
          digitalWrite(LED9, HIGH);
        }
        // else if ((sound) > 200)
        else if ((sound >= 680) && (sound < 850))
        {
          digitalWrite(LED1, HIGH);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, LOW);
          digitalWrite(LED6, HIGH);
          digitalWrite(LED7, HIGH);
          digitalWrite(LED8, HIGH);
          digitalWrite(LED9, HIGH);
        }
        // else if ((sound) > 150)
        else if ((sound >= 510) && (sound < 680))
        {
          digitalWrite(LED1, HIGH);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          digitalWrite(LED6, LOW);
          digitalWrite(LED7, HIGH);
          digitalWrite(LED8, HIGH);
          digitalWrite(LED9, HIGH);
        }
        // else if ((sound) > 100)
        else if ((sound >= 340) && (sound < 510))
        {
          digitalWrite(LED1, HIGH);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          digitalWrite(LED6, LOW);
          digitalWrite(LED7, LOW);
          digitalWrite(LED8, HIGH);
          digitalWrite(LED9, HIGH);
        }
        // else if ((sound) > 50)
        else if ((sound >= 170) && (sound < 340))
        {
          digitalWrite(LED1, HIGH);
          digitalWrite(LED2, LOW);
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          digitalWrite(LED6, LOW);
          digitalWrite(LED7, LOW);
          digitalWrite(LED8, LOW);
          digitalWrite(LED9, HIGH);
        }
        else
        {
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, LOW);
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          digitalWrite(LED6, LOW);
          digitalWrite(LED7, LOW);
          digitalWrite(LED8, LOW);
          digitalWrite(LED9, LOW);
        }
        //  Serial.println(soundpin); // print ATMEGA328P pin number
        // Serial.println(sound);  // print value read on "sound" pin - slows responsiveness
        delay(25);
        break;
    }
  // }
}

float sinewave() {
     for (float i = 0.0; i <= 2 * PI; i += 0.1) {
      Serial.println(sin(i));
   }
}

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 24, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-led-bar-graph",
      "id": "bargraph1",
      "top": -110.2,
      "left": 53,
      "rotate": 270,
      "attrs": { "color": "#ff00ff" }
    },
    { "type": "wokwi-ir-receiver", "id": "ir1", "top": -68.55, "left": -59.38, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -68.5, "left": 143.8, "attrs": {} },
    { "type": "wokwi-ir-remote", "id": "remote1", "top": -144, "left": -220.8, "attrs": {} },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -14.4,
      "left": 76.25,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -14.4,
      "left": 9.05,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": -14.4,
      "left": 18.65,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -14.4,
      "left": 28.25,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r5",
      "top": -14.4,
      "left": 37.85,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r6",
      "top": -14.4,
      "left": 47.45,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r7",
      "top": -14.4,
      "left": 57.05,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r8",
      "top": -14.4,
      "left": 66.65,
      "rotate": 90,
      "attrs": { "value": "330" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r9",
      "top": -14.4,
      "left": 85.85,
      "rotate": 90,
      "attrs": { "value": "330" }
    }
  ],
  "connections": [
    [ "bargraph1:C1", "bargraph1:C2", "green", [ "v0" ] ],
    [ "bargraph1:C2", "bargraph1:C3", "green", [ "v0" ] ],
    [ "bargraph1:C3", "bargraph1:C4", "green", [ "v0" ] ],
    [ "bargraph1:C4", "bargraph1:C5", "green", [ "v0" ] ],
    [ "bargraph1:C5", "bargraph1:C6", "green", [ "v0" ] ],
    [ "bargraph1:C6", "bargraph1:C7", "green", [ "v0" ] ],
    [ "bargraph1:C7", "bargraph1:C8", "green", [ "v0" ] ],
    [ "bargraph1:C8", "bargraph1:C9", "green", [ "v0" ] ],
    [ "bargraph1:C9", "bargraph1:C10", "green", [ "v0" ] ],
    [ "nano:GND.2", "bargraph1:C10", "black", [ "v0" ] ],
    [ "ir1:DAT", "nano:13", "green", [ "v86.4", "h38.48" ] ],
    [ "ir1:VCC", "nano:5V", "red", [ "v97.79", "h148.07" ] ],
    [ "ir1:GND", "nano:GND.1", "black", [ "v105.79", "h176.87" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v124.8", "h-28.8" ] ],
    [ "pot1:SIG", "nano:A0", "green", [ "v102.79", "h-135.26" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v115.2", "h-68" ] ],
    [ "bargraph1:A2", "r2:1", "green", [ "v0" ] ],
    [ "r3:1", "bargraph1:A3", "green", [ "h0" ] ],
    [ "bargraph1:A4", "r4:1", "green", [ "v0" ] ],
    [ "r5:1", "bargraph1:A5", "green", [ "h0" ] ],
    [ "bargraph1:A6", "r6:1", "green", [ "v0" ] ],
    [ "r7:1", "bargraph1:A7", "green", [ "h0" ] ],
    [ "bargraph1:A8", "r8:1", "green", [ "v0" ] ],
    [ "r1:1", "bargraph1:A9", "green", [ "h0" ] ],
    [ "bargraph1:A10", "r9:1", "green", [ "v0" ] ],
    [ "r9:2", "nano:2", "green", [ "h0" ] ],
    [ "r1:2", "nano:3", "green", [ "h0" ] ],
    [ "r8:2", "nano:4", "green", [ "h0" ] ],
    [ "r7:2", "nano:5", "green", [ "h0" ] ],
    [ "r6:2", "nano:6", "green", [ "h0" ] ],
    [ "r5:2", "nano:7", "green", [ "h0" ] ],
    [ "r4:2", "nano:8", "green", [ "h0" ] ],
    [ "r3:2", "nano:9", "green", [ "h0" ] ],
    [ "r2:2", "nano:10", "green", [ "h0" ] ]
  ],
  "dependencies": {}
}

thank you for answer. Yes, I know about the range up to 1023 bits. I have set small values ​​for testing after listening to music from a mobile phone.
(I don't know if the microphone is defective or not, I expected it to be more sensitive)

anyway i tried your code and it works same as mine.
The LEDs react to music only if the button on the controller is permanently on

Okay. This can be solved by constantly monitoring the IR remote... right now, you only react to the audio when the button is pressed by using an if() condition to read the IR remote. Try taking that IR remote function out of the if() condition. Don't worry about breaking the code... you have a fallback right here.

I went through a lot of IR coding sites but couldn't find anything that worked as I needed.
I understand what you write, but I have no idea how to remove the IR from the IF() function.
I'm a beginner in coding, I'm still looking around and I would be very grateful if you could help me solve this

Follow these instructions.

Look at the code. Look at the if statement. Match up the { and } after it. They surround the block of code that will be controlled by your if statement.

See in that block of code where the IR code is. Use your mouse to highlight it. On your keyboard press Control-x. That will cut out that code.

Now scroll down to right after the closing } brace on the block controlled by the if statement. Put your cursor on the line after that }. Press Control-v and it will paste the code you just cut into that place.

It is now outside of the if statement.

@razercz77 - Yes, what Delta_G says...

[quote="Delta_G, post:14, topic:1147015"]Look at the code. Look at the if statement. Match up the { and } after it. They surround the block of code that will be controlled by your if statement.

See in that block of code where the IR code is. Use your mouse to highlight it. On your keyboard press Control-x. That will cut out that code.

Now scroll down to right after the closing } brace on the block controlled by the if statement. Put your cursor on the line after that }. Press Control-v and it will paste the code you just cut into that place.
[/quote]

You still need the command to read the receiver on its own line

    IrReceiver.decode(); //  leave the command, without the "if()" condition

When I try to fix a program, I like to leave the old/bad lines in place (but commented-out) until I can verify the changes were good. Post your results here, good or bad.

The result will be: You must choose (one touch) 1 or 2 to tell the program to use "outside-in" LED movement or "inside-out" LED movement. The LEDs will not react until you select 1 or 2 (one touch).

1 Like

only now I understood that you mean remove it from the IF conditions.
Remove the BOLD TEXT:

if ( IrReceiver.decode()) { ...CODE... }

thanks guys for helping me solve my problem, I couldn't have done it without you.
It took me a long time to understand what you wanted from me, but I succeeded mainly thanks to
@ xfpd

and too @ Delta_G

:heart: :heart: :heart:

functional code for IR control of 2 or more effects with 9 LEDs:

#include <IRremote.h>
#define IR_RECEIVE_PIN 12
#define soundpin A0
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
#define LED9 10
#define IR_BUTTON_1 12
#define IR_BUTTON_2 24

int sound;


void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN);
  pinMode(soundpin, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
}
void loop() {
  IrReceiver.decode();
  IrReceiver.resume();
  int command = IrReceiver.decodedIRData.command;
  switch (command) {
    case IR_BUTTON_1:

      sound = analogRead(soundpin);  // this samples the sound constantly
      if ((sound) > 250) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      } else if ((sound) > 200) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, LOW);
      } else if ((sound) > 150) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      } else if ((sound) > 100) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      } else if ((sound) > 50) {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      } else {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      }
      Serial.println(soundpin);
      delay(25);
      break;


    case IR_BUTTON_2:

      sound = analogRead(soundpin);  // this samples the sound constantly
      if ((sound) > 250) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, HIGH);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      } else if ((sound) > 200) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, HIGH);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      } else if ((sound) > 150) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED7, HIGH);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      } else if ((sound) > 100) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, HIGH);
        digitalWrite(LED9, HIGH);
      } else if ((sound) > 50) {
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, HIGH);
      } else {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        digitalWrite(LED5, LOW);
        digitalWrite(LED6, LOW);
        digitalWrite(LED7, LOW);
        digitalWrite(LED8, LOW);
        digitalWrite(LED9, LOW);
      }
      Serial.println(soundpin);
      delay(25);
      break;
  }
}

1 Like

Very good!

In post #7 a suggestion to simplify your code by using arrays to represent repetition was made. For example, this:

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);

could be replaced by this:

   for (int i; i < 9; i++) {
     pinMode(LED[i], OUTPUT);
   }

So, look up "arrays" and "structures" (or "structs")... and see if you can simplify your code. These styles will help you in many ways.

ouu thanks I'll check it out, the style looks interesting
but I use this one because I'm more familiar with it than yours :sweat_smile: :rofl:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.