Problem using IR transmitter and arduno nano for a universal remote

Found a sample of this ir remote code that i plan on using to turn on off my tv and changing the different sources on my speakers. plan on using this with alexa, (using a esp to turn on or off a pin on the nano that activates the IR Transmitter.
the problem that im having is that its sending the signal and it working but then it gets into this loop where the IR transmitter just turns on and off at random. Any answers or help is greatly appreciated. :slight_smile:

heres the sketch

 int IRledPin =  2;    // LED connected to digital pin 13
 const int buttonPin = 10;
 const int tvSound = 11;
 const int auxSound = 8;
 const int tvOnOff = 7;
 const int mute = 4;
 int previousButtonState = 0;
 int buttonState = 0;

 // The setup() method runs once, when the sketch starts

 void setup()   {               
   // initialize the IR digital pin as an output:
   pinMode(IRledPin, OUTPUT);
   pinMode(buttonPin, INPUT); 
   pinMode(tvSound, INPUT);
   pinMode(auxSound, INPUT);
   pinMode(tvOnOff, INPUT);
   pinMode(mute, INPUT);   

   Serial.begin(9600);
 }

 void loop()                    
 {
  {
   buttonState = digitalRead(buttonPin);
   if(buttonState != previousButtonState){
    if(buttonState == HIGH){ //Sends the code every time the button is pressed.
      SendChannelUpCode();
    }
   }
   previousButtonState = buttonState;
  }
  {
    buttonState = digitalRead(tvSound);
   if(buttonState != previousButtonState){
    if(buttonState == HIGH){ //Sends the code every time the button is pressed.
      SendtvSoundCode();
    }
   }
   previousButtonState = buttonState;
  }

{
    buttonState = digitalRead(auxSound);
   if(buttonState != previousButtonState){
    if(buttonState == HIGH){ //Sends the code every time the button is pressed.
      SendauxSoundCode();
    }
   }
   previousButtonState = buttonState;
 }
 }

 // This procedure sends a 38KHz pulse to the IRledPin
 // for a certain # of microseconds. We'll use this whenever we need to send codes
 void pulseIR(long microsecs) {
   // we'll count down from the number of microseconds we are told to wait

   cli();  // this turns off any background interrupts

   while (microsecs > 0) {
     // 38 kHz is about 13 microseconds high and 13 microseconds low
    digitalWrite(IRledPin, HIGH);  // this takes about 3 microseconds to happen
    delayMicroseconds(10);         // hang out for 10 microseconds
    digitalWrite(IRledPin, LOW);   // this also takes about 3 microseconds
    delayMicroseconds(10);         // hang out for 10 microseconds

    // so 26 microseconds altogether
    microsecs -= 26;
   }

   sei();  // this turns them back on
 }

 void SendChannelUpCode() {
   // This is the code for the CHANNEL + for the TV COMCAST
  
   delayMicroseconds(8736);      //Time off (LEFT column)       
   pulseIR(8700);                //Time on (RIGHT column)    <-------DO NOT MIX THESE UP
   delayMicroseconds(4380);
   pulseIR(520);
   delayMicroseconds(500);
   pulseIR(580);
   delayMicroseconds(520);
   pulseIR(540);
   delayMicroseconds(1640);
   pulseIR(540);
   delayMicroseconds(540);
   pulseIR(560);
   delayMicroseconds(520);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(500);
   delayMicroseconds(560);
   pulseIR(500);
   delayMicroseconds(580);
   pulseIR(520);
   delayMicroseconds(1660);
   pulseIR(520);
   delayMicroseconds(1620);
   pulseIR(580);
   delayMicroseconds(540);
   pulseIR(520);
   delayMicroseconds(1660);
   pulseIR(540);
   delayMicroseconds(1680);
   pulseIR(500);
   delayMicroseconds(1660);
   pulseIR(520);
   delayMicroseconds(1660);
   pulseIR(540);
   delayMicroseconds(1620);
   pulseIR(540);
   delayMicroseconds(580);
   pulseIR(520);
   delayMicroseconds(520);
   pulseIR(560);
   delayMicroseconds(500);
   pulseIR(560);
   delayMicroseconds(1660);
   pulseIR(560);
   delayMicroseconds(560);
   pulseIR(520);
   delayMicroseconds(540);
   pulseIR(520);
   delayMicroseconds(560);
   pulseIR(520);
   delayMicroseconds(580);
   pulseIR(520);
   delayMicroseconds(1620);
   pulseIR(540);
   delayMicroseconds(1680);
   pulseIR(520);
   delayMicroseconds(1660);
   pulseIR(520);
   delayMicroseconds(520);
   pulseIR(560);
   delayMicroseconds(1620);
   pulseIR(560);
   delayMicroseconds(1660);
   pulseIR(540);
   delayMicroseconds(1660);
   pulseIR(520);
   delayMicroseconds(1640);
   pulseIR(520);
   delayMicroseconds(38560);
   pulseIR(8700);
   delayMicroseconds(2160);
   pulseIR(540);
   delayMicroseconds(27464);
   pulseIR(8760);
   delayMicroseconds(2140);
   pulseIR(540);
 } 

 void SendtvSoundCode() {
   // This is the code for the CHANNEL + for the TV COMCAST
  
   delayMicroseconds(16660);      //Time off (LEFT column)       
   pulseIR(9320);                //Time on (RIGHT column)    <-------DO NOT MIX THESE UP
   delayMicroseconds(4620);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(1720);
   pulseIR(580);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(1720);
   pulseIR(480);
   delayMicroseconds(1840);
   pulseIR(560);
   delayMicroseconds(1740);
   pulseIR(560);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(1740);
   pulseIR(460);
   delayMicroseconds(1840);
   pulseIR(560);
   delayMicroseconds(1760);
   pulseIR(560);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(1720);
   pulseIR(460);
   delayMicroseconds(1860);
   pulseIR(540);
   delayMicroseconds(1740);
   pulseIR(580);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(560);
   pulseIR(580);
   delayMicroseconds(580);
   pulseIR(560);
   delayMicroseconds(1720);
   pulseIR(480);
   delayMicroseconds(1840);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(560);
   delayMicroseconds(560);
   pulseIR(580);
 } 

 void SendauxSoundCode() {
   // This is the code for the CHANNEL + for the TV COMCAST
  
   delayMicroseconds(63528);      //Time off (LEFT column)       
   pulseIR(9280);                //Time on (RIGHT column)    <-------DO NOT MIX THESE UP
   delayMicroseconds(4640);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(1760);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(1760);
   pulseIR(520);
   delayMicroseconds(1780);
   pulseIR(540);
   delayMicroseconds(1780);
   pulseIR(520);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(560);
   delayMicroseconds(1780);
   pulseIR(480);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(1820);
   pulseIR(500);
   delayMicroseconds(1760);
   pulseIR(560);
   delayMicroseconds(1780);
   pulseIR(500);
   delayMicroseconds(1780);
   pulseIR(520);
   delayMicroseconds(620);
   pulseIR(520);
   delayMicroseconds(1800);
   pulseIR(520);
   delayMicroseconds(1780);
   pulseIR(500);
   delayMicroseconds(1780);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(600);
   pulseIR(540);
   delayMicroseconds(47240);
   pulseIR(9260);
   delayMicroseconds(2360);
   pulseIR(520);
 }

Don’t use code that bangs out the codes directly.

Use the IRremote library.

ex: