Englishscone
Published © GPL3+

Tooth Fairy's Favorite Arduino Project

A Nano powered alarm clock without a real time clock in a floss container...

BeginnerShowcase (no instructions)3 hours489
Tooth Fairy's Favorite Arduino Project

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×2
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Buzzer
Buzzer
×1
SparkFun Solder-able Breadboard - Mini
SparkFun Solder-able Breadboard - Mini
×1
Male Header 80 Position 2 Row (0.1")
Male Header 80 Position 2 Row (0.1")
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Code

No RTC alarm clock (just a Timer really)

Arduino
Quite a redundant code honestly. Could be way better and optimized.
int ledPin = 3;
int buzzerPin = 2;
int fq = 440;
int startbuttonPin = 12;
int startbuttonPowerPin = 10;
int stopbuttonPin = 8;
int stopbuttonPowerPin = 6;

int val = 6;
int compareval;
int stateofbutton;

const unsigned long sixHour = 21600000;
const unsigned long sevenHour = 25200000;
const unsigned long eightHour = 28800000;
const unsigned long nineHour = 32400000;
const unsigned long tenHour = 36000000;
const unsigned long elevenHour = 39600000;
const unsigned long twelveHour = 43200000;

unsigned long startTime, stopTime, savedTime;
String stringSavedSeconds, stringSavedMilliseconds, selectedChar;
int startState, stopState, s, sIndex, ms, msIndex, msShortIndex, outputNum;

void stopWatch(){
startState = digitalRead(startbuttonPin);
stopState = digitalRead(stopbuttonPin);
digitalWrite(buzzerPin, LOW);
if (startState == HIGH){
  startTime = millis();
  delay(200); // debounce, very needed
  digitalWrite(ledPin, HIGH);
}
if (stopState == HIGH){
  stopTime = millis();
  delay(200); // debounce
  digitalWrite(ledPin, LOW);
  delay(1000);
  savedTime = stopTime - startTime;
  createTime();
  stringSavedSeconds = String(s);   //convert int s to string
  sIndex = stringSavedSeconds.length(); //find length, use as index
  for (unsigned int x = 0; x < sIndex; x++){
    selectedChar = stringSavedSeconds.charAt(x);// find certain number
    outputNum = selectedChar.toInt();  // change string number to int
    indicate(outputNum);
    delay(1000);    //delay between numbers          
    }
    delay(500);
    decimalPoint();
    delay(500);
  stringSavedMilliseconds = String(ms);
  msIndex = stringSavedMilliseconds.length();
  msShortIndex = msIndex - 2; // dont need high precision
    for (unsigned int y = 0; y < msShortIndex; y++){
    selectedChar = stringSavedMilliseconds.charAt(y);
    outputNum = selectedChar.toInt();
    indicate(outputNum);
    delay(1000);    //delay between numbers
    }
  }
}
void createTime(){
  s = int(savedTime/1000); // create seconds
  ms = int(savedTime%1000); //create ms from remainder of saved time
}
void decimalPoint(){
   tone(buzzerPin,fq);// short beep to show decimal point
   delay(25); 
   noTone(buzzerPin);
   digitalWrite(buzzerPin, LOW);
}
void snoozeButton(){    
   if (startState == HIGH){  
   delay(120000); // 2 minutes
  }
}
void indicate(int Nblink)
{
  for (int i = 0; i < Nblink; i++)
  {
    digitalWrite(ledPin,HIGH); 
    delay(400);
    digitalWrite(ledPin,LOW);
    delay(400);
  }
}
void setAlarm(int readyDelay){
digitalWrite(ledPin,HIGH);
delay(100);
digitalWrite(ledPin,LOW);
delay(100);
digitalWrite(ledPin,HIGH);// double quick blink, indicating setAlarm has started 
delay(100);

digitalWrite(ledPin,LOW);// making sure I'm not using extra power from battery
digitalWrite(buzzerPin,LOW);
if (readyDelay == 6){
delay(sixHour);
}
if (readyDelay == 7){ // probably needs a switch statment
delay(sevenHour);
}
if (readyDelay == 8){
delay(eightHour);
}
if (readyDelay == 9){
delay(nineHour);
}
if (readyDelay == 10){
delay(tenHour);
}
if (readyDelay == 11){
delay(elevenHour);
}
if (readyDelay == 12){
delay(twelveHour); 
}
   for (int w = 0; w <= 50; w++)
   {
   tone(buzzerPin,fq);
   delay(400);
   noTone(buzzerPin);
   delay(400);
   snoozeButton();
   }
}

void setup() {
pinMode(ledPin,OUTPUT);
pinMode(buzzerPin,OUTPUT);
pinMode(startbuttonPin, INPUT);
pinMode(stopbuttonPin, INPUT);

pinMode(startbuttonPowerPin, OUTPUT);
pinMode(stopbuttonPowerPin, OUTPUT);
digitalWrite(startbuttonPowerPin, HIGH);
digitalWrite(stopbuttonPowerPin, HIGH);

digitalWrite(ledPin,HIGH);
delay(100);
digitalWrite(ledPin,LOW);
delay(2000);
}

void loop() {
delay(1000);
startState = digitalRead(startbuttonPin);
stopState = digitalRead(stopbuttonPin);
if (stopState == HIGH){
  digitalWrite(ledPin, HIGH);
  delay(50);
  digitalWrite(ledPin, LOW);
  delay(3000);
  while(true){
    stopWatch();
    }
  }
  else{
  compareval = val;
  indicate(val);
  delay(2000);
  startState = digitalRead(startbuttonPin);
  if (startState == HIGH){   
    compareval = val;
    val = val + 1;
  }
  else{
  setAlarm(val); 
}
indicate(val);
delay(2000);
  startState = digitalRead(startbuttonPin);
  if (startState == HIGH){   
    compareval = val;
    val = val + 1;
  }
  else{
  setAlarm(val);  
  }
  if( val > 12){
    val = 6;
    }
  }
}

Credits

Englishscone
4 projects • 2 followers
Skills in Arduino, hardware dev, python. Help on anything I can help with, just ask.
Contact

Comments

Please log in or sign up to comment.