SAMMANFATTNING

Den här rapporten beskriver hur ljus, musik och tid kan användas för att motverka stress och hjälpa dig att vakna upp bättre, eftersom många människor har problem med det. Kärnidéen kommer ifrån forskning om melatoninkontroll där man använder ljus, stressreduktion där man använder musik och sömnförbättring där man använder två alarm (genom att använda tid). Baserat på gjord forskning byggdes en väckarklocka. Den är gjord så att användaren ska kunna ställa in väckarklockan så att det passar honom/henne. Under utställningen gavs bra feedback. Många gillade konceptet och tyckte att man även kunde lägga till kopplingar till andra maskiner i hemmet och att man skulle kunna kontrollera REM sömnen. Några tyckte systemet var för kontrollerande och att det skulle passa bättre inom hälsovård. Framtida utveckling skulle kunna innehålla en uppkoppling mot TV, Iphone eller dator samt fungera för flera användare.

Nyckelord: Väckarklocka, melatonin, stress, sömnförbättring, musik och sömn.

ABSTRACT

This report describes how light, music and time can be used to help you sleep and to wake up better since many people suffer from stress and sleeping disorders. The core idea of the project rose from research around melatonin control using light, stress reduction using music and the use of two alarms to train the body (using time). An alarm clock was built based on the suggested research. It is made in a way that it’s always controllable in any mode of preferred usage. During the exhibition most people liked the concept and other features could be added in the future and to control REM sleep. Some people thought it was too controlling and said it could fit better in health care. Future work could connect the device to the TV, Iphone or computer and to consider multiple users.

Keywords: Alarm clock, melatonin, stress, sleep deprivation, music and sleep.

Our three devices on a table for creative working late at night

fredag 18 december 2009

Pictures from the exhibition







Source code for alarm device

#include "FatReader.h"
#include "SdReader.h"
#include "avr/pgmspace.h"
#include "WaveUtil.h"
#include "WaveHC.h"
#include "Fat16.h"
#include "Fat16util.h"
#include "WProgram.h"
#include "Wire.h"
#include "DS1307.h"
#include "WString.h"
#include "EEPROM.h"
#include "NewSoftSerial.h"


SdReader card;
FatVolume vol;
FatReader root;
FatReader f;
uint8_t dirLevel; // indent level for file/dir names
dir_t dirBuf; // buffer for directory reads
WaveHC wave; // only one!


SdCard card1;
Fat16 file;
NewSoftSerial mySerial(7,8);
byte wakeLght=0;
byte wakeSng=0;
byte remm=0;
byte slpLght=0;
byte slpSng=0;
byte sched=0;
byte sd;
byte set=14;
byte up =9;
byte Stop=6;
byte alarm[3];
//String waveString;
byte stateup;
byte stateStop;
byte stateset;
byte stateP;
byte stateon;
byte almSng=0;
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
{
PgmPrint("error: ");
SerialPrintln_P(str);
while(1);
}

void setup()
{
Serial.begin(9600);
Serial.begin(9600);
Serial.print(17, BYTE);
Serial.print(12, BYTE);
mySerial.begin(19200);

pinMode(set,INPUT);
pinMode(up,INPUT);
pinMode(Stop,INPUT);
pinMode(15,INPUT);
pinMode(17,INPUT);
//pinMode(0,INPUT);

// initialize the SD card
if (!card1.init()) error("card.init");

// initialize a FAT16 volume
if (!Fat16::init(card1)) error("Fat16::init");

// open a file
if (file.open("PRINT00.TXT", O_READ))
{
//PgmPrintln("Opened PRINT00.TXT");
}

else
{
error("file.open");
}

int16_t n;
uint8_t buf[28];// nothing special about 7, just a lucky number.
byte temp[28];
//byte cnt=0;
byte cnt2=1;
byte val;
byte val2;
while ((n = file.read(buf, sizeof(buf))) > 0) //read from the text file 24 bytes at a time.
{
for (uint8_t i = 0; i < n; i++)
{
//Serial.print(buf[i]);
temp[i]=buf[i];// get all 24 values into the temp array
delay(10);
}
for(int i=0;i<28;i=i+2)
{
val=temp[i]-48;
val2=temp[i+1]-48;// convert consecative locations into integer values for the time array

val=(val*10)+val2;// add the two values converted to get one integer value
// cnt2++;
EEPROM.write(cnt2,val);
cnt2++;
}
//cnt++;
//cnt2=0;


}
file.close();
//write zero to location zero so that time will be set only once.
if (file.open("PRINT00.TXT", O_WRITE))
{
// PgmPrintln("Opened PRINT00.TXT");
}
else
{
error("file.open");
}
if (!file.isOpen()) error ("file.open");
PgmPrint("Writing to: ");
//Serial.print("PRINT00.TXT");
file.write("0");
// close file and force write of all data to the SD card
file.close();
PgmPrintln("Done");



//function to set time if time was set in the text file
if(EEPROM.read(1)==10)
{
RTC.stop();
RTC.set(DS1307_SEC,0); //set the seconds
RTC.set(DS1307_MIN,EEPROM.read(3)); //set the minutes
RTC.set(DS1307_HR,EEPROM.read(2)); //set the hours
RTC.set(DS1307_DOW,EEPROM.read(4)); //set the day of the week
RTC.set(DS1307_DATE,EEPROM.read(5)); //set the date
RTC.set(DS1307_MTH,EEPROM.read(6)); //set the month
RTC.set(DS1307_YR,EEPROM.read(7)); //set the year
RTC.start();
EEPROM.write(1,0);
}

// set up waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

// if (!card.init(true)) {//play with 4 MHz spi
if (!card.init()) {//play with 8 MHz spi
putstring_nl("Card init. failed!");
while(1);
}
// enable optimize read - some cards may timeout
card.partialBlockRead(true);

uint8_t part;
for (part = 0; part < 5; part++) {
if (vol.init(card, part)) break;
}
if (part == 5) {
putstring_nl("No valid FAT partition!");
while(1);
}

if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); while(1);
}

dirLevel = 0;
}







void loop()
{
Serial.print(17, BYTE);
Serial.print(22, BYTE);
Serial.print(12, BYTE);

displayTime();
Serial.print(13, BYTE);
Serial.print(" Sleep and Reminder");
Serial.print(13, BYTE);
Serial.print(" Alarm");
delay(1000);


stateup =digitalRead(up);
delay(50);
if(stateup==LOW)
{
sendToLamp(5);
}
stateset =digitalRead(set);
delay(50);
if(stateset==LOW)
{
sendToLamp(6);
}


stateP =digitalRead(16);
delay(50);
if(stateP==LOW)
{
presentation();
}

stateon =digitalRead(15);
delay(50);
if(stateon==LOW)
{
Serial.print(12, BYTE);
Serial.print(" Time or Alarm");
stateon=1;
while(stateon==1)
{
stateup =digitalRead(up);
delay(100);
if(stateup==LOW)
{
setTime();
}
stateset =digitalRead(set);
delay(100);
if(stateset==LOW)
{
setAlarm();
}
}
}





if(wakeSng==1)
{
playWakeUp();
}
if(slpSng==1)
{
playSleep();
}
if(remm==1)
{
remmember();
}
if(sd==1)
{
playSchduled();
}

delay(50);
stateup =digitalRead(up);
delay(50);
if(stateup==LOW)
{
sendToLamp(5);
}
stateset =digitalRead(set);
delay(50);
if(stateset==LOW)
{
sendToLamp(6);
}

checkAlarm();
checkSleepTime();
checkWakeUpTime();

//sendToLamp(1);


}

void playcomplete(char *name,byte what)
{
playfile(name);
while (wave.isplaying)
{
if(digitalRead(Stop)==LOW)
{

if(what==1)
{
slpSng=4;
}
else if(what==2)
{
wakeSng=4;
}
else if(what==3)
{
remm=4;
}
else if(what==4)
{
//sched=0;
sd=0;
}
wave.stop();
}
else
{
if(what==1)
{
Serial.print(12, BYTE);
displayTime();
Serial.print(13, BYTE);
Serial.print(" Sleep Time ");
delay(500);
}
else if(what==2)
{
Serial.print(12, BYTE);
displayTime();
Serial.print(13, BYTE);
Serial.print(" Wake Time ");
delay(500);
}
else if(what==3)
{
Serial.print(12, BYTE);
displayTime();
Serial.print(13, BYTE);
Serial.print(" Remmember ");
delay(500);
}
else if(what==4)
{
Serial.print(12, BYTE);
displayTime();
Serial.print(13, BYTE);
Serial.print(" Scheduled Song ");
delay(500);
}
else if(what==5)
{
Serial.print(12, BYTE);
displayTime();
Serial.print(13, BYTE);
Serial.print(" Normal Alarm");
delay(500);
}

}
}
}

void playfile(char *name) {
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
if (!f.open(root, name)) {
putstring("failed");
Serial.print(name);
return;
}
if (!wave.create(f)) {
putstring_nl("Not a WAV"); return;
}
// ok time to play!
wave.play();
}

void play(FatReader &dir)
{
FatReader file;
while (dir.readDir(dirBuf) > 0) {

// skip . and .. directories
if (dirBuf.name[0] == '.') continue;
Serial.println();
for (uint8_t i = 0; i < dirLevel; i++) Serial.print(' ');
if (!file.open(vol, dirBuf)) {
Serial.print("open fail");
while(1);
}

if (file.isDir()) {
putstring("Subdir: ");
dirLevel += 2;
// play files in subdirectory
play(file);
dirLevel -= 2;
}
else {
putstring("Playing ");
if (!wave.create(file)) {
putstring(" Not a WAV");
}
else {
Serial.println();
wave.play();

while (wave.isplaying) {
//putstring(".");
delay(100);
}
}
}
}
}
void displayTime()
{
Serial.print(" ");
Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
Serial.print(":");
Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
Serial.print(":");
Serial.print(RTC.get(DS1307_SEC,false));//read seconds

}

void playSchduled()
{
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);

if(EEPROM.read(stateset+12)==1)//&&(slpSng==2))
{
if((analogRead(3)>150))
{
playcomplete(convertIntTOwav(EEPROM.read(stateset+13)),4);
//sched=0;
}
}
else
{
sd=0;
}

}



byte convertToEEPROM(byte num)
{
num=(1+(14*num));
return num;
}

void checkWakeUpTime()
{
//int h,m,d;
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);
if(EEPROM.read(stateset)==1)
{
stateup=RTC.get(DS1307_HR,false);
stateStop=RTC.get(DS1307_MIN,false);
if((stateup==EEPROM.read(stateset+1))&&(stateStop==(EEPROM.read(stateset+2)-1))&&(wakeLght==0))// test to see if we are 2 minutes closer to the wake up alarm and start the action with the lights
{
stateset=stateset+3;
sendToLamp(EEPROM.read(stateset));// send the right action to the lamp
wakeLght=1;
sd=0;
}
else if((stateup==EEPROM.read(stateset+1))&&(stateStop==EEPROM.read(stateset+2))&&(wakeSng==0))
{

wakeSng=1;
//playWakeUp();
}

if(stateStop!=(EEPROM.read(stateset+2)+2))
{
wakeLght=0;
}

if((stateStop!=(EEPROM.read(stateset+2)))&&(remm==2))
{
wakeSng=0;
}
}
}

void checkSleepTime()
{
//int h,m,d;
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);
if(EEPROM.read(stateset+5)==1)
{
stateup=RTC.get(DS1307_HR,false);
stateStop=RTC.get(DS1307_MIN,false);
if((stateup==EEPROM.read(stateset+6))&&(stateStop==(EEPROM.read(stateset+7))+1)&&(slpLght==0))// test to see if we are 2 minutes closer to the sleep alarm and start the action with the lights
{
stateset=stateset+8;
sendToLamp(EEPROM.read(stateset));// send the right action to the lamp
slpLght=1;
sd=1;
}
else if((stateup==EEPROM.read(stateset+6))&&(stateStop==EEPROM.read(stateset+7))&&(slpSng==0))
{

slpSng=1;


}
if(stateStop==(EEPROM.read(stateset+7))+2)
{
slpLght=0;
}
if(stateStop!=(EEPROM.read(stateset+7)))
{
slpSng=0;
}
}
}

void remmember()
{
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);
if((EEPROM.read(stateset+10)==1))//&&(remm==1))
{
delay(5000);//just for presentation to be nice. actual time should be like 4 minutes

playcomplete(convertIntTOwav(EEPROM.read(stateset+11)),3);// do a call to the playcomplete function to start playing the wake up song.
remm=2;
wakeSng=2;
}
else
{
remm=2;
wakeSng=2;
}
}

String convertIntTOwav(int value)
{
String name="";
name.append(value);
name.append(".WAV");
return name;
}

void playWakeUp()
{
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);

while((analogRead(3)>150))//&&(wakeSng==1))
{
playcomplete(convertIntTOwav(EEPROM.read(stateset+4)),2);
}
remm=1;
wakeSng=2;
}


void playSleep()
{
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);
if(analogRead(3)<100)
{
playcomplete(convertIntTOwav(EEPROM.read(stateset+9)),1);
}

slpSng=2;

}

void presentation()
{
Serial.print(12, BYTE);
Serial.print(" PRESENTATION ");
delay(1000);
stateset=RTC.get(DS1307_DOW,false);
stateset=convertToEEPROM(stateset);
stateStop=RTC.get(DS1307_MIN,false);
stateup=RTC.get(DS1307_HR,false);
EEPROM.write(stateset+6,stateup);//=;//test to see if it works or change it to add plus 1
EEPROM.write(stateset+7,stateStop+1);//=;//test to see if it works or change it to add plus 1
EEPROM.write(stateset+2,stateStop+5);
EEPROM.write(stateset+1,stateup);//=;//test to see if it works or change it to add plus 1


}

void setAlarm()
{

byte a;
byte count=0;
byte c=1;
a=1;
alarm[0]=1;

while(c==1)
{

Serial.print(12, BYTE);
Serial.print("Set Alarm");
Serial.print(13, BYTE);
Serial.print("Minutes :");
Serial.print(count,DEC);

stateup=digitalRead(up);
stateon =digitalRead(15);
delay(200);

if(stateup==LOW)
{
count++;
}

if(count>59)
{
count=0;
}
if(stateon==LOW)
{
alarm[1]=count;
count=0;
c=2;
}


}
while(c==2)
{
Serial.print(12, BYTE);
Serial.print("Set Alarm");
Serial.print(13, BYTE);
Serial.print("Hours :");
Serial.print(count,DEC);

stateup=digitalRead(up);
stateon =digitalRead(15);
delay(200);

if(stateup==LOW)
{
count++;
}
if(count>23)
{
count=0;
}
if(stateon==LOW)
{
alarm[2]=count;
count=1;
c=3;

}

}
}
void checkAlarm()
{
if(alarm[0]==1)
{
stateup=RTC.get(DS1307_HR,false);
stateStop=RTC.get(DS1307_MIN,false);
if((stateup==alarm[2])&&(stateStop==(alarm[1]))&&(almSng==0))// test to see if we are 2 minutes closer to the sleep alarm and start the action with the lights
{
almSng=1;
playcomplete("6.WAV",5);

}
}
if(stateStop!=(alarm[1]))
{
almSng=0;
}
}



void setTime()
{
//byte time[3];
byte count=0;

byte a;
a=1;

Serial.print(12, BYTE);
Stop=0;
stateStop=0;
while(a==1)
{
//Serial.print(22, BYTE);
Serial.print(12, BYTE);
Serial.print("Set Time");
Serial.print(13, BYTE);
Serial.print("Minutes :");
Serial.print(count,DEC);

stateon =digitalRead(15);
//statedn=digitalRead(down);
stateset=digitalRead(set);
delay(200);
if(stateon==LOW)
{
Stop=count;
count=0;
a=2;
//b=1;
}
if(stateset==LOW)
{
count++;
}
if(count>59)
{
count=0;
}
}
while(a==2)
{

Serial.print(12, BYTE);
Serial.print("Set Time");
Serial.print(13, BYTE);
Serial.print("Hours :");
Serial.print(count,DEC);

stateon =digitalRead(15);
//statedn=digitalRead(down);
stateset=digitalRead(set);
delay(200);
if(stateon==LOW)
{
stateStop=count;
count=1;
a=3;
}
if(stateset==LOW)
{
count++;
}

if(count>23)
{
count=0;
}
}


RTC.stop();
RTC.set(DS1307_SEC,0); //set the seconds
RTC.set(DS1307_MIN,Stop); //set the minutes
RTC.set(DS1307_HR,stateStop); //set the hours
//RTC.set(DS1307_DOW,stateset);
RTC.start();
Stop=6;// set it back to 6 which is pin 6 so others can use it as such
a=0;
}

void sendToLamp(int data)
{

mySerial.print(data);
}

Source code for light control device

#include "TimerOne.h" // Avaiable from http://www.arduino.cc/playground/Code/Timer1

volatile int i=0; // Variable to use as a counter
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 10; // Output to Opto Triac
int Dimmer_pin = 0; // Pot for testing the dimming
int LED = 3; // LED for testing
int dim = 0; // Dimming level (0-128) 0 = on, 128 = 0ff
int freqStep = 78; // Set the delay for the frequency of power (65 for 60Hz, 78 for 50Hz) per step (using 128 steps)
// freqStep may need some adjustment depending on your power the formula
// you need to us is (500000/AC_freq)/NumSteps = freqStep
// You could also write a seperate function to determine the freq

#include

NewSoftSerial mySerial(5,6);
int val;
int val2;
int value;
int dm=0;
int bt=0;
int of=0;
int onn=0;
int dmof=0;
int bton=0;
byte some=0;
void setup()
{
onn=1;
mySerial.begin(19200);
Serial.println("Beginning ... ");
//pinMode(pin6,INPUT);
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
pinMode(LED, OUTPUT); // Set the LED pin as output
attachInterrupt(0, zero_cross_detect, CHANGE); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection , this is where is did some modification adn used change instead of falling.
Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
Timer1.attachInterrupt(dim_check, freqStep); // Use the TimerOne Library to attach an interrupt

}
void zero_cross_detect() { // function to be fired at the zero crossing
zero_cross = 1; // set the boolean to true to tell our dimming function that a zero cross has occured
}
void dim_check() // Function will fire the triac at the proper time
{
digitalWrite(AC_pin, LOW); // Turn off the Triac gate (Triac will not turn off until next zero cross)
if(zero_cross == 1) // First check to make sure the zero-cross has happened else do nothing
{
if(i>=dim) // Check and see if i has accumilated to the dimming value we want
{

digitalWrite(AC_pin, HIGH);// Fire the Triac mid-phase

i = 0; // Reset the accumilator
zero_cross = 0; // Reset the zero_cross so it may be turned on again at the next zero_cross_detect
} else {
i++; // If the dimming value has not been reached, incriment our counter
} // End dim check
} // End zero_cross check
}

void loop()
{
recieve();

}




void recieve()
{

if (mySerial.available())
{
val2=(int)mySerial.read();
val2=val2-48;
if(val2==1)
{
bton=0;
dimmer();
}
else if(val2==2)
{

bton=0;
brigth();

}
else if(val2==3)
{

//sendAck();
bton=0;
on();
}
else if(val2==4)
{
bton=0;
off();
}
else if(val2==5)
{
on();
bton=1;
}
else if(val2==6)
{
bton=1;
off();

}
}
}
void dimmer()
{
bt=0;
if(dm!=1)
{
dm=1;
for(int i=5;i<=115;i++)
{
dim=i;
//delay(50);
if(dim>30)
{
delay(500);
}

}

}
else
{
dim=115;
//delay(50);
}

}

void brigth()
{
dm=0;
if(bt!=1)
{
bt=1;
for(int i=115;i>=5;i--)
{
dim=i;
if(dim<90)
{
delay(400);
}

}
}
else
{
dim=5;
delay(50);
}

}

void off()
{
dm=0;bt=0;
dim=115;
delay(50);
}
void on()
{
dm=0;bt=0;
dim=5;
delay(50);

}

måndag 7 december 2009

Light package finished, start with the main package!

So the light package is finished, now we are starting with the MAIN package .. the alarm clock.





Good job

The work with the hardware continues. Soldering and more soldering needs to be done. The test phase is starting and we are all happy with the work so far ;)






Light package and the lamp

After this we started to work with the package that should control the light. Also we had some time to build a lamp. The design that we wanted was that is was supposed to feel soft and make you sleepy. We found a fabric taht looks almost like a cloud and was very soft .. this fabric was also very easy to put on and also made everything look beuatiful without doing much more ;)






More work

Here Miao is working with the Interface and also you can see the work done with the wave shield and our Arduino. Soon the hardware will be finished :)