Two programs in one

Hi everyone,

i wanted to make a program that is able to:

  1. Open a relay after a certain amount of time once a specific input was triggered
  2. Open another relay instantly after a specific button on an IR remote was pressed

So i made my research and found both of these programs, however, there is a problem that i dont know how to put them into a one loop, would that be difficult? How can it be done?

Here is the program of delay:

int LED = 11;
int BUTTON = 13;

unsigned long previousMillis = 0;
const long interval = 2000;

void setup(){
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
}

void loop(){

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

if(digitalRead(BUTTON) == HIGH){
digitalWrite(LED,LOW);
}else{
digitalWrite(LED,HIGH);
}
}
}

and here is the code of another program:

#include <IRremote.h>

int RECV_PIN = 12;
int mRelay = 13;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
pinMode(mRelay, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

int on = 0;
unsigned long last = millis();

void loop() {
if (irrecv.decode(&results)) {
if (results.value == 0xD || results.value == 0x80D) { // TIVO button
// If it’s been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
on = !on;
digitalWrite(mRelay, on ? HIGH : LOW);
}
last = millis();
}
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}

Rather than posting two programs (badly), why not post the one program you tried to write that does both things?

Oh, sorry, ill post it down below, What i tried to do was make my delay program as a function anode() and launch it at the end of the main loop:

#include <IRremote.h>

int RECV_PIN = 12;
int muteRelay = 13;
int anodeRelay = 6;
int BUTTON = 5;
unsigned long previousMillis = 0;
const long interval = 2000;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
pinMode(muteRelay, OUTPUT);
pinMode(4, OUTPUT);
pinMode(anodeRelay,OUTPUT);
pinMode(BUTTON,INPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
unsigned long last = millis();
anode ();

}

int on = 0;

void loop() {

if (irrecv.decode(&results)) {
if (results.value == 0xD || results.value == 0x80D) { // TIVO button
// If it’s been at least 1/4 second since the last
// IR received, toggle the relay
// if (millis() - last > 250) {
on = !on;
digitalWrite(muteRelay, on ? HIGH : LOW);
// }
// last = millis();
}
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
void anode();
}
}

void anode() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

if(digitalRead(BUTTON) == HIGH){
digitalWrite(anodeRelay,LOW);
}else{
digitalWrite(anodeRelay,HIGH);
}
}
}

What was the result? Does it work?

 Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
   void anode();

When you call the function digitalWrite, do you write

void digitalWrite(pin, state);

or just

digitalWrite(pin, state);

You've confused calling a function with defining a function. You only need the void when you define a function. I would suggest doing at least the first little part of any of the many C++ tutorials online to get the basics of stuff like that. This can be a very frustrating endeavor if you don't know these sorts of things.