Problem with loops in sketch?

Hello. I have problem with software. IR control wont works , AVR reaciver signal from remote control, but nothing more.

//IR//
#include <IRremote.h>
#define irPin 11
IRrecv irrecv(irPin);
decode_results results;

//PRZYCISKI//
#define OPT1sw 18
#define OPT2sw 19
#define OPT3sw 20
#define COAXsw 21
#define USBsw 22
#define BTsw 23


//LEDy//
#define OPT1led 9
#define OPT2led 10
#define OPT3led 8
#define COAXled 31
#define USBled 14
#define BTled 30

//AK4118
#define IPS0 4
#define IPS1 3
#define ISOusb 0
#define MUXbt 1
#define MUXak 2


//RGB//
int redPin = 12;
int greenPin = 13;
int bluePin = 15;
//RGB//
int potPin_red = A7;
int potPin_green = A6;
int potPin_blue = A5;
//RGB//
int readValue_red;
int readValue_green;
int readValue_blue;
//RGB//
int writeValue_red;
int writeValue_green;
int writeValue_blue;



void setup()
{

  irrecv.enableIRIn();

  //PRZYCISKI//
  pinMode(OPT1sw, INPUT_PULLUP);
  pinMode(OPT2sw, INPUT_PULLUP);
  pinMode(OPT3sw, INPUT_PULLUP);
  pinMode(COAXsw, INPUT_PULLUP);
  pinMode(USBsw, INPUT_PULLUP);
  pinMode(BTsw, INPUT_PULLUP);

  //LEDy//
  pinMode(OPT1led, OUTPUT);
  pinMode(OPT2led, OUTPUT);
  pinMode(OPT3led, OUTPUT);
  pinMode(COAXled, OUTPUT);
  pinMode(USBled, OUTPUT);
  pinMode(BTled, OUTPUT);

  //MUXy//
  pinMode(MUXak, OUTPUT);
  pinMode(MUXbt, OUTPUT);

  //IZOLATOR//
  pinMode(ISOusb, OUTPUT);

  //AK4118//
  pinMode(IPS0, OUTPUT);
  pinMode(IPS1, OUTPUT);

  //RGB//
  pinMode(potPin_red, INPUT);
  pinMode(potPin_green, INPUT);
  pinMode(potPin_blue, INPUT);
  //RGB//
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);

  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, LOW);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, LOW);
  digitalWrite(USBled, HIGH);
  digitalWrite(OPT1led, LOW);
  digitalWrite(OPT2led, LOW);
  digitalWrite(OPT3led, LOW);
  digitalWrite(COAXled, LOW);
  digitalWrite(BTled, LOW);

}

void ledsoff()
{
  digitalWrite(OPT1led, LOW);
  digitalWrite(OPT2led, LOW);
  digitalWrite(OPT3led, LOW);
  analogWrite(COAXled, LOW);
  digitalWrite(USBled, LOW);
  analogWrite(BTled, LOW);
}



void opt1source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}


void opt2source()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}

void opt3source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}

void coaxsource()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
}

void btsource()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, HIGH);
  digitalWrite(MUXak, LOW);
}


void usbsource()
{
  digitalWrite(IPS0, HIGH);
  digitalWrite(IPS1, HIGH);
  digitalWrite(ISOusb, LOW);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, LOW);
}



void loop()
{

  //RGB//
  readValue_red = analogRead(potPin_red);
  readValue_green = analogRead(potPin_green);
  readValue_blue = analogRead(potPin_blue);
  //RGB//
  writeValue_red = (255. / 1023.) * readValue_red;
  writeValue_green = (255. / 1023.) * readValue_green;
  writeValue_blue = (255. / 1023.) * readValue_blue;
  //RGB//
  analogWrite(redPin, writeValue_red);
  analogWrite(greenPin, writeValue_green);
  analogWrite(bluePin, writeValue_blue);



  //ZRODLA//
  //OPT1//
  if (digitalRead(OPT1sw) == LOW)
  {
    ledsoff();
    opt1source();
    digitalWrite(OPT1led, HIGH);
  }

  //OPT2//
  if (digitalRead(OPT2sw) == LOW)
  {
    ledsoff();
    opt2source();
    digitalWrite(OPT2led, HIGH);
  }

  //OPT3//
  if (digitalRead(OPT3sw) == LOW)
  {
    ledsoff();
    opt3source();
    digitalWrite(OPT3led, HIGH);
  }

  //COAX//
  if (digitalRead(COAXsw) == LOW)
  {
    ledsoff();
    coaxsource();
    digitalWrite(COAXled, HIGH);
  }

  //BT//
  if (digitalRead(BTsw) == LOW)
  {
    ledsoff();
    btsource();
    digitalWrite(BTled, HIGH);
  }


  //USB//
  if (digitalRead(USBsw) == LOW)
  {
    ledsoff();
    usbsource();
    digitalWrite(USBled, HIGH);
  }

  

  if (irrecv.decode(&results))
  {
    switch (results.value)
    {
      case 0x801:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
   
      case 0x802:  // OPT2
        ledsoff();
        opt2source();
        digitalWrite(OPT2led, HIGH);
        break;
      case 0x02:  // OPT2
        ledsoff();
        opt2source();
        digitalWrite(OPT2led, HIGH);
        break;
        
      case 0x803:  // OPT3
        ledsoff();
        opt3source();
        digitalWrite(OPT3led, HIGH);
        break;
      case 0x03:  // OPT3
        ledsoff();
        opt3source();
        digitalWrite(OPT3led, HIGH);
        break;

      case 0x804:  // COAX
        ledsoff();
        coaxsource();
        digitalWrite(COAXled, HIGH);
        break;
      case 0x04:  // COAX
        ledsoff();
        coaxsource();
        digitalWrite(COAXled, HIGH);
        break;
        
      case 0x805:  // USB
        ledsoff();
        usbsource();
        digitalWrite(USBled, HIGH);
        break;
      case 0x05:  // USB
        ledsoff();
        usbsource();
        digitalWrite(USBled, HIGH);
        break;
        
      case 0x806:  // BT
        ledsoff();
        btsource();
        digitalWrite(BTled, HIGH);
        break;
      case 0x06:  // BT
        ledsoff();
        btsource();
        digitalWrite(BTled, HIGH);
        break;
        
    }
    irrecv.resume();
  }
  
}

Buttons works
IR wont works (I have checked hardware and all is ok). I think that problem is in void loops.

Atmega16, Sanguino pinout, 8MHz external, 3,3VDC, fuses are ok.

Maybe somone have idea whats wrong?

This test code for IR works without problem:

//IR//
#include <IRremote.h>
#define irPin 11
IRrecv irrecv(irPin);
decode_results results;

//LED
#define OPT1led 9

void setup()
{
  irrecv.enableIRIn();
    pinMode(OPT1led, OUTPUT);
  
}



void loop()
{
   if (irrecv.decode(&results))
  {
    switch (results.value)
    {
      case 0x801:  // OPT1
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        digitalWrite(OPT1led, LOW);
        break;
    }
    irrecv.resume();
  }
  
}

Regards

Nothing obvious sticks out as being wrong.

In this kind of situation, I start from the working code and add little pieces of the non-working code. Keep adding until it stops working. Now you know which piece is the problem, even if you don't know why yet.

Ok, part in void loop with RGB read/write makes IR not working..

  //RGB//
  readValue_red = analogRead(potPin_red);
  readValue_green = analogRead(potPin_green);
  readValue_blue = analogRead(potPin_blue);
  //RGB//
  writeValue_red = (255. / 1023.) * readValue_red;
  writeValue_green = (255. / 1023.) * readValue_green;
  writeValue_blue = (255. / 1023.) * readValue_blue;
  //RGB//
  analogWrite(redPin, writeValue_red);
  analogWrite(greenPin, writeValue_green);
  analogWrite(bluePin, writeValue_blue);

In ledsoff: are you sure about this analogWrite

  analogWrite(BTled, LOW);

Here, you should use the map function:

  writeValue_red = (255. / 1023.) * readValue_red;
  writeValue_green = (255. / 1023.) * readValue_green;
  writeValue_blue = (255. / 1023.) * readValue_blue;

Map function:

  writeValue_red   = map(readValue_red  ,0,1023,0,255);
  writeValue_green = map(readValue_green,0,1023,0,255);
  writeValue_blue  = map(readValue_blue ,0,1023,0,255);

yes, I know. I have 2 pins on analog (BT LED and COAX LED)

I will try with Your suggestions

Can you comment out this line:

analogWrite(bluePin, writeValue_blue);

and see if there's a difference?

Blackfin:
Can you comment out this line:

analogWrite(bluePin, writeValue_blue);

and see if there's a difference?

only one color is not working (IR also not working)

lesept:
In ledsoff: are you sure about this analogWrite

  analogWrite(BTled, LOW);

Here, you should use the map function:

  writeValue_red = (255. / 1023.) * readValue_red;

writeValue_green = (255. / 1023.) * readValue_green;
 writeValue_blue = (255. / 1023.) * readValue_blue;




Map function:


writeValue_red   = map(readValue_red  ,0,1023,0,255);
 writeValue_green = map(readValue_green,0,1023,0,255);
 writeValue_blue  = map(readValue_blue ,0,1023,0,255);

like this?

void loop()
{


  writeValue_red = (255. / 1023.) * readValue_red;
  writeValue_green = (255. / 1023.) * readValue_green;
  writeValue_blue = (255. / 1023.) * readValue_blue;

  writeValue_red   = map(readValue_red  ,0,1023,0,255);
  writeValue_green = map(readValue_green,0,1023,0,255);
  writeValue_blue  = map(readValue_blue ,0,1023,0,255);
  readValue_red = analogRead(potPin_red);
  readValue_green = analogRead(potPin_green);
  readValue_blue = analogRead(potPin_blue);
  //RGB//
  writeValue_red = (255. / 1023.) * readValue_red;
  writeValue_green = (255. / 1023.) * readValue_green;
  writeValue_blue = (255. / 1023.) * readValue_blue;
  //RGB//
  analogWrite(redPin, writeValue_red);
  analogWrite(greenPin, writeValue_green);
  analogWrite(bluePin, writeValue_blue);

What is wrong with a simple

  analogWrite(redPin,  analogRead(potPin_red) / 4);
  analogWrite(greenPin, analogRead(potPin_green) / 4);
  analogWrite(bluePin, analogRead(potPin_blue) / 4);

Have I missed something?

AWOL:
What is wrong with a simple

  analogWrite(redPin,  analogRead(potPin_red) / 4);

analogWrite(greenPin, analogRead(potPin_green) / 4);
  analogWrite(bluePin, analogRead(potPin_blue) / 4);




Have I missed something?

Thanks, I have forgot this simple code...

But still I have sometimes hang with IR control when I have "fun" with RGB (fun - testing new colors), I need to reset power suppply. With buttons works always, but IR is hanging after some testing RGB potentiometers.

gavron04:
Thanks, I have forgot this simple code...

But still I have sometimes hang with IR control when I have "fun" with RGB (fun - testing new colors), I need to reset power suppply. With buttons works always, but IR is hanging after some testing RGB potentiometers.

Curious: How close is the IR receiver to your RGB LEDs? Any chance you're getting interference from them?

Try covering the LEDs (or blocking them so they don't overheat) and try again.

RGB leds I have on other PCB, also IR TSOP4838 is on other PCB. (3 PCS - PCB with AVR ; PCB with RGB LEDs ; PCB with TSOP4838)

So only interference maybe is from nets on pcb from potentiometers to AVR - there are close to TSOP output signal

But when IR is hang (after some colors tests) I see that AVR is receiving signal (one led is blinkig/dims (not the LED which I wolud like drive from remote control) when I have press some button on remote control) but AVR cant do nothing with this signal (from IR).

gavron04:
RGB leds I have on other PCB, also IR TSOP4838 is on other PCB. (3 PCS - PCB with AVR ; PCB with RGB LEDs ; PCB with TSOP4838)

So only interference maybe is from nets on pcb from potentiometers to AVR - there are close to TSOP output signal

IR can bounce around a room, off walls, picture frames, hangings etc. The RGBs are PWMed (turned on and off at high speed.) It's possible the IR receiver is seeing the pulsed light from the RGBs and turning its gain all the way down so your actual IR signal goes undetected.

Cover the RGBs and try again.

The IR library uses a timer. The PWM functionality uses timers. If you are doing PWM output on the pins that use the same timer that the IR library uses, something isn't going to work.

Ok. I have tested with LEDs and without LEDs on oscilloscope (output pin of IR sensor) and I think that RGB (PWM) has no effect.

But..

I have question about external eeprom 24LC256 (I2C).

I have connected 24LC256 on my hardware (A1;2;3 = GND, address 0x50)

I would like to store in eeprom last input after pressed button (tact switch) or button pressed on IR remote control.

Now control by tact switch and IR are:

a) control input by button now I have:

 if (digitalRead(OPT1sw) == LOW)
  {
    ledsoff();
    opt1source();
    digitalWrite(OPT1led, HIGH);
  }

b) for remote control I have:

     case 0x801:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;

My idea:

int lastInput = 0 ;

void loop() {
lastInput = EEPROM.read(EEPROMaddress) ;

void opt1source()
{
  digitalWrite(IPS0, LOW);
  digitalWrite(IPS1, LOW);
  digitalWrite(ISOusb, HIGH);
  digitalWrite(MUXbt, LOW);
  digitalWrite(MUXak, HIGH);
  EEPROM.write(EEPROMaddress, lastInput);
}

its good way?

Or better is:

a) for tact switch:

 if (digitalRead(OPT1sw) == LOW)
  {
    ledsoff();
    opt1source();
    digitalWrite(OPT1led, HIGH);
 EEPROM.write(EEPROMaddress, lastInput);
  }

b) for IR:

     case 0x801:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
EEPROM.write(EEPROMaddress, lastInput);

which will be better? I need to put saves and read from eeprom for 6 Inputs (opt1;2;3, coax , bt , usb) (one of them after power loss)

Have a good day!

Regards

 if (digitalRead(OPT1sw) == LOW)
  {
    ledsoff();
    opt1source();
    digitalWrite(OPT1led, HIGH);
  }

There are 4 functions being called here. It is perfectly obvious what digitalRead() and digitalWrite() are doing, because the names make it perfectly clear, AND the names use camelCase.

I'm having a hard time figuring out what ledSoff() is doing.

And, opt1source is just a useless collection of letters that mean nothing.

It would be easier to offer advice on your code if one didn't need a code book to decipher it.

I'm having a hard time figuring out what ledSoff() is doing.

Its only switch all led OFF (leds for showing which input is now turn on)

digitalWrite(IPS0, LOW); - pin of LOW/HIGH state of input selector of AK4118
 digitalWrite(IPS1, LOW); - pin of LOW/HIGH state of input selector of AK4118
 digitalWrite(ISOusb, HIGH); - pin of LOW/HIGH state of ISO7640 which turn on/off USB input
 digitalWrite(MUXbt, LOW); - pin of LOW/HIGH state of MUX on PCB (mux which can turn on/off bluetooth)
 digitalWrite(MUXak, HIGH); - pin of LOW/HIGHG state of MUX on PCB (mux which can turn on/off AK4118)

So I need to put Low/High state to use OPT1;OPT2;OPT3;COAX;BT;USB for pins IPS0;IPS1;ISOusb;MUXbt;MUXak

IPS0 - 0;1;0;1
IPS1 - 0;1;1;0

so total 4 inputs of AK4118 (OPT1;OPT2;OPT3;COAX)

void ledsoff

void ledsoff()
{
  digitalWrite(OPT1led, LOW);
  digitalWrite(OPT2led, LOW);
  digitalWrite(OPT3led, LOW);
  analogWrite(COAXled, LOW);
  digitalWrite(USBled, LOW);
  analogWrite(BTled, LOW);
}

ledsoff() - its only for turn off all leds. OPT1led is on if I have input OPT1 turned ON, OPT2 led is on if I have input OPT2 turned ON ...

for example IR control:

press "1" on remote control I can switch input to OPT1 :

      case 0x801:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        ledsoff();
        opt1source();
        digitalWrite(OPT1led, HIGH);
        break;

press 1 on remote control > ledsoff (turn all leds of input to off) > opt1source (put low/high state on IPS0;IPS1;ISOusb;MUXbt;MUXak) > turn on OPT1led and thats all. Now I have OPT1 as input source

for example button switch:

//OPT2//
  if (digitalRead(OPT2sw) == LOW)
  {
    ledsoff();
    opt2source();
    digitalWrite(OPT2led, HIGH);
  }

press OPT2sw (tactswitch) > leds off (turn all led of input to off) > opt2source (put low/high state on IPS0;IPS1;ISOusb;MUXbt;MUXak) > turn on OPT2led and thats all. Now I have OPT2 as input source

Maybe now will be clear.

Its only switch all led OFF (leds for showing which input is now turn on)

My point was that it is hard to tell just what that function is supposed to do, because you used all lower case letters in the name. Now, if the function had been names ledsOff(), then it would have been more obvious that something got turned off. There IS a reason that most keyboards have two shift keys.

Maybe now will be clear.

No. source implies the opposite of destination, and implies that it would collect some data and return something. Not storing the return value doesn't make sense. Nor does using global variables unnecessarily.

I have used global variables like ledsoff ; opt1source ; opt2source ; opt3source ; coaxsource ; btsource ; usbsource because I need to use the same I/O configuration for remote control and button switches.

For example:

  1. With global variables for OPT1:
void opt1source()
{
 digitalWrite(IPS0, LOW);
 digitalWrite(IPS1, LOW);
 digitalWrite(ISOusb, HIGH);
 digitalWrite(MUXbt, LOW);
 digitalWrite(MUXak, HIGH);
}

void ledsoff()
{
 digitalWrite(OPT1led, LOW);
 digitalWrite(OPT2led, LOW);
 digitalWrite(OPT3led, LOW);
 analogWrite(COAXled, LOW);
 digitalWrite(USBled, LOW);
 analogWrite(BTled, LOW);
}

button switch:

//OPT1//
 if (digitalRead(OPT1sw) == LOW)
 {
   ledsoff();
   opt1source();
   digitalWrite(OPT1led, HIGH);
 }

remote control:

    case 0x801:  // OPT1
       ledsoff();
       opt1source();
       digitalWrite(OPT1led, HIGH);
       break;
     case 0x01:  // OPT1
       ledsoff();
       opt1source();
       digitalWrite(OPT1led, HIGH);
       break;
  1. Without global variables:

button switch:

//OPT1//
 if (digitalRead(OPT1sw) == LOW)
 {
 digitalWrite(OPT1led, LOW);
 digitalWrite(OPT2led, LOW);
 digitalWrite(OPT3led, LOW);
 analogWrite(COAXled, LOW);
 digitalWrite(USBled, LOW);
 analogWrite(BTled, LOW);
 digitalWrite(IPS0, LOW);
 digitalWrite(IPS1, LOW);
 digitalWrite(ISOusb, HIGH);
 digitalWrite(MUXbt, LOW);
 digitalWrite(MUXak, HIGH);
 digitalWrite(OPT1led, HIGH);
 }

remote control:

    case 0x801:  // OPT1
 digitalWrite(OPT1led, LOW);
 digitalWrite(OPT2led, LOW);
 digitalWrite(OPT3led, LOW);
 analogWrite(COAXled, LOW);
 digitalWrite(USBled, LOW);
 analogWrite(BTled, LOW);
 digitalWrite(IPS0, LOW);
 digitalWrite(IPS1, LOW);
 digitalWrite(ISOusb, HIGH);
 digitalWrite(MUXbt, LOW);
 digitalWrite(MUXak, HIGH);
 digitalWrite(OPT1led, HIGH);
       digitalWrite(OPT1led, HIGH);
       break;

     case 0x01:  // OPT1
 digitalWrite(OPT1led, LOW);
 digitalWrite(OPT2led, LOW);
 digitalWrite(OPT3led, LOW);
 analogWrite(COAXled, LOW);
 digitalWrite(USBled, LOW);
 analogWrite(BTled, LOW);
 digitalWrite(IPS0, LOW);
 digitalWrite(IPS1, LOW);
 digitalWrite(ISOusb, HIGH);
 digitalWrite(MUXbt, LOW);
 digitalWrite(MUXak, HIGH);
 digitalWrite(OPT1led, HIGH);
       digitalWrite(OPT1led, HIGH);
       break;

now You see different?

as you mentioned that i have two SHIFT keys on keyboard, i have tried to use them :slight_smile:

lets try:

//IR//
#include <IRremote.h>
#define irPin 11
IRrecv irrecv(irPin);
decode_results results;

//PRZYCISKI//
#define OPT1switchButtonPin 18
#define OPT2switchButtonPin 19
#define OPT3switchButtonPin 20
#define COAXswitchButtonPin 21
#define USBswitchButtonPin 22
#define BLUETOOTHswitchButtonPin 23


//LEDy//
#define OPT1ledStatusPin 9
#define OPT2ledStatusPin 10
#define OPT3ledStatusPin 8
#define COAXledStatusPin 31
#define USBledStatusPin 14
#define BLUETOOTHledStatusPin 30

//AK4118
#define IPS0Pin 4
#define IPS1Pin 3
#define muxAK4118Pin 2

//USB
#define ISO7640usbPin 0

//BLUETOOTH
#define muxBLUETOOTHPin 1



//RGB//
int redPin = 12;
int greenPin = 13;
int bluePin = 15;
//RGB//
int potPin_red = A7;
int potPin_green = A6;
int potPin_blue = A5;
//RGB//
int readValue_red;
int readValue_green;
int readValue_blue;
//RGB//
int writeValue_red;
int writeValue_green;
int writeValue_blue;



void setup()
{

  irrecv.enableIRIn();

  //PRZYCISKI//
  pinMode(OPT1switchButtonPin, INPUT_PULLUP);
  pinMode(OPT2switchButtonPin, INPUT_PULLUP);
  pinMode(OPT3switchButtonPin, INPUT_PULLUP);
  pinMode(COAXswitchButtonPin, INPUT_PULLUP);
  pinMode(USBswitchButtonPin, INPUT_PULLUP);
  pinMode(BLUETOOTHswitchButtonPin, INPUT_PULLUP);

  //LEDy//
  pinMode(OPT1ledStatusPin, OUTPUT);
  pinMode(OPT2ledStatusPin, OUTPUT);
  pinMode(OPT3ledStatusPin, OUTPUT);
  pinMode(COAXledStatusPin, OUTPUT);
  pinMode(USBledStatusPin, OUTPUT);
  pinMode(BLUETOOTHledStatusPin, OUTPUT);

  //MUXy//
  pinMode(muxBLUETOOTHPin, OUTPUT);
  pinMode(muxAK4118Pin, OUTPUT);
  

  //IZOLATOR//
  pinMode(ISO7640usbPin, OUTPUT);

  //AK4118//
  pinMode(IPS0Pin, OUTPUT);
  pinMode(IPS1Pin, OUTPUT);

  //RGB//
  pinMode(potPin_red, INPUT);
  pinMode(potPin_green, INPUT);
  pinMode(potPin_blue, INPUT);
  //RGB//
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);

  digitalWrite(IPS0Pin, HIGH);
  digitalWrite(IPS1Pin, HIGH);
  digitalWrite(ISO7640usbPin, LOW);
  digitalWrite(muxBLUETOOTHPin, LOW);
  digitalWrite(muxAK4118Pin, LOW);
  digitalWrite(USBledStatusPin, HIGH);
  digitalWrite(OPT1ledStatusPin, LOW);
  digitalWrite(OPT2ledStatusPin, LOW);
  digitalWrite(OPT3ledStatusPin, LOW);
  digitalWrite(COAXledStatusPin, LOW);
  digitalWrite(BLUETOOTHledStatusPin, LOW);



}

void ledsStatusOff()
{
  digitalWrite(OPT1ledStatusPin, LOW);
  digitalWrite(OPT2ledStatusPin, LOW);
  digitalWrite(OPT3ledStatusPin, LOW);
  analogWrite(COAXledStatusPin, LOW);
  digitalWrite(USBledStatusPin, LOW);
  analogWrite(BLUETOOTHledStatusPin, LOW);
}



void changeTo-OPT1()
{
  digitalWrite(IPS0Pin, LOW);
  digitalWrite(IPS1Pin, LOW);
  digitalWrite(ISO7640usbPin, HIGH);
  digitalWrite(muxBLUETOOTHPin, LOW);
  digitalWrite(muxAK4118Pin, HIGH);
}


void changeTo-OPT2()
{
  digitalWrite(IPS0Pin, HIGH);
  digitalWrite(IPS1Pin, LOW);
  digitalWrite(ISO7640usbPin, HIGH);
  digitalWrite(muxBLUETOOTHPin, LOW);
  digitalWrite(muxAK4118Pin, HIGH);
}

void changeTo-OPT3()
{
  digitalWrite(IPS0Pin, LOW);
  digitalWrite(IPS1Pin, HIGH);
  digitalWrite(ISO7640usbPin, HIGH);
  digitalWrite(muxBLUETOOTHPin, LOW);
  digitalWrite(muxAK4118Pin, HIGH);
}

void changeTo-COAX()
{
  digitalWrite(IPS0Pin, HIGH);
  digitalWrite(IPS1Pin, HIGH);
  digitalWrite(ISO7640usbPin, HIGH);
  digitalWrite(muxBLUETOOTHPin, LOW);
  digitalWrite(muxAK4118Pin, HIGH);
}

void changeTo-BLUETOOTH()
{
  digitalWrite(IPS0Pin, HIGH);
  digitalWrite(IPS1Pin, HIGH);
  digitalWrite(ISO7640usbPin, HIGH);
  digitalWrite(muxBLUETOOTHPin, HIGH);
  digitalWrite(muxAK4118Pin, LOW);
}


void changeTo-USB()
{
  digitalWrite(IPS0Pin, HIGH);
  digitalWrite(IPS1Pin, HIGH);
  digitalWrite(ISO7640usbPin, LOW);
  digitalWrite(muxBLUETOOTHPin, LOW);
  digitalWrite(muxAK4118Pin, LOW);
}



void loop()
{

  //RGB//
  analogWrite(redPin,  analogRead(potPin_red) / 4);
  analogWrite(greenPin, analogRead(potPin_green) / 4);
  analogWrite(bluePin, analogRead(potPin_blue) / 4);



  //ZRODLA//
  //OPT1//
  if (digitalRead(OPT1sw) == LOW)
  {
    ledsStatusOff();
    changeTo-OPT1();
    digitalWrite(OPT1led, HIGH);
  }

  //OPT2//
  if (digitalRead(OPT2sw) == LOW)
  {
    ledsStatusOff();
    changeTo-OPT2();
    digitalWrite(OPT2led, HIGH);
  }

  //OPT3//
  if (digitalRead(OPT3sw) == LOW)
  {
    ledsStatusOff();
    changeTo-OPT3();
    digitalWrite(OPT3led, HIGH);
  }

  //COAX//
  if (digitalRead(COAXsw) == LOW)
  {
    ledsStatusOff();
    changeTo-COAX();
    digitalWrite(COAXled, HIGH);
  }

  //BT//
  if (digitalRead(BTsw) == LOW)
  {
    ledsStatusOff();
    changeTo-BLUETOOTH();
    digitalWrite(BTled, HIGH);
  }


  //USB//
  if (digitalRead(USBsw) == LOW)
  {
    ledsStatusOff();
    changeTo-USB();
    digitalWrite(USBled, HIGH);
  }

  

  if (irrecv.decode(&results))
  {
    switch (results.value)
    {
      case 0x801:  // OPT1
        ledsStatusOff();
        changeTo-OPT1();
        digitalWrite(OPT1led, HIGH);
        break;
      case 0x01:  // OPT1
        ledsStatusOff();
        changeTo-OPT1();
        digitalWrite(OPT1led, HIGH);
        break;
   
      case 0x802:  // OPT2
        ledsStatusOff();
        changeTo-OPT2();
        digitalWrite(OPT2led, HIGH);
        break;
      case 0x02:  // OPT2
        ledsStatusOff();
        changeTo-OPT2();
        digitalWrite(OPT2led, HIGH);
        break;
        
      case 0x803:  // OPT3
        ledsStatusOff();
        changeTo-OPT3();
        digitalWrite(OPT3led, HIGH);
        break;
      case 0x03:  // OPT3
        ledsStatusOff();
        changeTo-OPT3();
        digitalWrite(OPT3led, HIGH);
        break;

      case 0x804:  // COAX
        ledsStatusOff();
        changeTo-COAX();
        digitalWrite(COAXled, HIGH);
        break;
      case 0x04:  // COAX
        ledsStatusOff();
        changeTo-COAX();
        digitalWrite(COAXled, HIGH);
        break;
        
      case 0x805:  // USB
        ledsStatusOff();
        changeTo-USB();
        digitalWrite(USBled, HIGH);
        break;
      case 0x05:  // USB
        ledsStatusOff();
        changeTo-USB();
        digitalWrite(USBled, HIGH);
        break;
        
      case 0x806:  // BT
        ledsStatusOff();
        changeTo-BLUETOOTH();
        digitalWrite(BTled, HIGH);
        break;
      case 0x06:  // BT
        ledsStatusOff();
        changeTo-BLUETOOTH();
        digitalWrite(BTled, HIGH);
        break;
        
    }
    irrecv.resume();
  }
  
}