Hello all!
I have made a DIY clock, and everything works!... almost.
I am using an Atmega16 for the microcontroller, and have a RGB LED, White LED, LDR, Buzzer, 4x tactile buttons, 4way switch, dht11, potentiometer, 4-digit display, ds1307 IC... all connected.
I have tested everything and I have found that Button3 and Button4 (PB5 & PB7) aren't reading anything! Buttons 1 and 2 work just fine (PB4 & PB6) though .
I've tested almost everything else, and it all seems to be working.
Here is my code that I have so far (definitely not finished):
#include <Wire.h>
#include "uRTCLib.h"
#include <SevSeg.h>
#include <dht.h>
#include "DIY_Clock_Pins.h"
uRTCLib rtc(0x68);
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
SevSeg sevseg;
dht DHT;
unsigned long lastmillis=0;
unsigned long lmillis=0;
float t=0;
float h=0;
int times=0;
bool flash=false;
bool AM = false;
int alarmamount = 2;
bool buzzeralarm = false;
struct alarmstruct {
int hour=0;
int minute=0;
int second=0;
int day=0;
bool pm=false;
bool armed=false;
};
alarmstruct alarm[2];
void setup() {
byte numDigits = 4;
byte digitPins[] = {S7SEG_PIN_DIG1, S7SEG_PIN_DIG2, S7SEG_PIN_DIG3, S7SEG_PIN_DIG4};
byte segmentPins[] = {S7SEG_PIN_A, S7SEG_PIN_B, S7SEG_PIN_C, S7SEG_PIN_D, S7SEG_PIN_E, S7SEG_PIN_F, S7SEG_PIN_G, S7SEG_PIN_DECIMAL};
bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(100);
URTCLIB_WIRE.begin();
Serial.begin(9600);
Serial.println("DIY CLOCK!!!!");
SetupPinModes();
//rtc.set(0, 6, 10, 4, 29, 12, 22);
// rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
// set day of week (1=Sunday, 7=Saturday)
setalarm(1,5,40,0,5,true,true);
Serial.println(alarm[1].hour);
Serial.println(alarm[1].minute);
Serial.println(alarm[1].day);
Serial.println(alarm[1].pm);
Serial.println(alarm[1].armed);
Serial.println("-----------------------------");
}
void loop() {
rtc.refresh();
/*int val = map(analogRead(LDR),0,1023,255,0);
analogWrite(LED4, val);*/
if (millis() - lastmillis > 1000){
/*int readData = DHT.read11(DHT_PIN_DATA);
t = DHT.temperature; // Read temperature
h = DHT.humidity; // Read humidity
t = (t*1.80)+32;
Serial.println("Temperature: " + String(t));*/
times = getTime();
Serial.println("Time: " + String(times));
lastmillis = millis();
}
if (millis() - lmillis >= 1000){
flash=!flash;
lmillis = millis();
}
if (buzzeralarm){
digitalWrite(Buzzer, HIGH);
delay(1500);
digitalWrite(Buzzer, LOW);
buzzeralarm = false;
}
checkAlarm(1);
checkbuttons();
if (flash){
sevseg.setNumber(times,2);
}
else {
sevseg.setNumber(times);
}
sevseg.refreshDisplay();
}
void checkbuttons(){
if (digitalRead(Button1) == LOW){
Serial.println("Button1");
}
if (digitalRead(Button2) == LOW){
Serial.println("Button2");
}
if (digitalRead(Button3) == LOW){
Serial.println("Button3");
}
if (digitalRead(Button4) == LOW){
Serial.println("Button4");
}
}
void setalarm(int alarmnumber,int hour,int minute,int second,int day,bool pm,bool armed){
alarm[alarmnumber].hour = hour;
alarm[alarmnumber].minute = minute;
alarm[alarmnumber].second = second;
alarm[alarmnumber].day = day;
alarm[alarmnumber].pm = pm;
alarm[alarmnumber].armed = armed;
}
int getTime(){
if (rtc.hour() > 12){
AM=false;
return(((rtc.hour()-12)*100)+rtc.minute());
}
else {
AM=true;
return((rtc.hour()*100)+rtc.minute());
}
}
int getHour(){
if (rtc.hour() > 12){
return(rtc.hour()-12);
}
else {
return(rtc.hour());
}
}
void checkAlarm(int alarmnumber){
for (int i=1;i<alarmamount;i++){
if (alarm[i].armed == 1){
if (alarm[i].hour == getHour()){
if (alarm[i].minute == rtc.minute()){
if (alarm[i].second == rtc.second()){
Serial.println("true");
if (alarm[i].pm == !AM){//== 1 && AM == 0) || (alarm[i].pm == 0 && AM == 1)){
buzzeralarm = true;
}
}
}
}
}
}
}
void chirp(int t){
digitalWrite(Buzzer, HIGH);
delay(t);
digitalWrite(Buzzer, LOW);
}
void rgb(int r, int g, int b){
analogWrite(RGBRed, r);
analogWrite(RGBGreen, g);
analogWrite(RGBBlue, b);
}
void SetupPinModes(){
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
pinMode(SW3, INPUT);
pinMode(SW4, INPUT);
pinMode(LDR, INPUT);
pinMode(POT1, INPUT);
pinMode(LED4, OUTPUT);
pinMode(RGBRed, OUTPUT);
pinMode(RGBGreen, OUTPUT);
pinMode(RGBBlue, OUTPUT);
pinMode(Buzzer, OUTPUT);
}
DIY_Clock_Pins.h :
#define DHT_PIN_DATA PIN_PD2
#define S7SEG_PIN_DIG1 PIN_PA0
#define S7SEG_PIN_DIG2 PIN_PA1
#define S7SEG_PIN_DIG3 PIN_PA2
#define S7SEG_PIN_DIG4 PIN_PA3
#define S7SEG_PIN_A PIN_PD7
#define S7SEG_PIN_B PIN_PC2
#define S7SEG_PIN_C PIN_PC3
#define S7SEG_PIN_D PIN_PC4
#define S7SEG_PIN_E PIN_PC5
#define S7SEG_PIN_F PIN_PC6
#define S7SEG_PIN_G PIN_PC7
#define S7SEG_PIN_DECIMAL PIN_PD6
#define Button1 PIN_PB6
#define Button2 PIN_PB4
#define Button3 PIN_PB5
#define Button4 PIN_PB7
#define SW1 PIN_PB0
#define SW2 PIN_PB1
#define SW3 PIN_PB2
#define SW4 PIN_PB3
#define LDR PIN_PA4 //photoresistor
#define POT1 PIN_PA6
#define LED4 PIN_PA5
#define RGBRed PIN_PD3
#define RGBGreen PIN_PD4
#define RGBBlue PIN_PD5
#define Buzzer PIN_PA7
Also, I have a schematic made already, as I had a PCB made for this project:
Any idea why it's not working?
Thanks in advance!
Edit:
Also, I have the 4 buttons setup as INPUT_PULLUPs. They short the Atmega16 pin to GND.