Deepak Dhanavel Kumar Ravi Kumar
Published © GPL3+

Photon Alarm with Mozilla OS

A Particle board that functions as an alarm clock and is integrated with the Mozilla FireFox OS. You can set the alarms through the mobile.

BeginnerShowcase (no instructions)5 hours1,099
Photon Alarm with Mozilla OS

Things used in this project

Hardware components

Photon
Particle Photon
×1
MAX7219 LED Module 8-Digit 7 Segment Digital LED Display Tube
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1
Breadboard (generic)
Breadboard (generic)
×1
LED (generic)
LED (generic)
×3

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Photon

Code

Photon code

Arduino
This code is to be uploaded to the Photon
#include "LedControl-MAX7219-MAX7221/LedControl-MAX7219-MAX7221.h"
#include "SparkTime/SparkTime.h"

LedControl lc = LedControl(D6,D5,D4,1);

UDP UDPClient;
SparkTime rtc;

unsigned long currentTime;
unsigned long lastTime = 0UL;
// Global Time varibles
/*
    hh - hour
    mm - minute
    tt - alarm time
    TT - current time
    
    h - units place of Hour
    H - tens place of Hour
    m - units place of minute
    M - tens place of minute
*/
int hh,mm,tt,TT;
int h,H,m,M;

int alarm[15];      // Intializing the array for storing the alarm times. alarm[0] will always contain the next alarm
int alarmI = -1;    // Index for alarm

int value=0;        // Switch variable

void setup()
{
    pinMode(D0, OUTPUT);        // Output to the speakers
    digitalWrite(D0, LOW);      
    pinMode(D1, INPUT);         // Alarm Done switch

    // Initializing the LED driver
    lc.shutdown( 0 , false );
    lc.setIntensity( 0 , 8 );
    lc.clearDisplay( 0 );
    
    // Get the current time
    rtc.begin(&UDPClient, "north-america.pool.ntp.org");
    rtc.setTimeZone(5); // gmt offset

    // Functions exposed to the cloud
    Particle.function("newAlarm",newAlarm);
    Particle.function("ringAlarm",ringAlarm);
    Particle.function("doneAlarm",doneAlarm);

    // Set the initial time
    hh = rtc.hour(currentTime);
    mm = rtc.minute(currentTime);
    m = mm % 10 ;
    M = mm / 10 ;
    M += 3;
    if (M >= 6) {
        M -=6;
        hh ++;
        if (hh == 24) {
            hh = 0;
        }
    }
    h = hh % 10 ;
    H = hh / 10 ;
    
    lc.setDigit(0,4,m,true);
    lc.setDigit(0,5,M,true);
    lc.setDigit(0,6,h,true);
    lc.setDigit(0,7,H,true);
}

void loop()
{
    // Read the physical switch first
    value = digitalRead(D1);
    if (value==HIGH) {
        doneAlarm("hi");
    }
    
    currentTime = rtc.now();
    
    if (currentTime != lastTime) {
        // Update the time
        hh = rtc.hour(currentTime);
        mm = rtc.minute(currentTime);
	    m = mm % 10 ;
        M = mm / 10 ;
        M += 3;
        if (M >= 6) {
            M -=6;
            hh ++;
            if (hh == 24) {
                hh = 0;
            }
        }   
        h = hh % 10 ;
        H = hh / 10 ;
        TT = H * 1000 + h * 100 + M * 10 + m;
        lc.setDigit(0,4,m,true);
        lc.setDigit(0,5,M,true);
        lc.setDigit(0,6,h,true);
        lc.setDigit(0,7,H,true);
        if (alarmI != -1) {
            
            // Update the alarm time if there is a pending alarm
            tt = alarm[0];
            int m = tt % 10 ;
            tt = tt / 10 ;
            int M = tt % 10 ;
            tt = tt / 10 ;
            int h = tt % 10 ;
            tt = tt / 10 ;
            int H = tt % 10 ;
            lc.setDigit(0,0,m,true);
            lc.setDigit(0,1,M,true);
            lc.setDigit(0,2,h,true);
            lc.setDigit(0,3,H,true);
        }
        else {
            // Set alarm time to 0 if no alarm is pending
            lc.setDigit(0,0,0,true);
            lc.setDigit(0,1,0,true);
            lc.setDigit(0,2,0,true);
            lc.setDigit(0,3,0,true);
        }
    lastTime = currentTime;
    }
}

// This is called when a new alarm is set on the phone
int newAlarm(String alarmTime) {
    int tt = alarmTime.toInt();
    insertAlarm(TT,tt);
    return 0;
}

// The new alarm must be inserted to the array at the correct place
// The order of alarms in the array must not be disturbed
// Since we are dealing with time, it is complicated
void insertAlarm(int TT,int tt) {
    int flag = 1;
    int t = tt - TT;
    int T;
    if ( alarmI == -1 ) {
        alarm[0] = tt;
        alarmI++;
    } else if (alarmI >= 8 ) {
        alarmI = 999;
    } else {
        int i = 0;
        while ( i <= alarmI && flag == 1 ) {
            T = alarm[i] - TT;
            if ( t < T ) {
                int j = alarmI;
                while( j >= i ) {
                    alarm[j+1] = alarm[j];
                    j--;
                }
                alarm[i] = tt;
                alarmI++;
                flag = 0;
            }
            i++;
        }
        if(flag == 1) {
            alarmI++;
            alarm[alarmI] = tt;
        }
    }
}

int ringAlarm(String t)
{
    digitalWrite(D0, HIGH);
    tt = TT;
    int m = tt % 10 ;
    tt = tt / 10 ;
    int M = tt % 10 ;
    tt = tt / 10 ;
    int h = tt % 10 ;
    tt = tt / 10 ;
    int H = tt % 10 ;

    tt = alarm[0];
    int Am = tt % 10 ;
    tt = tt / 10 ;
    int AM = tt % 10 ;
    tt = tt / 10 ;
    int Ah = tt % 10 ;
    tt = tt / 10 ;
    int AH = tt % 10 ;
    for(int i = 0; i < 10; i++) {
        lc.clearDisplay( 0 );
        delay(300);
        lc.setDigit(0,0,Am,true);
        lc.setDigit(0,1,AM,true);
        lc.setDigit(0,2,Ah,true);
        lc.setDigit(0,3,AH,true);
        lc.setDigit(0,4,m,true);
        lc.setDigit(0,5,M,true);
        lc.setDigit(0,6,h,true);
        lc.setDigit(0,7,H,true);
        delay(300);
    }

    return 0;
}

int doneAlarm(String t)
{
    digitalWrite(D0, LOW);
    for(int i = 0 ; i <= alarmI ; i++ ) {
        alarm[i] = alarm[i+1];
    }
    alarmI--;
}

Credits

Deepak Dhanavel Kumar Ravi Kumar

Deepak Dhanavel Kumar Ravi Kumar

2 projects • 8 followers
Exploring the possibilities of changing the world thourgh IoT.

Comments