Combining sketches

What I’m trying to do is when I hit power on my remote it goes to the strobe effect.
Both sketches work independently. I can turn an led on and off using power button with my remote and the strobe effect works but I just can’t get the two sketches combined

sketch 1. The remote sketch
/*
source: www.electroschematics.com
You'll need to change the led pins and the codes
accordingly to your configuration and IR remote
*/

#include
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver

int itsONled[] = {0,0,0,0,0};

/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 41565 // code received from Power Button

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{

irrecv.enableIRIn(); // Start the receiver
pinMode(ledPin, OUTPUT);

}

void loop() {

if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(ledPin, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(ledPin, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}

break;
}

irrecv.resume(); // Receive the next value
}
}

STROBE. Sketch. 2. The strobe and or fade

int cycle=75;
int strobe=cycle*20; // calculate strobe delay
int maxFade=75; // maximum brightness before strobe
int ledPin = 11; // Led connected to digital pin 11
int fadeValue;

void setup() {
// nothing happens in set
}
void loop() {
// fade in from min to max in increments of 2 points:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
analogWrite(ledPin, 255); // simulate a rotating beacon catching your eye
delay(strobe); // hold full brightness for strobe delay
analogWrite(ledPin, maxFade);
// fade out from maxFade to min in increments of 2 points:
for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
}

You've posted two sketches you say both work. So there's nothing there to advise you on.
You say you can't get them to work as one which implies that you've tried to combine them. Show us that code along with any error messages the IDE produced while trying to compile it. Then we're good to go for some suggestions.

Always show us your ‘current’ complete sketch.
Use CTRL T to format the sketch.

Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

I've combined to best of my limited knowledge, but nothing happens. Bottom line is when I push my power button I'd like for the strobe effect to come on. I made a lighthouse and it will be sitting on top of a cupboard, I'd like to just use the remote to turn it on. It will only be used periodically. I'm not sure this is feasible, I'm brain dead researching. Thanks for looking

/*
   source: www.electroschematics.com
   You'll need to change the led pins and the codes
   accordingly to your configuration and IR remote
*/

#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int ledPin = 11;
int cycle = 75;  // wait for "cycle" milliseconds to see the dimming effect
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1  41565 // code received from Power Button


IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{

  irrecv.enableIRIn();  // Start the receiver
  pinMode(ledPin, OUTPUT);

}

void loop() {

  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch (value) {
      case code1:
        // fade in from min to max in increments of 2 points:
        for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2) {
          // sets the value (range from 0 to maxFade):
          analogWrite(ledPin, fadeValue);
          // wait for "cycle" milliseconds to see the dimming effect
          delay(cycle);
        }
        analogWrite(ledPin, 255); // simulate a rotating beacon catching your eye
        delay(strobe); // hold full brightness for strobe delay
        analogWrite(ledPin, maxFade);
        // fade out from maxFade to min in increments of 2 points:
        for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2) {
          // sets the value (range from 0 to maxFade):
          analogWrite(ledPin, fadeValue);
          // wait for "cycle" milliseconds to see the dimming effect
          delay(cycle);


          break;
        }

        irrecv.resume(); // Receive the next value
    }
  }
}

Have you seen Grumpy Mike's merging code tutorial?

irrecv.resume(); should be outside the case. You always want to resume after you have handled the button press (regardless of the button that was pressed), don't you :wink:

void loop()
{
  if (irrecv.decode(&results))
  {
    unsigned int value = results.value;
    switch (value)
    {
      ...
      ...
    }

    irrecv.resume(); // Receive the next value
  }
}

Your second for-loop contains an unconditional break; that will end the for-loop immediately so the code will only fade one step.

That break was probably supposed to be the end of the case and hence after the for-loop.

    switch (value)
    {
      case code1:
        // fade in from min to max in increments of 2 points:
        for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
        {
          ...
          ...
        }

        ...
        ...
        // fade out from maxFade to min in increments of 2 points:
        for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
        {
          ...
          ...
        }
        break;
    }

No difference in the above suggestions. Thank you for trying. I tried to reverse engineer this starting with the fade effect and inserting the remote sketch parts. When I try to put in irrecv.enableIRIn(); // Start the receiver in the void setup and running the sketch it throws the whole strobe effect out of whack. Maybe this just cant be done, any thoughts?

Gbwith:
No difference in the above suggestions.

Show your new version of your code.

#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver

int ledPin = 11; // Led connected to digital pin 11
int cycle=75;
int strobe=cycle*20; // calculate strobe delay
int maxFade=75; // maximum brightness before strobe
int fadeValue;
#define code1  41565 // code received from Power Button


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup() {
   pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();  // Start the receiver
}

void loop() {
 

for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2) {
      analogWrite(ledPin, fadeValue);
      delay(cycle);

}
      analogWrite(ledPin, 255);
      delay(strobe); 
      analogWrite(ledPin, maxFade);

for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2) {
      analogWrite(ledPin, fadeValue);
      delay(cycle);
}
  }

I'm excited so close. Moved LED to pin 5 which took care of the IR remote issue. Now I hit power button, it cycles through 1 bright to dim and stops. Hit power again 1 more cycle etc. Can't get it to loop and was hoping to shut off with another push of power button.

#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int ledPin = 5; // Led connected to digital pin 11
int cycle = 75;
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1  41565 // code received from Power Button


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup() {

  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();  // Start the receiver

}

void loop() {

  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch (value) {
      case code1:

        for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2) {
          analogWrite(ledPin, fadeValue);
          delay(cycle);

        }
        analogWrite(ledPin, 255);
        delay(strobe);
        analogWrite(ledPin, maxFade);

        for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2) {
          analogWrite(ledPin, fadeValue);
          delay(cycle);
        }


    }  irrecv.resume(); // Receive the next value

  }

}

Your code as it stands now only cycles when you press the button because your for-loops are inside the if (irrecv.decode(&results)).

I think that this loop() will work; it does compile but is not tested.

void loop()
{
  // variable to remember the last value from the remote
  static int value = 0;

  // if remote keypress received
  if (irrecv.decode(&results))
  {
    value = results.value;
    irrecv.resume(); // Receive the next value
  }

  // take action
  switch (value)
  {
    case code1:
      for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
      {
        analogWrite(ledPin, fadeValue);
        delay(cycle);
      }
      analogWrite(ledPin, 255);
      delay(strobe);
      analogWrite(ledPin, maxFade);

      for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
      {
        analogWrite(ledPin, fadeValue);
        delay(cycle);
      }
  }
}

I'm sorry for being a pain It also goes through 1 cycle and stops. I did try moving the ( irrecv.resume(); // Receive the next value) statement, actually took it out and it cycled just as I was looking for. However without it it won't shut off. I know I'm missing some little thing.

I know I'm missing some little thing

We're missing your code.

I've gotten it to turn on and cycle but unable to turn off by remote with the following sketch.

[code#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int ledPin = 5; // Led connected to digital pin 11
int cycle = 75;
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1  41565 // code received from Power Button


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup() {

  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();  // Start the receiver

}

void loop()
{
  // variable to remember the last value from the remote
  static int value = 0;

  // if remote keypress received
  if (irrecv.decode(&results))
  {
    value = results.value;

  }

  // take action
  switch (value)
  {
    case code1:
      for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
      {
        analogWrite(ledPin, fadeValue);
        delay(cycle);
      }
      analogWrite(ledPin, 255);
      delay(strobe);
      analogWrite(ledPin, maxFade);

      for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
      {
        analogWrite(ledPin, fadeValue);
        delay(cycle);
      } irrecv.resume(); // Receive the next value
  }
}]

You have no code to turn things off.

Suppose I can get you to help me out? I'm over my head

How do you want to switch it off? Press of the same button? Press of another button?

For the latter, you need to add an additional case to your switch.

For the former, you need to keep a flag to remember if you're on or off. That flag can be the value variable that you can set to 0 if its current value isn't 0 and set to results.value if its current value equals 0.

I’m sorry I’m lost 63 years old and I can’t get my head wrapped around what I thought I could do. I appreciate all the help. Thought when I got the led to go on and off it wouldn’t be this hard

OK, how do you want to switch it off? Press of the same button? Press of another button?

And if you tried something based on my previous reply, show that code please.

Same button is fine this is what is working so far. It turns on and cycles like I want it to, but I can't turn it off.

#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int ledPin = 5; // Led connected to digital pin 11
int cycle = 75;
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1  41565 // code received from Power Button


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup() {

  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();  // Start the receiver

}

void loop()
{
  // variable to remember the last value from the remote
  static int value = 0;

  // if remote keypress received
  if (irrecv.decode(&results))
  {
    value = results.value;

  }

  // take action
  switch (value)
  {
    case code1:
      for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
      {
        analogWrite(ledPin, fadeValue);
        delay(cycle);
      }
      analogWrite(ledPin, 255);
      delay(strobe);
      analogWrite(ledPin, maxFade);

      for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
      {
        analogWrite(ledPin, fadeValue);
        delay(cycle);
      } irrecv.resume(); // Receive the next value
  }
}