I need littlehelp for my project

this is my code

#include <Arduino.h>
#include <EEPROM.h>
#include <IRremote.hpp>

#define IR_RECEIVE_PIN 3

bool dev1st, dev2st;
const int dev1addr=2, dev2addr=4, dimmeraddr=8;
int currentspeed;
const int dev1Pin = 11; // D7 for device 1
const int dev2Pin = 10; // D8 for device 2
const int dimmer1 = 6; // D9 for Dimmer
int pwmValue; // Declare pwmValue globally without initialization

// Define IR remote codes
#define code1 0xDA25FBE2 // Speed 0 (dimmer 1 level 0)
#define code2 0xD22DFBE2 // Speed 1 (dimmer 1 level 40)
#define code3 0xD926FBE2 // Speed 2 (dimmer 1 level 80)
#define code4 0xF40BFBE2 // Speed 3 (dimmer 1 level 120)
#define code5 0xF50AFBE2 // Speed 4 (dimmer 1 level 160)
#define code6 0xF00FFBE2 // Speed 5 (dimmer 1 level 255)
#define code13 0xF30CFBE2 // Device 1 Toggle
#define code14 0xE51AFBE2 // Device 2 Toggle

void setup() {
  Serial.begin(115200); // Initialize Serial communication
  IrReceiver.begin(IR_RECEIVE_PIN); // Start the IR receiver

  pinMode(A3,INPUT); // Set EEPROM READ pin as Input (high = read)
  pinMode(A2,INPUT); // Set EEPROM WRITE pin as Input (low = Write)
  pinMode(dev1Pin, OUTPUT); // Set device 1 pin as output
  pinMode(dev2Pin, OUTPUT); // Set device 2 pin as output
  pinMode(dimmer1, OUTPUT); // Set dimmer pin as output

int analogValue = analogRead(A3);

  if (analogValue>900) {
  // Restore data from EEPROM to variables
  dev1st = EEPROM.read(dev1addr);
  dev2st = EEPROM.read(dev2addr);
  currentspeed = EEPROM.read(dimmeraddr);

  // Update device states and dimmer levels accordingly
  digitalWrite(dev1Pin, dev1st);
  digitalWrite(dev2Pin, dev2st);
  analogWrite(dimmer1, currentspeed);
  Serial.println(F("Restored"));
  }  Serial.println(F("System Initialized"));
}

void loop() {
  int analogin= analogRead(A2);
  if(analogin<20){
    EEPROM.write(dev1addr,dev1st);
    EEPROM.write(dev2addr,dev2st);
    EEPROM.write(dimmeraddr,currentspeed);
  Serial.println(F("Data Writing Complete"));    
  delay(2000000);
  }
  // Check for IR remote commands
  if (IrReceiver.decode()) {
    // Check which IR code was received and perform actions
    if (IrReceiver.decodedIRData.decodedRawData == code1) {
      analogWrite(dimmer1, 0); // Set dimmer level to 0
      currentspeed = 0;        // Update current speed for dimmer1
      Serial.println(F("Dimmer1 value = 0%"));
    } else if (IrReceiver.decodedIRData.decodedRawData == code2) {
      analogWrite(dimmer1, 40); // Set dimmer level to 40
      currentspeed = 16;        // Update current speed for dimmer1
      Serial.println(F("Dimmer1 value = 16%"));
    } else if (IrReceiver.decodedIRData.decodedRawData == code3) {
      analogWrite(dimmer1, 80); // Set dimmer level to 80
      currentspeed = 31;        // Update current speed for dimmer1
      Serial.println(F("Dimmer1 value = 31%"));
    } else if (IrReceiver.decodedIRData.decodedRawData == code4) {
      analogWrite(dimmer1, 120); // Set dimmer level to 120
      currentspeed = 47;        // Update current speed for dimmer1
      Serial.println(F("Dimmer1 value = 47%"));
    } else if (IrReceiver.decodedIRData.decodedRawData == code5) {
      analogWrite(dimmer1, 160); // Set dimmer level to 160
      currentspeed = 63;        // Update current speed for dimmer1
      Serial.println(F("Dimmer1 value = 63%"));
    } else if (IrReceiver.decodedIRData.decodedRawData == code6) {
      analogWrite(dimmer1, 255); // Set dimmer level to 255
      currentspeed = 100;        // Update current speed for dimmer1
      Serial.println(F("Dimmer1 value = 100%"));
    } else if (IrReceiver.decodedIRData.decodedRawData == code13) {
      // Toggle Device 1
      dev1st = !dev1st;
      digitalWrite(dev1Pin, dev1st);
      Serial.print(F("Device 1 = "));
      Serial.println(dev1st);
    } else if (IrReceiver.decodedIRData.decodedRawData == code14) {
      // Toggle Device 2
      dev2st = !dev2st;
      digitalWrite(dev2Pin, dev2st);
      Serial.print(F("Device 2 = "));
      Serial.println(dev2st);
    }

    IrReceiver.resume(); // Receive the next IR code
  }

  // Check for Serial commands
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');

    if (command.equals("/1on")) {
      digitalWrite(dev1Pin, HIGH); dev1st=1; // Turn on Device 1
      Serial.println(F("Device 1 = 1"));
    } else if (command.equals("/1off")) {
      digitalWrite(dev1Pin, LOW); dev1st=0; // Turn off Device 1
      Serial.println(F("Device 1 = 0"));
    } else if (command.equals("/2on")) {
      digitalWrite(dev2Pin, HIGH); dev2st=1; // Turn on Device 2
      Serial.println(F("Device 2 = 1"));
    } else if (command.equals("/2off")) {
      digitalWrite(dev2Pin, LOW); dev2st=0; // Turn off Device 2
      Serial.println(F("Device 2 = 0"));
    } else if (command.startsWith("/s/")) {
      int pwmspeed = command.substring(3).toInt(); // Extract pwm speed value

      if (pwmspeed >= 0 && pwmspeed <= 100) {
        pwmValue = map(pwmspeed, 0, 100, 0, 255); // Map pwm speed to PWM value
        analogWrite(dimmer1, pwmValue); // Set dimmer to calculated PWM value
        currentspeed = pwmspeed; // Update current speed
        Serial.print(F("Dimmer1 value = "));
        Serial.print(pwmspeed);
        Serial.println(F("%"));
      }
    } else if(command.equals("/w")){
        printstatus();
    } else {
      Serial.println(F("Invalid command."));
    }
  }
}

void printstatus() {
  // Print device states
  Serial.print(F("Device 1 = "));
  Serial.println(dev1st);
  delay(200);
  Serial.print(F("Device 2 = "));
  Serial.println(dev2st);
  delay(200);

  // Print dimmer values
  Serial.print(F("Dimmer1 value = "));
  Serial.print(currentspeed);
  Serial.println(F("%"));
  delay(200);
}

i want to transmite IR too (example if i send serial Lamp1-on / Lamp1-off IR led1 send 0xFF00FBE2 and if i send serial Fan1-on / Fan1-off IR led2 send 0xFD02FBE2

i want to use 2 ir tranmite leds

thanks in advance

forget 1 thing : i dont want to save ir transmite data to eeprom

Can't help ... but you'll have time for a cuppa there! :stuck_out_tongue_winking_eye:

You can use this to get instructions:
IrReceiver.printIRSendUsage(&Serial);

Basically this code works good i used this code for my room lights and it works quite well and i use this long delay to avoid rewrite again in eeprom after power loss (i make logic for arduino to give power to controller for 3 to 10 sec after main power loss (enough time to write current device status to eeprom) @cherk

Can you please share any example cuz i tried multiple time and searched so much on search engine but got no luck ...... i use latest IR libraray v4 which make irremote.hpp instead of irremote.h

Use that syntax (post#4) in your receive code to get instructions how to send received signal.
Having a hex of the signal without protocol (timings, headers)is completely useless for sending purposes

example from your library:

#include <IRremote.hpp>
#define IR_RECEIVE_PIN 2

void setup()
{
...
  Serial.begin(115200); // // Establish serial communication
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
}

void loop() {
  if (IrReceiver.decode()) {
      Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); // Print "old" raw data
      IrReceiver.printIRResultShort(&Serial); // Print complete received data in one line
      IrReceiver.printIRSendUsage(&Serial);   // Print the statement required to send this data
      ...
      IrReceiver.resume(); // Enable receiving of the next value
  }
  ...
}

can you clarify the help you need ?

Hi Jackson,
I'm working on a project where I want to control my room's ceiling fan and light using an Arduino-based controller. Both devices are currently controlled with separate IR remotes.
I want to add two IR transmitters to my circuit, one for the fan and one for the light. I want to be able to send IR codes to the fan and light from my Arduino using serial commands. For example, sending "fan1" would turn the fan on at low speed, while sending "fan5" would turn it on at high speed.
I'm looking for guidance on how to add IR transmitters to my Arduino circuit and send IR codes to control my fan and light.

Thank you for your help!

I plan to use heat shrink tubing to securely bind one IR transmitter to the fan's IR receiver and the other to the light's IR receiver. This will ensure a reliable connection and prevent accidental dislodging.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.

I can share hand drawn concept schematic

  • Please confirm:
    -The two (2) IR RXs are wired to what hardware ?
    -The two TX IR LEDs are connected to an Arduino ?
    - How does the Arduino know what to send ?



  • Questioners know what their hardware looks like but the volunteers do not.
    You need to fully explain how it is supposed to work.

  • Please supply a full schematic showing all components.

i want to transmit IR too (example if i send serial Lamp1-on / Lamp1-off IR led1 send 0xFF00FBE2 and if i send serial Fan1-on / Fan1-off IR led2 send 0xFD02FBE2

i want to use 2 ir transmit leds (tx) fan and lamp have ir eye 3pin ir rx

  • What action starts this Lamp1-on request ?

Check this

After that i want to add ir tx in this code

  • I would like to contribute to this discussion but we seem to have a disjointed discussion.

  • We need a complete schematic accurately showing all components.

  • We need to see the sketch you want to modify.

  • We need a complete explanation of how things are supposed to work.

- This user will wait for the requested information.

I already provide things ..... and sorry to say but i think jackson can help me