sábado, 20 de mayo de 2017

Relay in Aduino uno & DS3132 timer

Mayo 2017
Aduino uno Relay & DS3132 timer





#include <Wire.h>
#include "RTClib.h"
 #define RELAY_ON 0
#define RELAY_OFF 1
#define RELAY_1  9

const int outputPin = 9; // conectar activador de Relay
bool state = false;

// RTC_DS1307 rtc;
RTC_DS3231 rtc;

void setup() {
   Serial.begin(9600);
   delay(1000);

 // Set pin as output.
  pinMode(RELAY_1, OUTPUT);

  // Initialize relay one as off so that on reset it would be off by default
  digitalWrite(RELAY_1, RELAY_OFF);
 //pinMode(9, OUTPUT);

   if (!rtc.begin()) {
      Serial.println(F("Couldn't find RTC"));
      while (1);
   }

   if (rtc.lostPower()) {
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
   }
}

// Comprobar si esta programado el encendido
bool isScheduledON(DateTime date)
{
   int weekDay = date.dayOfTheWeek();
   float hours = date.hour() + date.minute() / 60.0;

   // De 09:30 a 11:30 y de 21:00 a 23:00
   bool hourCondition = (hours > 6.30 && hours < 15.50) || (hours > 16.00 && hours < 6.00);

   // Miercoles, Sabado o Domingo
   bool dayCondition = (weekDay == 1 || weekDay == 2 || weekDay == 3|| weekDay == 4|| weekDay == 5|| weekDay == 6); 
   if (hourCondition && dayCondition)
   {
      return true;
   }
   return false;
}

void loop() {
   DateTime now = rtc.now();

   if (state == false && isScheduledON(now))      // Apagado y debería estar encendido
   {
      digitalWrite(RELAY_1, LOW);
      state = true;
    // Serial.print("Activado");
   }
   else if (state == true && !isScheduledON(now))  // Encendido y deberia estar apagado
   {
      digitalWrite(RELAY_1, HIGH);
      state = false;
      //Serial.print("apaga Relay");
   }

  // delay(3000);
}

domingo, 9 de octubre de 2016

Arduino uno +RFDI522 y Relay

CONTROL DE ACCESO CON TARJETA RFID522 Y SERVO




Pasos a seguir

  1. Unir la tarjeta RFID-RC522 con  Arduino Uno
  2. Unir el resto de las piesas con Arduino Uno
  3. Sketch  Arduino UNO

Conecciones del RFID-RC522 con Arduino UNO

Arduino UNO | RFID-RC522
 Pin 10          SDA
 Pin 13          SCK
 Pin 11          MOSI
 Pin 12          MISO
 -               IRQ
 GND             GND
 Pin9            RST
 3.3V            3.3V

Sketch para  Arduino UNO

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10 // RFID-RC522 SDA
#define RST_PIN 9 // RFID-RC522 RST
const byte servoPin  = 8;  // digital Pin - Servo
const byte buzzerPin = 7;  // digital Pin - Buzzer
Servo myServo;
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Colocar aqui los numeros de tarjetas autorizadas
// Al abrir el monitor dara el numero rechazado primero
long erlaubteKarten[] = { 1011520, 2492530, 171762080 };
byte erlaubteKartenCount = 3;
void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(buzzerPin, OUTPUT);
  pinMode(servoPin, OUTPUT);
  myServo.attach(servoPin);
  myServo.write(0); //Servo en lugar inicio
}
void loop() {
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  long code=0;
   
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    code=((code+mfrc522.uid.uidByte[i])*10);
  }
  Serial.print(F("Tarjeta reconosida:"));
  Serial.print(code);
  if(PruefeKartenberechtigung(code) == true) {
    Serial.println(F(" [AUTORIZADO]"));
    KarteErkanntSound();
    myServo.write(90);
    delay(500);
    myServo.write(0);
    delay(100);
  } else {
    Serial.println(F(" [SIN AUTORIZACION]"));
    UnbekannteKarteSound();
  }
  delay(2000);
}
boolean PruefeKartenberechtigung(long KartenUUID) {
  boolean erlaubt = false;
   
  for(int i=0; i < erlaubteKartenCount; i++) {
    if(erlaubteKarten[i] == KartenUUID) {
      erlaubt = true;
    }
  }
  return erlaubt;
}
void UnbekannteKarteSound(void) {
  for (byte i=0; i<3; i++) {
    tone(buzzerPin, 500, 150);
    delay(200);
  }
}
void KarteErkanntSound(void) {
  tone(buzzerPin, 1000, 150);
  delay(200);
  tone(buzzerPin, 1500, 150);
  delay(200);
}