Sketch not running till reset pressed?

Hi all, I'm building a simple circuit to control a ceiling fan and light using a cheapo Chinese IR remote. I've adapted a sketch I found on 'tinternet and although a little tweaking is still needed it seems to work ok, except when first powered up nothing happens unless I press reset on the Arduino. Any ideas what might be causing this? Many thanks for any advise as usual. Regards, Bob.

// IR remote for ceiling Fan adapted from
// www.circuitbasics.com/arduino-ir-remote-receiver-tutorial/
#include <IRremote.h>

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int A1Pin = 10;         //Speed 1 relay
const int A2Pin = 11;         //Speed 2 relay
const int A3Pin = 12;         //Speed 3 relay
const int A4Pin = 6;          //Spare output for later
const int A11Pin = 5;         //Light relay
void setup(){
 irrecv.enableIRIn();
 irrecv.blink13(true);
 pinMode(A1Pin, OUTPUT);
 pinMode(A2Pin, OUTPUT);
 pinMode(A3Pin, OUTPUT);
 pinMode(A4Pin, OUTPUT);
 pinMode(A11Pin, OUTPUT);
}

void loop(){
   if (irrecv.decode(&results)){

       switch(results.value){
         case 0xFFA25D: //Keypad button "1"
         digitalWrite(A1Pin, HIGH);
         digitalWrite(A2Pin, LOW);
         digitalWrite(A3Pin, LOW);
         }

       switch(results.value){
         case 0xFF629D: //Keypad button "2"
         digitalWrite(A2Pin, HIGH);
         digitalWrite(A1Pin, LOW);
         digitalWrite(A3Pin, LOW);
         }

       switch(results.value){
         case 0xFFE21D: //Keypad button "3"
         digitalWrite(A3Pin, HIGH);
         digitalWrite(A2Pin, LOW);
         digitalWrite(A1Pin, LOW);
         }

       switch(results.value){
         case 0xFF22DD: //Keypad button "4"
         digitalWrite(A4Pin, HIGH);
        
         }

       switch(results.value){
         case 0xFF6897: //Keypad button "*"
         digitalWrite(A11Pin, HIGH);

         }

       switch(results.value){
         case 0xFFB04F: //Keypad button "#"
         digitalWrite(A11Pin, LOW);
         }

       switch(results.value){
         case 0xFF38C7: //Keypad button "OK"
         digitalWrite(A1Pin, LOW);
         digitalWrite(A2Pin, LOW);
         digitalWrite(A3Pin, LOW);
         digitalWrite(A11Pin, LOW);
         digitalWrite(A4Pin, LOW);
         }

       irrecv.resume(); 
   }
}

What are all the components of your circuit? Sometimes certain devices need a reset to "turn on". The reset essentially indicates to the powered device that it will be used.

Maybe not your problem but, you should not have a switch statement for each case. Switch statement reference.

Did you forget code tags (see #7)?

groundFungus:
Maybe not your problem but, you should not have a switch statement for each case. Switch statement reference.

Did you forget code tags (see #7)?

As you might well have guessed I'm pretty much out of my depth here, I've adapted a sketch which does what I need it to, just not until I press the reset button on the Arduino board. I had thought that applying power for the first time actually achieved the same thing and started the sketch again but it seems not to.

Press reset botton or detack and reattac power is the same, and have to be the same. What appends in a case? And in the other?

Silente:
Press reset botton or detack and reattac power is the same, and have to be the same. What appends in a case? And in the other?

Yes that's what I thought, but if I attach power then try the remote nothing happens. Only after I press reset or ground the reset pin does it start to work. Most confusing?

Well I don't quite get this but I tried using another Arduino Uno board instead of the genuine article, this time a cheap Chinese knock-off and it works perfectly. So thanks for your advise and sorry to feed you a bum steer but it looks like problem solved! :slight_smile:

Hello Silente

Silente:
Press reset botton or detack and reattac power is the same, and have to be the same. What appends in a case? And in the other?

No : the effect on the SRAM is not the same.

Regards,
bidouilleelec