Hello, all
Overview of the project: Using a toy EEG, this device is supposed to help me wake up at the best time in the morning (ie in light sleep)
Overview of he problem: Although each of the small parts of the code worked separately, when I put them all together it compiles but does not work. If you look in the void setup, once the code is started up it should print out "Initializing SD card..." to the serial monitor. It does not do that. Nor does it print out to the Bluetooth console.
**Details of the project: ** There are a few simple functions.
playTone: takes in a pin and plays a tone on the piezo. This has been tested and works successfully
addValue: this function adds a new value into the array by removing the oldest one and shifting all the others over one. This too works alone
isAwake: this function finds the average of the array if it is equal to or greater than the input tolerance it returns a boolean based on the result of the computation. This also works alone
In the void setup you will see the whole setup for getting the time up and running. An android app sends the current time and the desired wake up range to the arduino in bytes. In the void setup there is a while true that waits for this processs to be finished.
After that process the array is populated with the first 120 packets. but these parts of the setup are not really significant because I believe the error occurs before hand, because the serial monitor is not printing out the messege
#include "pitches.h"
#include <SD.h>
#include <Brain.h>
#include <Time.h>
#include <SoftwareSerial.h>
const int sampleSize=120; // the amount of packets needed to start actual computation
const int chipSelect = 4;
const long tolerance = 55000;//lowest amplitude for light sleep
SoftwareSerial blue(9,10); //nein- connect the bluetooth to 9, 10.
int data;
// The next few variables are for the alarm feature
int counter=0;
String setH;
String setMinutes;
String setSeconds;
String minHour;
String minMinute;
String maxHour;
String maxMinute;
long brain_data[sampleSize];
Brain brain(Serial);
// this function plays a piezo buzzer at the given pin
void playTone(int pin){
int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4,4,4,4,4 };
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000/noteDurations[thisNote];
tone(pin, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(pin);
}
}
//this function adds a value to the global array, it deletes the last one and moves all the others over one
void addValue(int number){
for (int i=sampleSize-1; i>=1; i--){
brain_data[i] = brain_data[i-1];
}
brain_data[0]=number;
}
// this function takes in a tolerance,and finds the average of the global array. If the average is bigger than the tolerance the user is in light sleep and the function returns true
boolean isAwake(long tolerance){
long sum = 0;
for (int i = 0; i < sampleSize; i++){
sum += brain_data[i];
}
long average = sum / sampleSize;
if (average >= tolerance){
return true;
}
else return false;
}
void setup()
{
Serial.begin(9600);
blue.begin(9600);
Serial.print("Initializing SD card..."); //so this is where the error occurs. This does not print out.
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
int i = 0;
while (true){
if (blue.available()){
if (counter<2){
int num= (int)blue.read()-48;
setH+=(String)num;
counter++;
}
else if (1<counter && counter<4){
int num= (int)blue.read()-48;
setMinutes+= (String)num;
counter++;
}
else if (3<counter && counter<6){
int num= (int)blue.read()-48;
setSeconds+=(String)num;
counter++;
}
else if (5<counter && counter<8){
int num= (int)blue.read()-48;
minHour+=(String)num;
counter++;
}
else if (7<counter && counter<10){
int num= (int)blue.read()-48;
minMinute+=(String)num;
counter++;
}
else if (9<counter && counter<12){
int num= (int)blue.read()-48;
maxHour+=(String)num;
counter++;
}
else {
int num= (int)blue.read()-48;
maxMinute+=(String)num;
counter++;
}
}
if (counter>=14){
break;
}
}
setTime(setH.toInt(), setMinutes.toInt(), setSeconds.toInt(), 1, 1, 11);
Serial.println("Wake up minimum time " + minHour+":"+ minMinute);
Serial.println("Wake up maximum time " + maxHour+":"+maxMinute);
while ( i<=sampleSize){
if (brain.update()){
addValue((long)brain.readDelta()) ;
Serial.println(brain.readDelta());
i++;
}
}
}
void loop()
{
time_t t=now(); //time now
// make a string for assembling the data to log:
String dataString = "...";
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
if (brain.update()){
dataString = brain.readCSV();
addValue((long)brain.readDelta());
if (hour(t)== minHour.toInt()){
if(minute(t) >= minMinute.toInt()){
if(isAwake(tolerance)){
playTone(8);
dataString += "WOKE UP";
dataString += hour(t);
dataString += minute(t);
}
}
}
else if(hour(t)==maxHour.toInt()){
if(minute(t)<=maxMinute.toInt()){
if(isAwake(tolerance)){
playTone(8);
dataString += "WOKE UP";
dataString += hour(t);
dataString += minute(t);
}
}
}
}
// if the file is available, write to it:
if (dataString.length() > 5){
File dataFile = SD.open("brain2.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
//delay(1000);
}