0

Hı everyone I am trying give my led a breathing effect using millis() But neither led isnt breathinng and it stucks I cannot use other fucntions.

Please help...

#include <IRremote.h>
#define irPin 2
int led1 = 9;
int turn = 11;
bool breathing = false; 
IRrecv irrecv(irPin);
decode_results results;


int a;



void breathe()
{
  static unsigned long previousMillis = 0;
  static boolean breathingIn = true;
  static unsigned int brightness = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > 30)
  {
    previousMillis = currentMillis;


    if(breathingIn){
      brightness++;
      analogWrite(turn, brightness);      
      if (brightness == 255){
        breathingIn = false;
      }   
    }
    else{
      brightness--;
      analogWrite(turn, brightness);      
      if (brightness == 0){
        breathingIn = true;
      }
    }
  }
  a = brightness;
}
void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(led1, OUTPUT);
  pinMode(turn, OUTPUT);
  a = 0;
}
void loop() {
  if(breathing){
    //Serial.println(millis());   
    breathe();
    Serial.println(a);
  }   
  if(irrecv.decode(&results)){
    switch(results.value){
      case 0xFF6897: //IR remote key to pwmLED LED1 ON
        digitalWrite(led1, HIGH);
        break;
 
      case 0xFFB04F: //IR remote key to pwmLED LED1 OFF
        digitalWrite(led1, LOW);
        break;
 
      case 0xFFA25D: //I want to use this key to start breathing
      //  Serial.println(results.value, HEX);
        breathing = true;
        break;
 
      case 0xFFA857: //I want to use this key to stop breathing
        Serial.println(results.value, HEX);
        breathing = false;
        break;
 
    }
    irrecv.resume();
  }
}
5
  • 1
    Your breathe() never run because of your breathing initial state. – hcheung Oct 23 '20 at 16:21
  • 1
    this is very confusing, because IR is not visible. are you actually using an IR led or just some library? – entropicCreator Oct 23 '20 at 16:46
  • Are you sure, that the code reaches the breathing = true; line, when you press the corresponding button on your IR remote? You could check with some serial print lines. And what do you mean with "it stucks I cannot use other fucntions"? – chrisl Oct 23 '20 at 18:17
  • 2
    In your loop(), the switch() statement has no default: case. It is good practice to include at least an empty one, and in this case you might print any unexpected values. Are you sure you are testing for the right values? It might be worth printing results.value outside of the switch statement while you press the control buttons you want to detect, to be sure. Print statements are your friend, here. They can show you what values you receive and what paths your sketch does or doesn't follow. They will slow your code down but once you find & fix your errors you can remove them. – JRobert Oct 23 '20 at 20:12
  • I updated the code its more healthier right now but its still stcuks. Yes I tested it really reaches breathinh true. I tested to print results.value in all way it sees only once but when it stucks it cant show anything. – Semih Ufuk Güler Oct 24 '20 at 16:06

Your Answer

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

Browse other questions tagged or ask your own question.