Skip to content

Instantly share code, notes, and snippets.

@GamberrO
Last active August 29, 2015 14:13
Show Gist options
  • Save GamberrO/dd33f14124786413be13 to your computer and use it in GitHub Desktop.
Save GamberrO/dd33f14124786413be13 to your computer and use it in GitHub Desktop.
#include <Wire.h> // For some strange reasons, Wire.h must be included here
#include <DS1307new.h>
#include <IRremote.h>
// *********************************************
// DEFINE
// *********************************************
// *********************************************
// VARIABLES
// *********************************************
uint16_t startAddr = 0x0000; // Start address to store in the NV-RAM
uint16_t lastAddr; // new address for storing in NV-RAM
uint16_t TimeIsSet = 0xaa55; // Helper that time must not set again
const int MAIN_LOOP_DELAY = 1000; // Intervalo para preguntarle la hora al Reloj: Setear en 1000 ms
const int DURACION_PULSO = 250; // Setear en 500 ms
const int LIBERAR_PRENDER = 250; // Setear en 500 ms
const int DELAY_ENCENDIDO = 500; // Tiempo entre el ENCENDIDO y la FOTO
//const int INTERVALO = 3; // Tiempo de INTERVALO para intervalometro simple
const int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;
int FOTO = 6;
int PRENDER = 7;
//int LED = 10;
int BOTON = 11;
//int CARGAR = 8;
int constante = 0;
int horaArranque = 7;
int horaFinal = 18;
//int horaCarga = 12;
//int horaCorte = 16;
// *********************************************
// SETUP
// *********************************************
void setup(){
pinMode(BOTON, INPUT);
// pinMode(LED, OUTPUT);
// digitalWrite(2, LOW); // Test of the SQW pin, D2 = Pullup on
pinMode(PRENDER, OUTPUT);
pinMode(FOTO, OUTPUT);
//pinMode(CARGAR, OUTPUT);
digitalWrite(PRENDER, HIGH);
digitalWrite(FOTO, HIGH);
//digitalWrite(CARGAR, HIGH);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
/*
PLEASE NOTICE: WE HAVE MADE AN ADDRESS SHIFT FOR THE NV-RAM!!!
NV-RAM ADDRESS 0x08 HAS TO ADDRESSED WITH ADDRESS 0x00=0
TO AVOID OVERWRITING THE CLOCK REGISTERS IN CASE OF
ERRORS IN YOUR CODE. SO THE LAST ADDRESS IS 0x38=56!
*/
RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08
/*
Uncomment the next 2 lines if you want to SET the clock
Comment them out if the clock is set.
DON'T ASK ME WHY: YOU MUST UPLOAD THE CODE TWICE TO LET HIM WORK
AFTER SETTING THE CLOCK ONCE.
*/
// TimeIsSet = 0xffff;
// RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
/*
Control the clock.
Clock will only be set if NV-RAM Address does not contain 0xaa.
DS1307 should have a battery backup.
*/
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet != 0xaa55)
{
RTC.stopClock();
//LAS DOS LINEAS DE ABAJO SETEAN EL RELOJ
// RTC.fillByYMD(2015,01,20);
// RTC.fillByHMS(00,47,00);
RTC.setTime();
TimeIsSet = 0xaa55;
RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.startClock();
}
{
RTC.getTime();
}
/*
Control Register for SQW pin which can be used as an interrupt.
*/
RTC.ctrl = 0x00; // 0x00=disable SQW pin, 0x10=1Hz,
// 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
RTC.setCTRL();
Serial.println("Tiempo Actual");
uint8_t MESZ;
Serial.println();
}
// *********************************************
// MAIN (LOOP)
// *********************************************
void loop() {
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print("NEC: ");
} else if (results.decode_type == SONY) {
Serial.print("SONY: ");
} else if (results.decode_type == RC5) {
Serial.print("RC5: ");
} else if (results.decode_type == RC6) {
Serial.print("RC6: ");
} else if (results.decode_type == UNKNOWN) {
Serial.print("UNKNOWN: ");
}
Serial.println(results.value);
irrecv.resume(); // Receive the next value
if (results.value == 849) { //Remoto SONY CD Player (Boton REPEAT)
Serial.println("REMOTO PRENDER_ON");
digitalWrite (PRENDER , LOW); // Prende la camara
}
if (results.value == 4017) { //Remoto SONY CD Player (Boton FADER)
Serial.println("REMOTO PRENDER_OFF");
digitalWrite (PRENDER , HIGH); // Apaga la camara
}
if (results.value == 1233) { //Remoto SONY CD Player (Boton PLAY)
Serial.println("Sacar Foto Con REMOTO");
digitalWrite (FOTO , LOW); // Sacar FOTO
delay (DURACION_PULSO);
digitalWrite (FOTO, HIGH); // Libera Sacar FOTO
delay(1000);
}
else {
digitalWrite (FOTO, HIGH); // Libera Sacar FOTO
}
}
/*
if(digitalRead(BOTON) == HIGH) {
Serial.println("Boton prendido");
digitalWrite (PRENDER , LOW); // Prende la camara
delay (DELAY_ENCENDIDO);
} else {
digitalWrite (PRENDER, HIGH); // Apaga la camara
}
*/
if(digitalRead(BOTON) == HIGH) {
Serial.println("Sacar Foto Con Boton");
digitalWrite (FOTO , LOW); // Sacar FOTO
delay (DURACION_PULSO);
} else {
digitalWrite (FOTO, HIGH); // Libera Sacar FOTO
}
RTC.getTime();
// if(RTC.hour >= horaCarga && RTC.hour <= horaCorte) {
// digitalWrite (CARGAR , LOW);}
//
// else {
// digitalWrite (CARGAR , HIGH);
// }
if(RTC.hour >= horaArranque && RTC.hour <= horaFinal) {
// Serial.println("Dentro de horario");
if (debeSacarFoto()) {
Serial.println("Debe sacar foto");
digitalWrite (PRENDER , LOW); // Prende la camara
delay (DELAY_ENCENDIDO);
sacarFoto();
delay (LIBERAR_PRENDER);
digitalWrite (PRENDER , HIGH); // Apaga la camara
delay(59000); // Delay para que no saca mas de una foto por minuto
}
} else {
// Serial.println("Fuera de horario");
digitalWrite (FOTO , HIGH);
}
imprimirTiempo();
uint8_t MESZ = RTC.isMEZSummerTime();
RTC.getRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
lastAddr = lastAddr + 1; // we want to use it as addresscounter for example
RTC.setRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
// if (TimeIsSet == 0xaa55) { // check if the clock was set or not
//
// }
delay(MAIN_LOOP_DELAY);
}
void sacarFoto() {
Serial.print("Sacando foto: ");
imprimirTiempo();
digitalWrite (FOTO , LOW);
delay (DURACION_PULSO);
digitalWrite (FOTO , HIGH);
}
void imprimirTiempo() {
RTC.getTime();
String ESPACIO = " ";
String tiempo = ESPACIO;
// tiempo = tiempo + RTC.day;
// tiempo = tiempo + ESPACIO;
tiempo = tiempo + RTC.hour, DEC;
tiempo = tiempo + ESPACIO;
tiempo = tiempo + RTC.minute, DEC;
tiempo = tiempo + ESPACIO;
tiempo = tiempo + RTC.second, DEC;
Serial.println(tiempo);
// Serial.print (RTC.dow );
// Serial.print (" ");
// Serial.print (RTC.hour, DEC);
// Serial.print (" ");
// Serial.print (RTC.minute, DEC);
// Serial.print (" ");
// Serial.println (RTC.second);
}
boolean debeSacarFoto() {
// return ((RTC.minute % INTERVALO) == 0) ; // Intervalometro Simple
return (RTC.minute == 00 || RTC.minute == 15 || RTC.minute == 30 || RTC.minute == 45) ;
// return (RTC.minute == 39 || RTC.minute == 41 || RTC.minute == 43 || RTC.minute == 45 || RTC.minute == 47 || RTC.minute == 49 || RTC.minute == 45 || RTC.minute == 47) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment