Está en la página 1de 4

/**************************************************************************

**************************************************************************/

#include <RBDdimmer.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels


#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)


// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C
for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16

dimmerLamp dimmer(18, 5); //se crea onjeto Dimmer


int outVal = 0;

#define SENSOR_PIN 39
#define REFERENCE_RESISTANCE 10000.0
#define NOMINAL_RESISTANCE 10000.0
#define NOMINAL_TEMPERATURE 298.00
#define B_VALUE 3950.0
#define ADC_RESOLUTION 4096.0

float resistencia = 0; //resistencia sensor NTC 10K 3950


float invTemperatura = 0;
float temperatura = 0;

tmElements_t tm; //objeto de tiempo para el modulo RTC DS1307


int dutyCycle = 50; //ciclo de trabajo salidas PWM

void setup() {

pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);

pinMode(2, OUTPUT);
pinMode(4, OUTPUT);

pinMode(19, OUTPUT);
pinMode(23, OUTPUT);

pinMode(26, INPUT_PULLUP); //pin 26 entrada con resistencia de pull up habilitada


pinMode(27, INPUT_PULLUP); //pin 27 entrada con resistencia de pull up habilitada

pinMode(32, INPUT_PULLUP); //pin 32 entrada con resistencia de pull up


habilitada
pinMode(33, INPUT_PULLUP); //pin 33 entrada con resistencia de pull up
habilitada
pinMode(34, INPUT); //el pin 34 no tiene resistencia de pull up interna, es
externa
pinMode(35, INPUT); //el pin 35 no tiene resistencia de pull up interna, es
externa

Serial.begin(9600);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally


if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}

// Clear the buffer


display.clearDisplay();

// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text

//se define la hora inicial del reloj DS1307


tm.Hour = 10;
tm.Minute = 35;
tm.Second = 10;
tm.Day = 19;
tm.Month = 1;
tm.Year = 2023;
RTC.write(tm); //escribe al RTC DS1307

ledcSetup(0, 5000, 8); //canal 0 PWM, 5Khz, 8 bits


// attach the channel to the GPIO to be controlled
ledcAttachPin(23, 0); //Canal 0 PWM a pin D23
ledcSetup(1, 5000, 8); //canal 1 PWM, 5Khz, 8 bits
// attach the channel to the GPIO to be controlled
ledcAttachPin(19, 1); //Canal 1 PWM a pin D19

void loop() {

digitalWrite(12, !digitalRead(32));
digitalWrite(13, !digitalRead(33));
digitalWrite(14, !digitalRead(34));
digitalWrite(15, !digitalRead(35));

digitalWrite(2, !digitalRead(26)); // turn the LED on (HIGH is the voltage


level)
digitalWrite(4, !digitalRead(27)); // turn the LED on (HIGH is the voltage
level)
//digitalWrite(19, !digitalRead(26)); // turn the LED on (HIGH is the voltage
level)
//digitalWrite(23, !digitalRead(27)); // turn the LED on (HIGH is the voltage
level)

resistencia = REFERENCE_RESISTANCE/((ADC_RESOLUTION/analogRead(SENSOR_PIN))-1);
//realiza la lectura de la resistencia del sensor NTC
//temperatura =
(B_VALUE/(log(resistencia/NOMINAL_RESISTANCE)+(B_VALUE/NOMINAL_TEMPERATURE)))-273;
temperatura =
(1.0/((log(resistencia/NOMINAL_RESISTANCE)/B_VALUE)+(1.0/NOMINAL_TEMPERATURE)))-273
; //Se convierta la resistencia a temperatura

//Serial.println(resistencia);
display.clearDisplay();
display.setCursor(0,0); // Start at top-left corner
display.println(F("JELELECTRONICA"));
display.println("Temperatura:");
display.print(temperatura);
display.display();
//le la información del reloj DS1307 y la manda al puerto serial
RTC.read(tm);
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();

//realiza la lectura de potenciometro para actualizar el ciclo de trabajo de los


canales PWM
dutyCycle = map(analogRead(36), 1, 4096, 255, 0);
// changing the LED brightness with PWM
ledcWrite(0, dutyCycle);
ledcWrite(1, dutyCycle);

//realiza la lectura de potenciometro para actualizar la salida del Dimmer


outVal = map(analogRead(36), 1, 4096, 100, 0); // analogRead(analog_pin),
min_analog, max_analog, 100%, 0%);
dimmer.setPower(outVal); // name.setPower(0%-100%)

delay(50); // wait for a second

void print2digits(int number) {


if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}

También podría gustarte