A problem with IRsend and ESP32

Hello, lately i’m having a problem with simpletimer, always when i call virtual pin 3 for the first time, simpletimer ignores the codes inside “ar1” and “ar2” and just counts the time, but when i call pin v3 For the second time on it works normally, how could I fix this? below is the code snippet


BLYNK_WRITE(V3)
{
  if (param.asInt() == HIGH && !uai)
  {
    uai = 1;
    ar1();
  }
}

void ar1() {
  int khz = 38;
  unsigned int irSignal1[59] = {3272, 9840, 536, 1560, 512, 508, 532, 520, 508, 520, 512, 1544, 532, 504, 536, 520, 508, 528, 512, 516, 532, 520, 508, 520, 512, 508, 532, 520, 508, 520, 512, 520, 512, 512, 532, 520, 512, 520, 512, 1548, 512, 1548, 512, 520, 512, 1544, 532, 520, 512, 520, 512, 516, 512, 1560, 512, 1552, 532, 1536, 532}; //LIGA
  irsend.sendRaw(irSignal1, sizeof(irSignal1) / sizeof(irSignal1[0]), khz); //Note the approach used to automatically calculate the size of the array.
  taimer.setTimeout(100L, ar2);
}

void ar2()
{
  int khz = 38;
  unsigned int irSignal2[59] = {3168, 9856, 504, 1556, 536, 520, 508, 520, 512, 520, 508, 1576, 512, 504, 536, 520, 508, 520, 512, 520, 508, 520, 512, 520, 512, 1564, 512, 516, 532, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 508, 532, 504, 532, 1552, 512, 520, 508, 520, 512, 520, 512, 1548, 512, 528, 512, 520, 508, 1552, 512 }; //BOTA NO TURBO
  irsend.sendRaw(irSignal2, sizeof(irSignal2) / sizeof(irSignal2[0]), khz);
  taimer.setTimeout(100L, ar3);
}


void ar3()
{
  int khz = 38;
  unsigned int irSignal3[59] = {3168, 9824, 500, 1552, 532, 520, 512, 528, 508, 524, 508, 1552, 508, 532, 508, 508, 532, 512, 532, 1576, 512, 1548, 512, 528, 512, 520, 508, 520, 512, 520, 512, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 516, 532, 1556, 512, 520, 512, 1548, 512, 520, 512, 520, 508, 1552, 512, 1556, 512, 528, 512}; //Desliga as luzes
  irsend.sendRaw(irSignal3, sizeof(irSignal3) / sizeof(irSignal3[0]), khz);
  Blynk.virtualWrite(V3, LOW);
  uai = 0;
}

Are you sure you have initialized uai like as zero before setup ?

If I were you , I’ll try that

BLYNK_WRITE(V3)
{
  Serial.print(uai);

  if (param.asInt() == HIGH && !uai)
  {
    uai = 1;
    ar1();
  }
}

yes, if uai was not zero, v3 wouldn’t work, but thanks for the tip

That’s right !
So IDK where is the problem, I’ll try your sketch to see what happen

Ok, i’m waiting!

this is the sketch I modified, it works well :wink:

BLYNK_WRITE(V3){
    if (param.asInt() == 1 && uai == 0) {
    uai = 1;
    Serial.println("ar1");
    int khz = 38;
    unsigned int irSignal1[59] = {3272, 9840, 536, 1560, 512, 508, 532, 520, 508, 520, 512, 1544, 532, 504, 536, 520, 508, 528, 512, 516, 532, 520, 508, 520, 512, 508, 532, 520, 508, 520, 512, 520, 512, 512, 532, 520, 512, 520, 512, 1548, 512, 1548, 512, 520, 512, 1544, 532, 520, 512, 520, 512, 516, 512, 1560, 512, 1552, 532, 1536, 532}; //LIGA
    irsend.sendRaw(irSignal1, sizeof(irSignal1) / sizeof(irSignal1[0]), khz); //Note the approach used to automatically calculate the size of the array.
   
      timer.setTimeout(100L, []() { //Timer Lambda Function1
      Serial.println("ar2");
      int khz = 38;
      unsigned int irSignal2[59] = {3168, 9856, 504, 1556, 536, 520, 508, 520, 512, 520, 508, 1576, 512, 504, 536, 520, 508, 520, 512, 520, 508, 520, 512, 520, 512, 1564, 512, 516, 532, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 508, 532, 504, 532, 1552, 512, 520, 508, 520, 512, 520, 512, 1548, 512, 528, 512, 520, 508, 1552, 512 }; //BOTA NO TURBO
      irsend.sendRaw(irSignal2, sizeof(irSignal2) / sizeof(irSignal2[0]), khz);

        timer.setTimeout(100L, []() {  //Timer Lambda Function2
        Serial.println("ar3");
        int khz = 38;
        unsigned int irSignal3[59] = {3168, 9824, 500, 1552, 532, 520, 512, 528, 508, 524, 508, 1552, 508, 532, 508, 508, 532, 512, 532, 1576, 512, 1548, 512, 528, 512, 520, 508, 520, 512, 520, 512, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 516, 532, 1556, 512, 520, 512, 1548, 512, 520, 512, 520, 508, 1552, 512, 1556, 512, 528, 512}; //Desliga as luzes
         irsend.sendRaw(irSignal3, sizeof(irSignal3) / sizeof(irSignal3[0]), khz);
        Blynk.virtualWrite(V3, LOW);
        uai = 0;
      });  // END Timer Lambda Function2
    });  // END Timer Lambda Function1
  }
}


1 Like

Still having the same problem, but thanks for trying to help. : /

1 Like

Im
Maybe you have multiple timers?
That works well for me now.
I’m using blynk timer, not simple timer

I tested the code without the timer and it’s giving the same problem, so the problem is with the ir library, i already changed the name of the topic because of that, im using blynktimer too.

BLYNK_WRITE(V3)
{
  if (param.asInt() == HIGH && !uai)
  {
    uai = 1;
    ar1();
  }
}

void ar1() {
  int khz = 38;
  unsigned int irSignal1[59] = {3272, 9840, 536, 1560, 512, 508, 532, 520, 508, 520, 512, 1544, 532, 504, 536, 520, 508, 528, 512, 516, 532, 520, 508, 520, 512, 508, 532, 520, 508, 520, 512, 520, 512, 512, 532, 520, 512, 520, 512, 1548, 512, 1548, 512, 520, 512, 1544, 532, 520, 512, 520, 512, 516, 512, 1560, 512, 1552, 532, 1536, 532}; //LIGA
  irsend.sendRaw(irSignal1, sizeof(irSignal1) / sizeof(irSignal1[0]), khz); //Note the approach used to automatically calculate the size of the array.
  unsigned int irSignal2[59] = {3168, 9856, 504, 1556, 536, 520, 508, 520, 512, 520, 508, 1576, 512, 504, 536, 520, 508, 520, 512, 520, 508, 520, 512, 520, 512, 1564, 512, 516, 532, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 508, 532, 504, 532, 1552, 512, 520, 508, 520, 512, 520, 512, 1548, 512, 528, 512, 520, 508, 1552, 512 }; //BOTA NO TURBO
  irsend.sendRaw(irSignal2, sizeof(irSignal2) / sizeof(irSignal2[0]), khz);
  unsigned int irSignal3[59] = {3168, 9824, 500, 1552, 532, 520, 512, 528, 508, 524, 508, 1552, 508, 532, 508, 508, 532, 512, 532, 1576, 512, 1548, 512, 528, 512, 520, 508, 520, 512, 520, 512, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 516, 532, 1556, 512, 520, 512, 1548, 512, 520, 512, 520, 508, 1552, 512, 1556, 512, 528, 512}; //Desliga as luzes
  irsend.sendRaw(irSignal3, sizeof(irSignal3) / sizeof(irSignal3[0]), khz);
  Blynk.virtualWrite(V3, LOW);
  uai = 0;
}

try that to know if #include <IRremote.h> is the issue
I can’t compile with #include <IRremote.h> due to ESP32 not supported by IRremote lib.
IRremote.h:365:23: error: ‘SEND_PIN’ was not declared in this scope

const int sendPin = SEND_PIN;


BLYNK_WRITE(V3)
{
  Serial.println(uai);
 
   if (param.asInt() == 1 && uai == 0)
  {
    uai = 1;
 
    Serial.println("ar1");
    int khz = 38;
    unsigned int irSignal1[59] = {3272, 9840, 536, 1560, 512, 508, 532, 520, 508, 520, 512, 1544, 532, 504, 536, 520, 508, 528, 512, 516, 532, 520, 508, 520, 512, 508, 532, 520, 508, 520, 512, 520, 512, 512, 532, 520, 512, 520, 512, 1548, 512, 1548, 512, 520, 512, 1544, 532, 520, 512, 520, 512, 516, 512, 1560, 512, 1552, 532, 1536, 532}; //LIGA
    //irsend.sendRaw(irSignal1, sizeof(irSignal1) / sizeof(irSignal1[0]), khz); //Note the approach used to automatically calculate the size of the array.
    timer.setTimeout(100L, []() {
      Serial.println("ar2");
      int khz = 38;
      unsigned int irSignal2[59] = {3168, 9856, 504, 1556, 536, 520, 508, 520, 512, 520, 508, 1576, 512, 504, 536, 520, 508, 520, 512, 520, 508, 520, 512, 520, 512, 1564, 512, 516, 532, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 508, 532, 504, 532, 1552, 512, 520, 508, 520, 512, 520, 512, 1548, 512, 528, 512, 520, 508, 1552, 512 }; //BOTA NO TURBO
      //irsend.sendRaw(irSignal2, sizeof(irSignal2) / sizeof(irSignal2[0]), khz);

      timer.setTimeout(100L, []() {
        Serial.println("ar3");
        int khz = 38;
        unsigned int irSignal3[59] = {3168, 9824, 500, 1552, 532, 520, 512, 528, 508, 524, 508, 1552, 508, 532, 508, 508, 532, 512, 532, 1576, 512, 1548, 512, 528, 512, 520, 508, 520, 512, 520, 512, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 516, 532, 1556, 512, 520, 512, 1548, 512, 520, 512, 520, 508, 1552, 512, 1556, 512, 528, 512}; //Desliga as luzes
        // irsend.sendRaw(irSignal3, sizeof(irSignal3) / sizeof(irSignal3[0]), khz);
        Blynk.virtualWrite(V3, LOW);
        uai = 0;
      });  // END Timer Function
    });  // END Timer Function
  }
}

08:44:44.596 -> clockDisplay
08:44:44.596 -> 29196844
08:44:44.643 -> 08:44
08:44:44.689 -> 0 <----- uai=0, V3 is pushed
08:44:44.689 -> ar1
08:44:44.924 -> ar2
08:44:45.111 -> blinkLedWidget 1000 / 1000
08:44:45.205 -> ar3
08:44:45.814 -> blinkLedWidget 1000 / 1000

I wonder if there’s something else happening here that we aren’t seeing?

How are you verifying this behavior? Is it because your device isn’t responding to the IR codes the first time around, but it is the second?

As we aren’t seeing the full code, I wonder if the IRsend library isn’t being initialised in the code, but maybe calling it once has the effect of initialising it? do you have a line in your void setup that says irsend.begin();?

Maybe posting your full code would help?

Pete.

1 Like

im using this library (https://github.com/ExploreEmbedded/Arduino-IRremote) because is compatible with esp32 to send

I put irsend in the code and an error appeared., i don’t know what to do from this point, just change the library.

here’s the code:

//ir-5
//led vermelho-34
//led verde-35
//Servo-25

//pinos do rfid:
//sda preto-21
//sck branco-18
//mosi cinza-23
//miso roxo-19
//azul-nenhum
//ground verde-ground
//rst amarelo-22
//3v3 laranja-3v3

#define BLYNK_PRINT Serial // Blynk Serial Print
#include <WiFi.h> // WIFI
#include <WiFiClient.h> // cliente WIFI
#include <BlynkSimpleEsp32.h> // Blynk-ESP32
#include <MFRC522.h> //Biblioteca do leitor do cartão
#include <SPI.h> //Biblioteca da comunicação via SPI
#include <Servo.h> //Biblioteca do Servo
#include <IRremote.h> //Biblioteca do Led Infravermelho

#define BLYNK_PRINT DebugSerial
#define SS_PIN 21 //Pino do Leitor slave
#define RST_PIN 22 //Pino do Leitor reset
#define LED_G 33 //led verde
#define LED_R 32 //led vermelho
#define servopin 25 //pino do servo
MFRC522 mfrc522(SS_PIN, RST_PIN); //Início da comunicação c/ o leitor
BlynkTimer taimer; //Início do taimer
Servo myServo; //Nome do Servo
IRsend irsend; //Nome do IR
WidgetLED led(6); //Led(virtual) do Blynk

char auth[] = "a"; //Token do Blynk
char ssid[] = "b";
char pass[] = "c";
char timerfunccartao ; //Nome do Timer do cartão
char timerportaaberta ; //Nome do Timer da porta
int ServoPos; //Posição do Servo
bool uai; //Libera o servo

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

void setup()
{
  Serial.begin(115200); //Início da comunicação Serial
  SPI.begin(); //Início da comunicação do SPI
  pinMode(LED_G, OUTPUT); //Configura Led verde como saida
  pinMode(LED_R, OUTPUT); //Configura Led vermelho como saida
  mfrc522.PCD_Init();   // Início do RFID
  //irsend.begin();
  myServo.attach(servopin); //Liga o Servo
  myServo.write(140); //primeira posição do servo
  delay(3000); //delay de 5s
  myServo.detach(); //Desliga o Servo
  ServoPos = 140; //Iguala a posição do servo com a variável do servo
  uai = 0; //libera o servo pra ser usado
  Blynk.config(auth); //Início da configuração do Blynk
  WiFi.begin(ssid, pass);
 //taimer.setInterval(10L, funccartao); //cria a função timerfunccartao para ser chamada a cada 10 milisengundos
  taimer.setInterval(10L, portaaberta); //cria a função taimerportaaberta para ser chamada a cada 10 milisengundos
  taimer.setInterval(10000L, tentarreco);
}

BLYNK_WRITE(V1) {
  if (param.asInt() == HIGH && ServoPos == 15 && !uai)
  {
    uai = 1;
    digitalWrite(LED_G, HIGH);
    ServoPos = 140;
    myServo.attach(servopin);
    myServo.write(ServoPos);
    taimer.setTimeout(1000L, contblynkwrite1);
  }
  else {
    Blynk.virtualWrite(V1, LOW);
  }
}

void contblynkwrite1() {
  myServo.detach();
  Blynk.virtualWrite(V1, LOW);
  Blynk.virtualWrite(V2, LOW);
  digitalWrite(LED_G, LOW);
  ledRligdesl();
}

void ledRligdesl() {
  digitalWrite(LED_R, HIGH);
  taimer.setTimeout(1000L, ledRdesl);
}

void ledRdesl() {
  digitalWrite(LED_R, LOW);
  uai = 0;
}

BLYNK_WRITE(V2)
{
  if (param.asInt() == HIGH && ServoPos == 140 && !uai)
  {
    uai = 1;
    digitalWrite(LED_G, HIGH);
    myServo.attach(servopin);
    ServoPos = 15;
    myServo.write(ServoPos);
    taimer.setTimeout(1000L, contblynkwrite1);
  }
  else {
    Blynk.virtualWrite(V2, LOW);
  }
}

BLYNK_WRITE(V3)
{
  if (param.asInt() == HIGH && !uai)
  {
    uai = 1;
    ar1();
  }
}

void ar1() {
  int khz = 38;
  unsigned int irSignal1[59] = {3272, 9840, 536, 1560, 512, 508, 532, 520, 508, 520, 512, 1544, 532, 504, 536, 520, 508, 528, 512, 516, 532, 520, 508, 520, 512, 508, 532, 520, 508, 520, 512, 520, 512, 512, 532, 520, 512, 520, 512, 1548, 512, 1548, 512, 520, 512, 1544, 532, 520, 512, 520, 512, 516, 512, 1560, 512, 1552, 532, 1536, 532}; //LIGA
  irsend.sendRaw(irSignal1, sizeof(irSignal1) / sizeof(irSignal1[0]), khz); //Note the approach used to automatically calculate the size of the array.
  taimer.setTimeout(100L, ar2);
}

void ar2()
{
  int khz = 38;
  unsigned int irSignal2[59] = {3168, 9856, 504, 1556, 536, 520, 508, 520, 512, 520, 508, 1576, 512, 504, 536, 520, 508, 520, 512, 520, 508, 520, 512, 520, 512, 1564, 512, 516, 532, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 508, 532, 504, 532, 1552, 512, 520, 508, 520, 512, 520, 512, 1548, 512, 528, 512, 520, 508, 1552, 512 }; //BOTA NO TURBO
  irsend.sendRaw(irSignal2, sizeof(irSignal2) / sizeof(irSignal2[0]), khz);
  taimer.setTimeout(100L, ar3);
}


void ar3()
{
  int khz = 38;
  unsigned int irSignal3[59] = {3168, 9824, 500, 1552, 532, 520, 512, 528, 508, 524, 508, 1552, 508, 532, 508, 508, 532, 512, 532, 1576, 512, 1548, 512, 528, 512, 520, 508, 520, 512, 520, 512, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 516, 532, 1556, 512, 520, 512, 1548, 512, 520, 512, 520, 508, 1552, 512, 1556, 512, 528, 512}; //Desliga as luzes
  irsend.sendRaw(irSignal3, sizeof(irSignal3) / sizeof(irSignal3[0]), khz);
  Blynk.virtualWrite(V3, LOW);
  uai = 0;
}


BLYNK_WRITE(V4)
{
  if (param.asInt() == HIGH && !uai) {
    uai = 1;
    taimer.setTimeout(100L, ardesl);
  }
}

void ardesl() {
  int khz = 38;
  unsigned int irSignal[59] = {3128, 9836, 524, 1556, 536, 528, 508, 520, 512, 520, 508, 1560, 512, 508, 532, 504, 532, 520, 512, 1548, 512, 1552, 508, 520, 512, 520, 512, 520, 512, 516, 512, 520, 512, 520, 508, 524, 508, 520, 512, 528, 508, 524, 508, 520, 512, 1556, 512, 520, 512, 1556, 512, 528, 512, 508, 532, 512, 532, 1560, 508}; //Desliga
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
  Blynk.virtualWrite(V4, LOW);
  uai = 0;
}

BLYNK_WRITE(V5)
{
  if (param.asInt() == HIGH && !uai) {
    uai = 1;
    taimer.setTimeout(100L, projon);
  }
}

void projon() {
  int khz = 38;
  unsigned int irSignal[68] = {8850, 4350, 600, 1650, 550, 550, 600, 550, 600, 550, 550, 1650, 600, 1600, 600, 550, 600, 550, 550, 550, 600, 1650, 550, 1650, 600, 1600, 600, 550, 550, 550, 600, 1650, 550, 1650, 600, 1600, 600, 550, 600, 550, 550, 550, 600, 550, 600, 550, 550, 550, 600, 1650, 550, 550, 600, 1650, 550, 1650, 600, 1600, 600, 1600, 600, 1650, 550, 1650, 600, 550, 550}; // liga/desliga o projetor
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
  Blynk.virtualWrite(V5, LOW);
  uai = 0;
}

void portaaberta()
{
  if (ServoPos == 140) { //while trava o blynk
    led.on();
  }
  else if (ServoPos == 15) {
    led.off();
  }
}


void funccartao()
{
  if (! uai) {
    if ( ! mfrc522.PICC_IsNewCardPresent())
    {
      return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial())
    {
      return;
    }
    String content = "";
    byte letter;
    for (byte i = 0; i < mfrc522.uid.size; i++)
    {
      content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
      content.concat(String(mfrc522.uid.uidByte[i], HEX));
    }
    content.toUpperCase();
    if (content.substring(1) == "84 63 4C 79" || content.substring(1) == "26 CC 78 89" || content.substring(1) == "08 B9 42 54" )
    {
      uai = 1;
      //  timer.disable(timerfunccartao);
      taimer.setTimeout(300L, p1);
    }
    else  {
      uai = 1;
      //  timer.disable(timerfunccartao);
      digitalWrite(LED_R, HIGH);
      taimer.setTimeout(10000L, p3);
    }
  }
}


void loop() {
  taimer.run();
  funccartao();
  if (Blynk.connected() == true)
  {
    Blynk.run();
  }

}

void conectou()
{
  if (Blynk.connected() == false)
  {
    funccartao();
  }
}
void tentarreco()
{
  if (!uai)
  {
    if (WiFi.status() != WL_CONNECTED || !Blynk.connected())
    {
      digitalWrite(LED_R, HIGH);
      WiFi.begin(ssid, pass);
      delay(1000);
      if (WiFi.status() == WL_CONNECTED) {
        Blynk.config(auth);
        Blynk.connect();
        digitalWrite(LED_R, LOW);
      }
      else
      {
        digitalWrite(LED_R, LOW);
      }
    }
  }
}


void p1()
{
  digitalWrite(LED_G, HIGH);
  if (ServoPos == 15)
  {
    ServoPos = 140;
    Blynk.notify("Porta trancada com cartão.");
    myServo.attach(servopin);
    myServo.write(ServoPos);
    taimer.setTimeout(3000L, p2);
  }
  else
  {
    ServoPos = 15;
    Blynk.notify("Porta aberta com cartão.");
    myServo.attach(servopin);
    myServo.write(ServoPos);
    taimer.setTimeout(3000L, p2);
  }
}


void p2()
{
  myServo.detach();
  digitalWrite(LED_G, LOW);
  //timer.enable(timerfunccartao);
  uai = 0;
}


void p3() {
  //  timer.enable(timerfunccartao);
  digitalWrite(LED_R, LOW);
  uai = 0;
}

it’s a little messy, if you want me to translate something just talk

Looking at the library that you’re using there’s no real documentation about how to use the various function calls that are available. The developers seem to spend more time thanking each other for their contributions rather than documentation it’s usage.

I’m not inclined to install this library on my PC, as I use the IRremoteESP8266 library in a few current projects and I don’t want to break that (and I cant be bothered to do a mobile install of the IDE as it takes so long to setup all the libraries).

Maybe a workaround is to call a dummy irsend command in your void setup, to kick-start the library into send mode?

Pete.

1 Like

I tried to put a dummy irsend in the void setup but kept giving the same problem.

What I don’t understand is when I call v4 and v5 does not happen, I believe it is some incompatibility with blynktimer, I am starting to consider using another library.

The IRremoteESP8266 supports sendRaw?

Yes, that’s how I use it most of the time. Also, despite it’s name it does support ESP32 as well, according to the documentation.

Pete.

1 Like

I changed the library and it worked like a charm!

Thanks for the help.

//ir-5
//led vermelho-34
//led verde-35
//Servo-25

//pinos do rfid:
//sda preto-21
//sck branco-18
//mosi cinza-23
//miso roxo-19
//azul-nenhum
//ground verde-ground
//rst amarelo-22
//3v3 laranja-3v3

#define BLYNK_PRINT Serial // Blynk Serial Print
#include <WiFi.h> // WIFI
#include <WiFiClient.h> // cliente WIFI
#include <BlynkSimpleEsp32.h> // Blynk-ESP32
#include <MFRC522.h> //Biblioteca do leitor do cartão
#include <SPI.h> //Biblioteca da comunicação via SPI
#include <Servo.h> //Biblioteca do Servo
//#include <IRremote.h> //Biblioteca do Led Infravermelho
#include <IRremoteESP8266.h>
#include <IRsend.h>

#define BLYNK_PRINT DebugSerial
#define SS_PIN 21 //Pino do Leitor slave
#define RST_PIN 22 //Pino do Leitor reset
#define LED_G 33 //led verde
#define LED_R 32 //led vermelho
#define servopin 25 //pino do servo
MFRC522 mfrc522(SS_PIN, RST_PIN); //Início da comunicação c/ o leitor
BlynkTimer taimer; //Início do taimer
Servo myServo; //Nome do Servo
IRsend irsend(5); //Nome do IR
WidgetLED led(6); //Led(virtual) do Blynk

char auth[] = "uno"; //Token do Blynk
char ssid[] = "dos";
char pass[] = "tres";
char timerfunccartao ; //Nome do Timer do cartão
char timerportaaberta ; //Nome do Timer da porta
int ServoPos; //Posição do Servo
bool uai; //Libera o servo

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

void setup()
{
  Serial.begin(115200); //Início da comunicação Serial
  SPI.begin(); //Início da comunicação do SPI
  pinMode(LED_G, OUTPUT); //Configura Led verde como saida
  pinMode(LED_R, OUTPUT); //Configura Led vermelho como saida
  mfrc522.PCD_Init();   // Início do RFID
  irsend.begin();
  myServo.attach(servopin); //Liga o Servo
  myServo.write(140); //primeira posição do servo
  delay(3000); //delay de 5s
  myServo.detach(); //Desliga o Servo
  ServoPos = 140; //Iguala a posição do servo com a variável do servo
  uai = 0; //libera o servo pra ser usado
  Blynk.config(auth); //Início da configuração do Blynk
  WiFi.begin(ssid, pass);
  //taimer.setInterval(10L, funccartao); //cria a função timerfunccartao para ser chamada a cada 10 milisengundos
  taimer.setInterval(10L, portaaberta); //cria a função taimerportaaberta para ser chamada a cada 10 milisengundos
  taimer.setInterval(10000L, tentarreco);
}

BLYNK_WRITE(V1) {
  if (param.asInt() == HIGH && ServoPos == 15 && !uai)
  {
    uai = 1;
    digitalWrite(LED_G, HIGH);
    ServoPos = 140;
    myServo.attach(servopin);
    myServo.write(ServoPos);
    taimer.setTimeout(1000L, contblynkwrite1);
  }
  else {
    Blynk.virtualWrite(V1, LOW);
  }
}

void contblynkwrite1() {
  myServo.detach();
  Blynk.virtualWrite(V1, LOW);
  Blynk.virtualWrite(V2, LOW);
  digitalWrite(LED_G, LOW);
  ledRligdesl();
}

void ledRligdesl() {
  digitalWrite(LED_R, HIGH);
  taimer.setTimeout(1000L, ledRdesl);
}

void ledRdesl() {
  digitalWrite(LED_R, LOW);
  uai = 0;
}

BLYNK_WRITE(V2)
{
  if (param.asInt() == HIGH && ServoPos == 140 && !uai)
  {
    uai = 1;
    digitalWrite(LED_G, HIGH);
    myServo.attach(servopin);
    ServoPos = 15;
    myServo.write(ServoPos);
    taimer.setTimeout(1000L, contblynkwrite1);
  }
  else {
    Blynk.virtualWrite(V2, LOW);
  }
}

BLYNK_WRITE(V3)
{
  if (param.asInt() == HIGH && !uai)
  {
    uai = 1;
    ar1();
  }
}
int khz = 38;
uint16_t ligaac[59] = {3272, 9840, 536, 1560, 512, 508, 532, 520, 508, 520, 512, 1544, 532, 504, 536, 520, 508, 528, 512, 516, 532, 520, 508, 520, 512, 508, 532, 520, 508, 520, 512, 520, 512, 512, 532, 520, 512, 520, 512, 1548, 512, 1548, 512, 520, 512, 1544, 532, 520, 512, 520, 512, 516, 512, 1560, 512, 1552, 532, 1536, 532}; //LIGA
uint16_t turboac[59] = {3168, 9856, 504, 1556, 536, 520, 508, 520, 512, 520, 508, 1576, 512, 504, 536, 520, 508, 520, 512, 520, 508, 520, 512, 520, 512, 1564, 512, 516, 532, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 508, 532, 504, 532, 1552, 512, 520, 508, 520, 512, 520, 512, 1548, 512, 528, 512, 520, 508, 1552, 512 }; //BOTA NO TURBO
uint16_t luzdeslac[59] = {3168, 9824, 500, 1552, 532, 520, 512, 528, 508, 524, 508, 1552, 508, 532, 508, 508, 532, 512, 532, 1576, 512, 1548, 512, 528, 512, 520, 508, 520, 512, 520, 512, 520, 508, 520, 512, 520, 512, 520, 508, 524, 508, 516, 532, 1556, 512, 520, 512, 1548, 512, 520, 512, 520, 508, 1552, 512, 1556, 512, 528, 512}; //Desliga as luzes

void ar1() {
  irsend.sendRaw(ligaac, 59, khz);
  taimer.setTimeout(600L, ar2);
}

void ar2()
{
  irsend.sendRaw(turboac, 59, khz);
  taimer.setTimeout(600L, ar3);
}


void ar3()
{
  irsend.sendRaw(luzdeslac, 59, khz);
  Blynk.virtualWrite(V3, LOW);
  uai = 0;
}


BLYNK_WRITE(V4)
{
  if (param.asInt() == HIGH && !uai) {
    uai = 1;
    ardesl();
  }
}

uint16_t deslac[59] = {3128, 9836, 524, 1556, 536, 528, 508, 520, 512, 520, 508, 1560, 512, 508, 532, 504, 532, 520, 512, 1548, 512, 1552, 508, 520, 512, 520, 512, 520, 512, 516, 512, 520, 512, 520, 508, 524, 508, 520, 512, 528, 508, 524, 508, 520, 512, 1556, 512, 520, 512, 1556, 512, 528, 512, 508, 532, 512, 532, 1560, 508}; //Desliga

void ardesl() {
  irsend.sendRaw(deslac, 59, khz);
  Blynk.virtualWrite(V4, LOW);
  uai = 0;
}

BLYNK_WRITE(V5)
{
  if (param.asInt() == HIGH && !uai) {
    uai = 1;
    projon();
  }
}

uint16_t projeon[68] = {8850, 4350, 600, 1650, 550, 550, 600, 550, 600, 550, 550, 1650, 600, 1600, 600, 550, 600, 550, 550, 550, 600, 1650, 550, 1650, 600, 1600, 600, 550, 550, 550, 600, 1650, 550, 1650, 600, 1600, 600, 550, 600, 550, 550, 550, 600, 550, 600, 550, 550, 550, 600, 1650, 550, 550, 600, 1650, 550, 1650, 600, 1600, 600, 1600, 600, 1650, 550, 1650, 600, 550, 550}; // liga/desliga o projetor

void projon() {
  irsend.sendRaw(projeon, 68, khz);
  Blynk.virtualWrite(V5, LOW);
  uai = 0;
}

void portaaberta()
{
  if (ServoPos == 140) { //while trava o blynk
    led.on();
  }
  else if (ServoPos == 15) {
    led.off();
  }
}


void funccartao()
{
  if (! uai) {
    if ( ! mfrc522.PICC_IsNewCardPresent())
    {
      return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial())
    {
      return;
    }
    String content = "";
    byte letter;
    for (byte i = 0; i < mfrc522.uid.size; i++)
    {
      content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
      content.concat(String(mfrc522.uid.uidByte[i], HEX));
    }
    content.toUpperCase();
    if (content.substring(1) == "84 63 4C 79" || content.substring(1) == "26 CC 78 89" || content.substring(1) == "08 B9 42 54" )
    {
      uai = 1;
      //  timer.disable(timerfunccartao);
      taimer.setTimeout(300L, p1);
    }
    else  {
      uai = 1;
      //  timer.disable(timerfunccartao);
      digitalWrite(LED_R, HIGH);
      taimer.setTimeout(10000L, p3);
    }
  }
}


void loop() {
  taimer.run();
  funccartao();
  if (Blynk.connected() == true)
  {
    Blynk.run();
  }
}

void tentarreco()
{
  if (!uai)
  {
    if (WiFi.status() != WL_CONNECTED || !Blynk.connected())
    {
      digitalWrite(LED_R, HIGH);
      WiFi.begin(ssid, pass);
      delay(1000);
      if (WiFi.status() == WL_CONNECTED) {
        Blynk.config(auth);
        Blynk.connect();
        digitalWrite(LED_R, LOW);
      }
      else
      {
        digitalWrite(LED_R, LOW);
      }
    }
  }
}


void p1()
{
  digitalWrite(LED_G, HIGH);
  if (ServoPos == 15)
  {
    ServoPos = 140;
    Blynk.notify("Porta trancada com cartão.");
    myServo.attach(servopin);
    myServo.write(ServoPos);
    taimer.setTimeout(3000L, p2);
  }
  else
  {
    ServoPos = 15;
    Blynk.notify("Porta aberta com cartão.");
    myServo.attach(servopin);
    myServo.write(ServoPos);
    taimer.setTimeout(3000L, p2);
  }
}


void p2()
{
  myServo.detach();
  digitalWrite(LED_G, LOW);
  //timer.enable(timerfunccartao);
  uai = 0;
}


void p3() {
  //  timer.enable(timerfunccartao);
  digitalWrite(LED_R, LOW);
  uai = 0;
}
1 Like