Skip to content

Instantly share code, notes, and snippets.

@GamberrO
Created April 19, 2015 23:01
Show Gist options
  • Save GamberrO/314b2b909d8a4681c343 to your computer and use it in GitHub Desktop.
Save GamberrO/314b2b909d8a4681c343 to your computer and use it in GitHub Desktop.
TimeLapse_RTC_Ivan_3_canales_v3_Corte_de_Carga_Boton_Foto_IR_SE (JOSE)
#include <Wire.h> // For some strange reasons, Wire.h must be included here
#include <DS1307new.h>
#include <IRremote.h>
#include <Servo.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
//Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
//int pos = 0; // variable to store the servo position
int iPrevRtcMinute = -1; // variable to store the previous minute
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 CARGAR = 8;
int LED = 9;
int BOTON = 11;
//int SERVO = 12;
int constante = 0;
int horaArranque = 6;
int horaFinal = 19;
int horaCarga = 8;
int horaCorte = 17;
// *********************************************
// SETUP
// *********************************************
void setup(){
pinMode(BOTON, INPUT);
pinMode(LED, OUTPUT);
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,02,8);
// RTC.fillByHMS(01,32,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 == 4294967295) { //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
digitalWrite (PRENDER , LOW); // Prende la camara
digitalWrite (LED , HIGH); // Prende
delay (120000);
} else {
// digitalWrite (FOTO, HIGH); // Libera Sacar FOTO
digitalWrite (PRENDER , HIGH); // Apaga la camara
digitalWrite (LED, LOW); // Apaga
}
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()) {
// Saco la primer foto///////////////////////////////////////////////////////////////
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(48250); // Delay para que no saque mas de una foto por minuto
}
} else {
// Serial.println("Fuera de horario");
digitalWrite (FOTO , HIGH);
}
/*
if(RTC.hour >= horaArranque && RTC.hour <= horaFinal) {
// Serial.println("Dentro de horario");
if (debeSacarFoto()) {
// Saco la primer foto///////////////////////////////////////////////////////////////
myservo.attach(SERVO); // attaches the servo on pin 12 to the servo object
delay(1500);
myservo.write(25); // tell servo to go to position in variable 'pos'
delay(1500);
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(2000); // Delay ENTRE FOTO TELE Y FOTO ANGULAR
// Saco la primer foto///////////////////////////////////////////////////////////////
myservo.write(165); // tell servo to go to position in variable 'pos'
delay(1500);
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(2500);
myservo.write(25);
delay(1000);
myservo.detach(); // Apaga El SERVO
//delay(48250); // Delay para que no saque 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() {
// variable para controlar si cambió el minuto
bool bCycleChanged = false;
// variable para determinar si estoy en un minuto en el que deba sacar foto
bool bInSnapshotMinute = false;
// valor de retrorno de la función
bool bDebeSacarFotoRet = false;
if (iPrevRtcMinute == -1)
bDebeSacarFotoRet = true;
else
{
// si se cambia de minuto, se debe sacar la foto
if (iPrevRtcMinute != RTC.minute ) bCycleChanged = true;
// siempre y cuando sea uno de los miutos correctos para tomar la foto
if ( RTC.minute == 00 || RTC.minute == 05
|| RTC.minute == 10 || RTC.minute == 15
|| RTC.minute == 20 || RTC.minute == 25
|| RTC.minute == 30 || RTC.minute == 35
|| RTC.minute == 40 || RTC.minute == 45
|| RTC.minute == 50 || RTC.minute == 55 ) bInSnapshotMinute = true;
// true cuando se cumplen la dos condiciones
bDebeSacarFotoRet = bCycleChanged && bInSnapshotMinute;
};
iPrevRtcMinute = RTC.minute;
return (bDebeSacarFotoRet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment