Hi everyone,
I put together two codes and when i upload it to my arduino it only shows the temperature. Can anyone help me so i can resolve this problem?
#include <SevSeg.h>
#include <Wire.h>
#include <DHT.h>
#define DHTPIN 11
#define DHTTYPE DHT11
#define DS1307_ADDRESS 0x68
SevSeg display7seg;
DHT dht(DHTPIN, DHTTYPE);
//Clock
int valor = 0;
int minutos_;
int horas_;
int buttonState = 0;
//Buttons for increase minuts and hours
const int buttonPin2 = A1;
const int buttonPin = A0;
byte zero = 0x00;
unsigned long timer;
//for temperature and humidity
byte digit0 = 9;
byte digit1 = 10;
byte digit2 = 12;
byte digit3 = 13;
byte sevenSegmentPins[] = {2, 3, 4, 5, 6, 7, 8};
byte sevenSegment[12][7] =
{
{ 1, 1, 1, 1, 1, 1, 0 }, // = 0
{ 0, 1, 1, 0, 0, 0, 0 }, // = 1
{ 1, 1, 0, 1, 1, 0, 1 }, // = 2
{ 1, 1, 1, 1, 0, 0, 1 }, // = 3
{ 0, 1, 1, 0, 0, 1, 1 }, // = 4
{ 1, 0, 1, 1, 0, 1, 1 }, // = 5
{ 1, 0, 1, 1, 1, 1, 1 }, // = 6
{ 1, 1, 1, 0, 0, 0, 0 }, // = 7
{ 1, 1, 1, 1, 1, 1, 1 }, // = 8
{ 1, 1, 1, 0, 0, 1, 1 }, // = 9
{ 0, 1, 1, 0, 1, 1, 1 }, // = H
{ 0, 0, 0, 1, 1, 1, 1 } // = t
};
void setup()
{ //clock
dht.begin();
Wire.begin();
int displayType = COMMON_CATHODE;
int digit4 = 14; //Pino Digito1 do display
int digit5 = 15; //Pino Digito2 do display
int digit6 = 16; //Pino Digito3 do display
int digit7 = 17; //Pino Digito4 do display
int segA = 22; //Pino segmento A
int segB = 23; //Pino segmento B
int segC = 24; //Pino segmento C
int segD = 25; //Pino segmento D
int segE = 26; //Pino segmento E
int segF = 27; //Pino segmento F
int segG = 28; //Pino segmento G
int segDP = 29; //Pino segmento H
int numberOfDigits = 4;
display7seg.Begin(displayType, numberOfDigits, digit4, digit5, digit6, digit7, segA, segB, segC, segD, segE, segF, segG, segDP);
display7seg.SetBrightness(100);
timer = millis();
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
//temperature
pinMode(digit0, OUTPUT); //pin 9
pinMode(digit1, OUTPUT); //pin 10
pinMode(digit2, OUTPUT); //pin 12
pinMode(digit3, OUTPUT); //pin 13
for (int i = 0; i < 7; i++)
{
pinMode(sevenSegmentPins[i], OUTPUT);
}
//digitalWrite(dotPin, HIGH);
digitalWrite(digit0, HIGH);
digitalWrite(digit1, HIGH);
digitalWrite(digit2, HIGH);
digitalWrite(digit3, HIGH);
}
//funcao segmentwrite
void segmentWrite(byte digit)
{
byte pin = 2;
for (byte i = 0; i < 7; ++i)
{
digitalWrite(pin, sevenSegment[digit][i]);
++pin;
}
}
void twoSegmentWrite(int digit)
{
int digit_1 = digit / 10;
int digit_2 = digit % 10;
digitalWrite(digit0, LOW);
segmentWrite(digit_1);
delay(5);
digitalWrite(digit0, HIGH);
digitalWrite(digit1, LOW);
segmentWrite(digit_2);
delay(5);
digitalWrite(digit1, HIGH);
}
void twoSegmentWrite1(int digit)
{
int digit_1 = digit / 10;
int digit_2 = digit % 10;
digitalWrite(digit2, LOW);
segmentWrite(digit_1);
delay(5);
digitalWrite(digit2, HIGH);
digitalWrite(digit3, LOW);
segmentWrite(digit_2);
delay(5);
digitalWrite(digit3, HIGH);
}
void twoSegmentWriteWord (int digit)
{
digitalWrite(digit0, LOW);
digitalWrite(digit1, HIGH);
segmentWrite(digit);
delay(5);
digitalWrite(digit1, HIGH);
}
void loop()
{ //clock
char tempString[10]; //Used for sprintf
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int segundos = ConverteparaDecimal(Wire.read());
int minutos = ConverteparaDecimal(Wire.read());
int horas = ConverteparaDecimal(Wire.read() & 0b111111);
sprintf(tempString, "%02d%02d", horas, minutos);
display7seg.DisplayString(tempString, 3);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
delay(150);
if (buttonState == HIGH)
{
minutos_ = incrementa(minutos);
SelecionaDataeHora();
}
}
buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH)
{
delay(150);
if (buttonState == HIGH)
{
horas_ = incrementa2(horas);
SelecionaDataeHora();
}
}
//get the values of temperature and humidity
float h = dht.readHumidity();
float ta = dht.readTemperature();
twoSegmentWrite(h);
for (int t=0; t<=99; t++)
{
twoSegmentWrite(h);
twoSegmentWrite1(ta);
}
} //this lines is just for get hours correctly
void SelecionaDataeHora()
{
byte segundos = 5;
byte minutos = minutos_;
byte horas = horas_;
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.write(ConverteParaBCD(segundos));
Wire.write(ConverteParaBCD(minutos));
Wire.write(ConverteParaBCD(horas));
Wire.write(zero);
Wire.endTransmission();
}
byte ConverteParaBCD(byte val)
{
//Converte o número de decimal para BCD
return ( (val / 10 * 16) + (val % 10) );
}
byte ConverteparaDecimal(byte val)
{
//Converte de BCD para decimal
return ( (val / 16 * 10) + (val % 16) );
}
int incrementa (int minutos_)
{
minutos_++;
if (minutos_ >= 60)
{
minutos_ = 0;
horas_++;
}
return minutos_;
}
int incrementa2 (int horas_)
{
horas_++;
return horas_;
}