IR Remote Controlled DC Fan with LCD Display

Hi, I'm creating an IR Remote Controlled DC Fan with LCD Display, but I'm having problems with setting the motor RPM while running the code. As soon as I press a button on the remote the motor turns off.

The Code:

#include "LiquidCrystal.h"
#include "IRremote.h"

LiquidCrystal lcd(6, 7, 2, 3, 4, 5);
int receiver = 8;

int ENABLE = 11;
int DIRA = 9;
int DIRB = 10;

IRrecv irrecv(receiver);
decode_results results;

int translateIR() {
  int fanSpeed = 0;
  lcd.setCursor(10, 1);
  switch (results.value) {
    case 0xFFA25D: 
      lcd.print("0"); 
      fanSpeed = 0;
      break;
    case 0xFF30CF:
      lcd.print("1");
      fanSpeed = 85;
      break;
    case 0xFF18E7: 
      lcd.print("2"); 
      fanSpeed = 170;
      break;
    case 0xFF7A85: 
      lcd.print("3"); 
      fanSpeed = 255;
      break;
    default:
      Serial.println(" other button   ");
  }
  delay(500);
  return fanSpeed;
}

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  irrecv.enableIRIn();
  lcd.begin(16, 2);
  lcd.print("  Fan Controls");
  Serial.begin(9600);
}

void loop() {
  if (irrecv.decode(&results)) {
    irrecv.resume();
  }
  int speed = translateIR();
  lcd.setCursor(0, 1);
  lcd.print("Fan Speed:");
  digitalWrite(ENABLE,HIGH);
  digitalWrite(DIRA,LOW);
  digitalWrite(DIRB,HIGH);
  analogWrite(ENABLE, speed);
  delay(1000);
}

This sets the speed to stop. Does the "other button" come up on the serial monitor?

#include "LiquidCrystal.h"
#include "IRremote.h"

LiquidCrystal lcd(6, 7, 2, 3, 4, 5);
int receiver = 8;

int ENABLE = 11;
int DIRA = 9;
int DIRB = 10;

IRrecv irrecv(receiver);
decode_results results;

int translateIR() {
  static int fanSpeed = 0; // try this from https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/static/
  lcd.setCursor(10, 1);
  switch (results.value) {
    case 0xFFA25D: 
      lcd.print("0"); 
      fanSpeed = 0;
      break;
    case 0xFF30CF:
      lcd.print("1");
      fanSpeed = 85;
      break;
    case 0xFF18E7: 
      lcd.print("2"); 
      fanSpeed = 170;
      break;
    case 0xFF7A85: 
      lcd.print("3"); 
      fanSpeed = 255;
      break;
    default:
      Serial.println(" other button   ");
  }
  delay(500);
  return fanSpeed;
}

void setup() {
  pinMode(ENABLE, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  irrecv.enableIRIn();
  lcd.begin(16, 2);
  lcd.print("  Fan Controls");
  Serial.begin(9600);
}

void loop() {
  if (irrecv.decode(&results)) {
    irrecv.resume();
  }
  int speed = translateIR();
  lcd.setCursor(0, 1);
  lcd.print("Fan Speed:");
  digitalWrite(ENABLE,HIGH);
  digitalWrite(DIRA,LOW);
  digitalWrite(DIRB,HIGH);
  analogWrite(ENABLE, speed);
  delay(1000);
}

it keeps repeating on the serial monitor, even when no buttons are pressed

and no more inputs are taken after the first one.

nope still not working

You set fanSpeed to zero, and default happens when translateIR() does not see 1, 2, or 3. What button is your sketch seeing?

Very unhelpful.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.