1

I have two different programs written for my Arduino. One checks the room temperature every 5 seconds. The other one looks for IR remote signals.

I would love to combine those 2 programs, yet the problem is the delay from the room temperature sketch.

If I would combine them. it means my IR script won't be working for a full 5 seconds (because of the delay of the room temperature script) then it will be able to receive a fraction of a second and then the temperature sketch goes again.

#include <IRremote.h>

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
int led = 12;
const int inPin = 0;

void setup() {
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  int value = analogRead(inPin);
  float millivolts = (value / 1024.0 ) * 5000;
  float celsius = millivolts / 10;
  Serial.println(celsius, 1);
  delay(5000);
  if(irrecv.decode(&results)) {
    //this checks to see if a code has been received
    if(results.value == 0xFF6897) {
      //if the button press equals the hex value 0xC284
        digitalWrite(led, HIGH);
        // turn the LED on (HIGH is the voltage level)
        delay(1000); // wait for a second
        digitalWrite(led, LOW);
        // turn the LED off by making the voltage LOW
        delay(1000); // wait for a second//do something useful here
    }
    irrecv.resume(); //receive the next value
  }
}

Does anyone know how to keep the IR part active and the temperature measure every 5 seconds, and do this simultaneously?

Or do I need another Arduino?

  • 2
    Take a look at the BlinkWithoutDelay example bundled with the IDE. It shows you exactly what you need to know. – Majenko Jul 16 '17 at 10:58
  • that was kinda what I was looking it is not working flawless yet, but I think it will work Thank you! – Anton van der Wel Jul 16 '17 at 17:47
2

EDIT: Modify the code according @Edgar's comment.

Check this alternative. The idea is executing code every 5 and 1 seconds. The blink is true when you need the LED blinking. I don't know how long will you need to keep it blinking, but this keeps it as long as results.value keep being equals to 0xFF6897.

#include <IRremote.h>

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
int led = 12;
const int inPin = 0;

void setup() {
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  static boolean blink = false;
  static unsigned long startTemp = 0;
  static unsigned long startBlink = 0;
  unsigned long msTime = millis();

  //  Check for room temp every 5 seconds.
  if (msTime - startTemp >= 5000) {
    startTemp = msTime;
    int value = analogRead(inPin);
    float millivolts = (value / 1024.0 ) * 5000;
    float celsius = millivolts / 10;
    Serial.println(celsius, 1);
  }

  //  Blink every 1 second
  if (msTime - startBlink >= 1000 && blink) {
    startBlink = msTime;
    int state = digitalRead(led);
    digitalWrite(led, !state);
    //  Change blink to stop blinking after one pass (HIGH/LOW).
    //  Modify if you need to keep blinking for longer time.
    blink = (state == LOW);    
  }

  if(irrecv.decode(&results)) {
    blink = (results.value == 0xFF6897);        
    irrecv.resume(); //receive the next value
  }
}
  • this one looks very promising I will give this a try – Anton van der Wel Jul 20 '17 at 15:49
  • @Anton. I didn't test the sketch and probably you have to adjust the blink variable to suit your needs. – user31481 Jul 20 '17 at 15:51
  • 2
    Testing for millis() % 1000 == 0 is not a good idea. The millis() counter is updated every 1024 µs: most of the time it is incremented by 1, but once every 42 increments (roughly) it is incremented by 2. Thus you are going to miss some blinks. – Edgar Bonet Jul 20 '17 at 17:54
  • so 1000 or 999 should fix it? – Anton van der Wel Jul 20 '17 at 19:45
  • 1
    @Anton. Check the new edited version that follows Edgar's comment. – user31481 Jul 20 '17 at 19:54
2

does anyone know how to keep ...

think about ways to conditionalize execution on time elapsed. I showed a short while ago ways to construct a software timer and you may want to look into that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.