Notifications
Clear all

use millis() in IR Remote Controls

5 Posts
3 Users
1 Likes
3,780 Views
 Roni
(@roni)
Member
Joined: 4 years ago
Posts: 2
Topic starter  

Hello everyone.
I'm working on a similar project ( https://dronebotworkshop.com/using-ir-remote-controls-with-arduino/ )

In yellow & red LED project.

The design: when i pressing a button on the remote control, the relay 1 will work for a 30 second .Pressing the same button on the remote control, relay 1 will turn off and relay 2 will be turn on for 30 seconds.
I want to change position of Relay  1 / 2  Not dependent on the 30-second ending.

  • The DELAY command cannot change the relay mode, only after the 30-second.
    The "millis()" command I was unable to implement.
    Does anyone have an idea or can help.
  • Note: The project for "TV elevator" that is already working. Just for upgrade it.

The code:

#include <IRremote.h>

// Define sensor pin
const int RECV_PIN = 4;

// Define LED pin constants
const int Relay _1= 7;

const int Relay _2=8;

// Define integer to remember toggle state
int togglestate = 0;

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
// Enable the IR Receiver
irrecv.enableIRIn();
// Set LED pins as Outputs
pinMode(Relay _1, OUTPUT);

pinMode(Relay _2, OUTPUT);
}

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

switch(results.value){

case 0x2FD48B7: //Relay _1
// Toggle Relay _1 On or Off
if(togglestate==0){
digitalWrite(Relay _1, HIGH);

//////////////////////////

Here TO put a DELAY of 30 seconds

//////////////////////////

digitalWrite(Relay _1, LOW);
togglestate=1;
}
else {
digitalWrite(Relay _2, HIGH);

//////////////////////////

Here TO put a DELAY of 30 seconds

//////////////////////////

digitalWrite(Relay _2, LOW);

togglestate=0;
}
break;

}
irrecv.resume();
}

}

Thanks

 


   
Quote
Topic Tags
(@jbeazy)
Member
Joined: 5 years ago
Posts: 18
 

Did you mean to post this in "new content?" Myself or others can help you out if you re-post in "Components and Programming-Arduino" or elsewhere.


   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1081
 

@jbeazy  Thank you for pointing this out, I have moved the post to a more suitable location.

@roni  Please post in the appropriate location, you'll see a description of each section at the top of the section. "Suggest New Content" is clearly for suggesting the subjects for new videos and articles.

Another thing - the <> button on the toolbar can be used to format any code samples you wish to enter, and it makes them a lot easier to read.  Like this:

#include <IRremote.h>

// Define sensor pin
const int RECV_PIN = 4;

// Define LED pin constants
const int Relay _1= 7;

const int Relay _2=8;

// Define integer to remember toggle state
int togglestate = 0;

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
// Enable the IR Receiver
irrecv.enableIRIn();
// Set LED pins as Outputs
pinMode(Relay _1, OUTPUT);

pinMode(Relay _2, OUTPUT);
}

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

switch(results.value){

case 0x2FD48B7: //Relay _1
// Toggle Relay _1 On or Off
if(togglestate==0){
digitalWrite(Relay _1, HIGH);

//////////////////////////

Here TO put a DELAY of 30 seconds

//////////////////////////

digitalWrite(Relay _1, LOW);
togglestate=1;
}
else {
digitalWrite(Relay _2, HIGH);

//////////////////////////

Here TO put a DELAY of 30 seconds

//////////////////////////

digitalWrite(Relay _2, LOW);

togglestate=0;
}
break;

}
irrecv.resume();
}

}

 

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
(@jbeazy)
Member
Joined: 5 years ago
Posts: 18
 

Here is something that has the following features: 1) Only one led (or relay) on at a time. 2)Maximum on time is 30 seconds. 3) If IR signal received (or switch press) while an led is on it immediately toggles the leds.  I don't think you need the switch-case unless you have a lot of "cases." An if-then or if-else would do for one or two cases.  Below uses a momentary switch to toggle. You can replace that with the if (irrecv.decode(&results)).

/* Assumptions
Only 1 of 2 relays (or leds) can be ON at any time
No relay (or led) can be ON more than 30 seconds
If button switch (or IR signal) is received while a relay (or led)is ON it will turn OFF and other ON
*/
int recv_pin = 4; // define momentary switch (or IR signal received) Arduino pin
// Define LED(RELAY) pin constants
int relay_1= 7; // define relay_1 Arduino pin ; for testing relay_1 is a led
int relay_2= 8; // define relay_2 Arduino pin ; for testing relay_2 is a led
bool toggle_state = LOW; //toggle relay_1 or relay_2
bool relay_on = LOW; // flag to say a relay (or led) is on
unsigned long start_time; // initial time (in msec) switch goes HIGH or the IR signal is received


void setup() {
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  digitalWrite(relay_1,LOW);
  digitalWrite(relay_2,LOW);
  pinMode(recv_pin, INPUT); // connected to momentary switch normally LOW 
}
void loop() {
  if( digitalRead(recv_pin) ){ // If button is pressed or IR signal received
    relay_on= 1;  // flag that says one relay (led) is on
    toggle_state= !toggle_state;
    start_time= millis(); // initial time relay (led) is on
    digitalWrite(relay_1,toggle_state);  // HIGH or LOW
    digitalWrite(relay_2,!toggle_state);
  }
  if(relay_on){ // check to see if the 30 second time limit has been reached
    if(millis() - start_time >= 30000){
    digitalWrite(relay_1,LOW);
    digitalWrite(relay_2,LOW);
    relay_on = 0;  // All relays (leds) are off
    }
  }
  delay(175);
}

   
ReplyQuote
 Roni
(@roni)
Member
Joined: 4 years ago
Posts: 2
Topic starter  

Hi JBeazy,

Thank you for your time and answer.

I will check and update you how it work.

thanks again. 👍 

 


   
ReplyQuote