Hello! Today I finished my first Arduino project (without tutorials and examples) and I would like to get some feedback on it The project was to make an easy Alarm clock which shows time, temperature and when the alarm will go off. I bought an Arduino starter kit (from Swedish Kjell.Co ) and all parts I used in the project came from that kit.
In my opinion I think I have succeeded well but I would like som opinions from you others out there! So feel free to share your thoughts/suggestions but try to explain carefully (if it is obvious for you, it might not be for me). So here follows the code and wiring! Thank you
// Time will NOT correspond correct time. It must be set first
//Importing needed libraries
#include <Button.h>
#include <RtcDS3231.h>
#include "SSD1306AsciiWire.h"
#include <Wire.h>
#include <Arduino.h>
//Creating objects
Button btnAlarm(3);
Button btnToggle(4);
Button btnH(5);
Button btnM(6);
RtcDS3231 rtc;
SSD1306AsciiWire oled;
//Declaring global variables
byte piezoPin = 8;
byte timeHour;
byte timeMinute;
byte timeSecond;
byte tempSecond;
byte wakeHour;
byte wakeMinute;
byte temp;
boolean state;
boolean alarm;
char timeString[9];
char wakeString[6];
void setup() {
 //Starting objects
 btnAlarm.begin();
 btnH.begin();
 btnM.begin();
 btnToggle.begin();
 Wire.begin();
 oled.begin(&Adafruit128x64, 0x3C);
 //Set variables and pinMode
 pinMode(8 , OUTPUT);
 state = true;
 alarm = true;
}
void loop() {
 drawTime();
 //Check if Alarm should go of
 if ( alarm == true && timeHour == wakeHour && timeMinute == wakeMinute) {
  ringAlarm();
 }
 //Telling Arduino what happens if button is pressed
 if (btnToggle.pressed()) {
  alarm = true;
  if (state == true) {
   state = false;
  } else {
   state = true;
  }
  oled.clear();
 }
 //Telling Arduino what happens if button is pressed
 if (btnH.pressed()) {
  if (alarm == true) {
   if (state == true) {
    if (wakeHour < 24) {
     wakeHour = wakeHour + 1;
    }
   } else {
    if (wakeHour > 0) {
     wakeHour = wakeHour - 1;
    }
   }
   oled.clear();
  }
 }
 //Telling Arduino what happens if button is pressed
 if (btnM.pressed()) {
  if (alarm == true) {
   if (state == true) {
    if (wakeMinute < 50) {
     wakeMinute = wakeMinute + 10;
    }
   } else {
    if (wakeMinute > 0) {
     wakeMinute = wakeMinute - 10;
    }
   }
   oled.clear();
  }
 }
 //Telling Arduino what happens if button is pressed
 if (btnAlarm.pressed()) {
  if (alarm == true) {
   alarm = false;
  } else {
   alarm = true;
  }
  oled.clear();
  drawTime();
 }
}
/*
 Method that get values for time and temperature and puts them in corresponding variables
*/
void updateTimeTemp() {
 RtcTemperature rtcTemp = rtc.GetTemperature();
 temp = rtcTemp.AsWholeDegrees();
 RtcDateTime now = rtc.GetDateTime();
 timeHour = now.Hour();
 timeMinute = now.Minute();
 timeSecond = now.Second();
}
/*
 Draws time, temperature and what wakeup time is (if alarm == true)
*/
void drawTime() {
 updateTimeTemp();
 oled.setFont(lcdnums14x24);
 oled.setRow(1);
 oled.setCol(8);
 sprintf(timeString, "%02d:%02d:%02d", timeHour, timeMinute, timeSecond);
 oled.println(timeString);
 oled.setFont(lcdnums12x16);
 oled.setRow(6);
 oled.setCol(102);
 oled.print(int(temp));
 if (alarm == true) {
  oled.setFont(lcdnums12x16);
  oled.setRow(6);
  oled.setCol(0);
  sprintf(wakeString, "%02d:%02d", wakeHour, wakeMinute);
  oled.print(wakeString);
  if (state == false) {
   oled.print("-");
  }
 }
}
/*
 Method that rings the alarm and checks if it is deactivated
*/
void ringAlarm() {
 for (byte i = 0; i < 15; i++) {
  if (btnAlarm.pressed()) {
   alarm = false;
   oled.clear();
   digitalWrite(piezoPin, LOW);
   break;
  }
  drawTime();
  digitalWrite(piezoPin, HIGH);
  delay (200);
  if (btnAlarm.pressed()) {
   alarm = false;
   oled.clear();
   digitalWrite(piezoPin, LOW);
   break;
  }
  drawTime();
  digitalWrite(piezoPin, LOW);
  delay (200);
  if (btnAlarm.pressed()) {
   alarm = false;
   oled.clear();
   digitalWrite(piezoPin, LOW);
   break;
  }
  drawTime();
  digitalWrite(piezoPin, HIGH);
  delay(200);
  if (btnAlarm.pressed()) {
   alarm = false;
   oled.clear();
   digitalWrite(piezoPin, LOW);
   break;
  }
  drawTime();
  digitalWrite(piezoPin, LOW);
  delay(300);
  drawTime();
  delay(300);
  if (btnAlarm.pressed()) {
   alarm = false;
   oled.clear();
   digitalWrite(piezoPin, LOW);
   break;
  }
 }
}
ChrisTenone:
That's nice. Are you going to put it in a housing? If so, you might want to consider a smaller board, like the nano.
Yeah I am thinking about it! Since I really need a good alarm clock. I did my first real test today and it worked great, it rang 1030 and I woke up! This is amazing since I went to bed 0300 and I have serious issues waking up! I might make some sort of wooden housing for it but then I will probaly buy a nano or somethiing and more components so I can keep experimenting with other stuff and my UNO!
Benarius2:
Why is there a Fritzing logo on the screenshot?
I used Fritzing to make the picture and I am pretty new to Fritzing. I exported the layout as a picture and in the process it showed up there. Shouldn't there be their logo?
More comments in your code. If someone else wants to change it, or you want to change it in a years time, they will be invaluable. When writing comments, I find that rather than saying what each section of code is doing, it's more important to say why it is doing it.
Save the alarm time in EEPROM so that it does not get forgotten when the power is removed.
Have different alarm time for weekdays and weekends.
Nice to hear another wood worker has joined the crew.
I made a few of these (using Lacewood) for clocks and serial monitors.
Maybe this design could be modified to your needs.
More comments in your code. If someone else wants to change it, or you want to change it in a years time, they will be invaluable. When writing comments, I find that rather than saying what each section of code is doing, it's more important to say why it is doing it.
Save the alarm time in EEPROM so that it does not get forgotten when the power is removed.
Have different alarm time for weekdays and weekends.
Yeah, I made the comments pretty quick before uploading it here, I will sometime improve it That was actually one of my "problems", really upsetting having to set alarm time after every reboot. But how do I do to save the alarm time in EEPROM? What is even EEPROM? Good idea for having different alarm times for different days! But since the project aims on easy to use and not to complicated I do not think I will implement this. I am okay with changing the alarm time once in a while
Thank you for great responses and suggestion!
LarryD:
Nice to hear another wood worker has joined the crew.
I made a few of these (using Lacewood) for clocks and serial monitors.
Maybe this design could be modified to your needs.
Nice design! What program did you use to make it? Thanks for sharing! Unfortunately I do not have the essential woodworking tools to do things like that and make them correctly, so I think I will do something simpler!
Nice design! What program did you use to make it? Thanks for sharing! Unfortunately I do not have the essential woodworking tools to do things like that and make them correctly, so I think I will do something simpler!
EEPROM is a type of memory that Arduino has, where the contents is not lost when power is switched off. So each time you update the alarm time, you can also save it in the EEPROM. Then you can read it back in startup() when the power is restored. Link
Ah ok cool! I have sketchup but I am mostly familiar with AutoCad 2016. Since I am a student I get it for free (hint hint).
PaulRB:
EEPROM is a type of memory that Arduino has, where the contents is not lost when power is switched off. So each time you update the alarm time, you can also save it in the EEPROM. Then you can read it back in startup() when the power is restored. Link
Thanks mate! I will look into that! Very useful tip since I often power my clock off when I do not need or use it. Saves me a lot of clicking
Off-topic question: I have an ESP8266 module, is it possible to wirelessly control an UNO from lets say a phone or computer? Like have a small app you can enter RGB-values and then send them to the ESP which tells the UNO to change an RGB-LED?
Hilleskog:
Off-topic question: I have an ESP8266 module, is it possible to wirelessly control an UNO from lets say a phone or computer? Like have a small app you can enter RGB-values and then send them to the ESP which tells the UNO to change an RGB-LED?
Yes. But if all you are doing is controlling an rgb led, or even a strip of rgb leds, you don't need the Uno. You can upload a sketch to the esp from the Arduino IDE. The esp-01 module only has two I/o pins, but if you use ws2812 leds, you only need one pin to control them.
For the phone app, you could use Blynk. Or you could use a web page as the interface.
PaulRB:
Yes. But if all you are doing is controlling an rgb led, or even a strip of rgb leds, you don't need the Uno. You can upload a sketch to the esp from the Arduino IDE. The esp-01 module only has two I/o pins, but if you use ws2812 leds, you only need one pin to control them.
For the phone app, you could use Blynk. Or you could use a web page as the interface.
Okay thanks! Well I used the RGB as an example to better explain what I was aiming for. But from what I have understood from your response it is possible? Could you please link on how to set up a webpage as interface? If we complicate this further, lets say I want to change my alarm time without having to press the buttons physically, how do I do?
Yes, it is possible. But remember the esp-01 has only two digital pins. Other types of esp module have more pins.
Why do you want to set the alarm time through a web browser? When you asked about rgb leds, I though this was for a separate project. If you use your phone to connect to the alarm clock to set the alarm time, why not just use the alarm app on your phone?
Hilleskog:
Okay thanks! Well I used the RGB as an example to better explain what I was aiming for. But from what I have understood from your response it is possible? Could you please link on how to set up a webpage as interface? If we complicate this further, lets say I want to change my alarm time without having to press the buttons physically, how do I do?
PaulRB:
Why do you want to set the alarm time through a web browser? When you asked about rgb leds, I though this was for a separate project. If you use your phone to connect to the alarm clock to set the alarm time, why not just use the alarm app on your phone?
@Hilleskog
You have a method for setting the alarm using buttons. This I understand.
You are not satisfied with your method for setting the alarm. This I also understand.
You intend to use some kind of Web interface as a solution. This I do not understand. It is as though you wanted to go from your home to your local pub, and decided that the best way to get there would be through Tampere. Tampere is a nice place, but really, it's neither the quickest nor the easiest way to get where you want to go.
One of my recent projects was a clock with two buttons. I found two buttons enough for me to be able to set the date and time quickly. On this clock's display were some digits (of course) and a cursor. The first button moved the cursor. The second button altered the digit above the cursor. If you are old enough to remember a time before smartphones, then you will recognize this as the procedure for setting a digital wristwatch.
PaulRB:
Yes, it is possible. But remember the esp-01 has only two digital pins. Other types of esp module have more pins.
Why do you want to set the alarm time through a web browser? When you asked about rgb leds, I though this was for a separate project. If you use your phone to connect to the alarm clock to set the alarm time, why not just use the alarm app on your phone?
odometer: @Hilleskog
You have a method for setting the alarm using buttons. This I understand.
You are not satisfied with your method for setting the alarm. This I also understand.
You intend to use some kind of Web interface as a solution. This I do not understand. It is as though you wanted to go from your home to your local pub, and decided that the best way to get there would be through Tampere. Tampere is a nice place, but really, it's neither the quickest nor the easiest way to get where you want to go.
One of my recent projects was a clock with two buttons. I found two buttons enough for me to be able to set the date and time quickly. On this clock's display were some digits (of course) and a cursor. The first button moved the cursor. The second button altered the digit above the cursor. If you are old enough to remember a time before smartphones, then you will recognize this as the procedure for setting a digital wristwatch.
I understand why you ask those questions, but the thing is that I am not aiming for simplicity. The goal here is for me to learn and try out different functions and possibilities. I might use my phones alarm clock but whats the fun in that? And also my Arduino clock is the clock that actually wakes me up.
So I really just want to learn and since I have my alarm clock I might just try to overcomplicate it to get more knowledge which I can use in other projects. So how would I proceed with doing what I described in my last post?
For some this might seem stupid but this is the way I learn, I make something, overcomplicate it with not so necessary functions, and then learn from using them. It is easier for me to do this on a already working project than it is to make a whole new project.
If you are going WiFi, you won't need the RTC any more. You can sync with an NTP server once a day.
Having tried them, I think those esp01 modules are more trouble than they are worth. The problem is they are just not very compatible with Uno. They are 3.3V and Uno is 5V. So you need logic shifters and a 3.3V regulator to provide it with enough current.
I would recommend getting an esp based development board such as a nodemcu board. My personal favourite is the Wemos D1 mini. This will mean you can put the Uno back in it's box for another project.