Monday, August 9, 2010

v2.0 Lucid dream aid - Eyepiece


Details shortly

v1.0 Lucid dream aid Source code

Here will be presented the source code for the lucid dream aid based on the PIC 12F683 microcontroller. This code was written in HI-TECH C lite, which is free for anyone.

/*********************************************************************
*
* Konstantin Avdashchenko Lucid dreaming aid.
*
********************************************************************/

/** I N C L U D E S **********************************************************/
#include
#include
//#include
#include "delay.h"

/** C O N F I G U R A T I O N ************************************************/

#define REDon GPIO5 = 0; GPIO4 = 1;
#define LEDoff GPIO5 = 0; GPIO4 = 0;
#define IRon GPIO5 = 1; GPIO4 = 0;

#define XTAL 4000000
#define BRATE 9600
#define TxData GPIO0 /* Map TxData to pin */

/** V A R I A B L E S ********************************************************/

unsigned short num[30];
unsigned short *ptr = &num[0];
unsigned char i,tmp,tmp1,tmp2,Pass,ni;
//unsigned char valu[];

/** P R I V A T E P R O T O T Y P E S ***************************************/
void Judge (void);
void Flash (void);
void Baud (void);
void Check (void);
void Fill (void);


/** D E C L A R A T I O N S **************************************************/
#define SCALER 10000000
#define ITIME 4*SCALER/XTAL /* Instruction cycle time */
#if BRATE > 1200
#define DLY 3 /* cycles per null loop */
#define TX_OHEAD 13 /* overhead cycles per loop */
#else
#define DLY 9 /* cycles per null loop */
#define TX_OHEAD 14
#endif
#define RX_OHEAD 12 /* receiver overhead per loop */

#define DELAY(ohead) (((SCALER/BRATE)-(ohead*ITIME))/(DLY*ITIME))


/******************************************************************************
* Function: unsigned int sendu (unsigned char chan)
*
* PreCondition: None
*
* Input: byte to send
*
* Output: uart structured byte to the rx pin of pickit2
*
* Side Effects: None
*
* Overview: sends data to uart reciever
*
* Note: None
*****************************************************************************/

void
sendu (unsigned short byte)
{
GPIO1 = 1;
DelayUs(100);
GPIO1 = 0;
i=0;
GPIO0 = 0;
Baud();
while (i!= 8){
if((byte>>i) & 1) GPIO0 = 1;
if(!((byte>>i) & 1)) GPIO0 = 0;
Baud();
i++;
}
GPIO0 = 1;
Baud();
Baud();

GPIO1 = 1;
DelayUs(100);
GPIO1 = 0;
}

/******************************************************************************
* Function: void Fill(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Fills array for checker.
*
* Note: None
*****************************************************************************/

void Fill(){
*ptr = ADRESL;
if (ptr == &num[30]){
Check();
ptr = (&num[0] -1);
}
ptr++;
}

/******************************************************************************
* Function: void Check(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Checks wether eyes are moving.
*
* Note: None
*****************************************************************************/

void Check(){
unsigned char diff, count = 0;
for (i=0;i<29;i++){
tmp1 = num[i];
tmp2 = num[i+1];
if (tmp1 >= tmp2) diff = tmp1 -tmp2;;
if (tmp2 >= tmp1) diff = tmp2 -tmp1;;
if (diff >= 2)count++;
}
if (count >= 2) Flash();
}

/******************************************************************************
* Function: void Flash(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Flashes the LED
*
* Note: None
*****************************************************************************/
void Flash(void){
i=0;
while(i <>
REDon
DelayMs(50);
LEDoff
DelayMs(30);
i++;
}
sendu(0x69);
}
/******************************************************************************
* Function: unsigned int readadc (unsigned char chan)
*
* PreCondition: None
*
* Input: char of channel to read from
*
* Output: 10 bit value of read adc
*
* Side Effects: None
*
* Overview: reads Analog signals
*
* Note: None
*****************************************************************************/

unsigned int
readadc (unsigned char chan)
{
IRon
unsigned short value;
ADCON0 = ((chan <<>
ADON = 1; // AD module enabled
DelayUs(5); // wait for conversion
GODONE = 1; // Conversion start
while(GODONE); // wait to finish
value = ADRESL; // takes the high 8 bits of 10 to reduce noise
ADON = 0; // disable module
return value;
}



/******************************************************************************
* Function: void main(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Main program entry point.
*
* Note: None
*****************************************************************************/
void main(void)
{
OSCCON = 0x60;
ANSEL = 0x34;
TRISIO = 0x04;
GPIO = 0x00;
CMCON0 = 0x07;
ADCON0 = 0x09;
// GPIO = 0x20;

tmp = 0;
while(tmp<5){
REDon
DelayMs(50);
LEDoff
DelayMs(50);
tmp++;
}

GPIO0 = 1;

// T0IE = 1;
// TMR0 = 0xE1;
// GIE = 1;

// num = 10;

/* serial sending works
unsigned short pass;

while(1){
IRon
pass = readadc(2);
LEDoff
sendu(pass);
Flash();
}
*/

//
tmp= 0;
Pass= 0;

tmp = readadc(2);
DelayMs(10);
while(1){
Pass = readadc(2);
Fill();
sendu(Pass);
DelayMs(1);
}

// continue; // let interrupt do its job
}//end main


/******************************************************************************
* Function: unsigned int Baud (unsigned long baud)
*
* PreCondition: None
*
* Input: baud rate to delay for
*
* Output: None
*
* Side Effects: None
*
* Overview: delays before sending next bit
*
* Note: None
*****************************************************************************/

void Baud (void)
{
DelayUs(140);
}

v1.0 Lucid dream aid - Paperclip style

This post will present a way to make a Paperclip-based lucid dream aid. The ingredients you will need are a SMD red led, a PIC 12F683, an IR photo-reflective sensor (CNB1302), a 5-10k ohm resistor, Paperclips, CR2032 3v battery, An Altoids case, bits of wire (in my case 26 gauge enameled wire), and lastly, something comfortable to wear while sleeping (Go Eye patch!). The ingredients for this build can be seen in the following image:

Shown above are the ingredients for the build and the frame of the device assembled. Now that we know what we need, here is the schematic/info sheet for the build:

The image above shows the schematic. Besides this schematic, I wrote down which GPIO(General Purpose I/O) must be high for the corresponding led lights. On the right, you see the table for the used ports and their associated pin numbers. The frame for the electronics was built in the following way:


An Altoids case was cut apart so that it could be soldered to the paperclip to create a solid working space. The paperclip was widened so that it would clip around the eye patch and the CR2032 battery. The header seen on the back of the device is an ICSP header used to program the device. Most of the frame is covered in tape as the frame would be tied to the positive voltage supply of the battery. In wiring the device, I used 26 AWG enameled wire to connect all the pins.
As the frame was tied to positive I had less wires to run as the components could be tied to the frame for positive voltage. The resistor I mentioned earlier is seen on top of the microcontroller (I scavenged it from somewhere, along with the red led). The following images show the assembled device from top and bottom (with its skin on) and how the device would be used, from top and bottom:




The device operates when the paperclip is clipped around the mask and the battery and a
wire from ground is placed between the negative terminal of the battery and the mask. The source code for this build will be uploaded in the next post.