Input
Back to main page: PRE2015_3_Groep4
To the code: Code
To the output: Output
Gathering Real-Time Data
Gathering Real-Time data is rather easy using the Arduino. When connected with a computer through USB, an Arduino can print values directly to a Serial Monitor. Thankfully, there is an add-on[1] for Matlab, which enables Matlab to read the Serial Monitor, and also return commands to the Arduino.
The Arduino needs some code to measure the input of the sound sensor, and print the values to the Serial Monitor. A figure of the code can be found at the page code.
Our intent was to have as much of the program running on the Arduino itself. That means that you, in theory, would be able to have the Arduino as a standalone product. In our case, that would mean you start the program from your computer, and then would be able to unplug your Arduino, since it is able to run the whole code by itself. This was however not possible because of multiple reasons. One of the reasons discussed here is saving the gathered data to read it.
For a more accurate calculation of the wake up moment, as much measurements as able should be saved. The problem is the size of the information, and saving that information on the Arduino. The used Arduino (Arduino Mega[2]) has up to 256 KB of storage. Not all of this can be used, since a lot would already be taken up by the program itself, and 8 KB for the bootloader. If we then want to review the last 30 minutes to choose the best moment to wake up (the larger the reviewed period, the more accurate the decision), the sample time has to be reduced dramatically. Reducing the sample time results in an inaccurate measurement, leading to the program not able to choose the correct moment to wake the person. For this reason, we chose to keep the laptop in the loop for this prototype, since a laptop has more than enough storage to store samples of the whole night. In our tests, the files containing all the measurements had a size of ~130 MB.
KAN DIT WEG?::
/*---------- Pin initialization ----------*/ const int inputPin = A0; // Input from the sensor const int outputPin = 13; // Pin for the output light /*---------- Editable values ----------*/ int counter = 50; // Number of averages taken. Average is taken to filter out small noise float timeStep = 0.1; int maxWakeUpTime = 60; // Alarm will wake you up no later than maxWakeUpTime int wakeUpRange = 15; // Time allowed before maxWakeUpTime bool printPeaks = false; // Testing feature; if true, it will print the peak values, otherwise, it will just print the measured values /*---------- Variables used by the program ----------*/ int timer = 0; // Clock int oldValueTimer; // Integer to remember old value for printing, if that turns out to be a peak float measurement; // Sensor value float currentValue = 0; // Current value float oldValue; // Previous value bool oldPlus = false; // Boolean to save if graph is increasing or decreasing // true false void setup() { Serial.begin(9600); // Set up output window Serial.println("CLEARDATA"); // Initialization of the Excel Table, needed for the PLX-DAQ Serial.println("LABEL,Time,Peak Value"); // Names of the rows, LABEL is for the PLX-DAQ pinMode(inputPin, INPUT); // Sensor input pin pinMode(outputPin, OUTPUT); // Light output pin digitalWrite(outputPin, LOW); // Light starts off being out } void loop() { for (int i = 0; i < counter; i++){ // Loop to take average value delay(1000); // Progress a second timer = timer + timeStep; measurement = analogRead(inputPin); currentValue = currentValue + measurement; if (printPeaks == false){ // Prints out the measurements to the Serial monitor Serial.print(timer); Serial.print(","); Serial.println(measurement); } } currentValue = currentValue / counter; // currentValue is now the average if (currentValue < oldValue && oldPlus == true){ // If the graph is just behind the peak (function is decreasing, but the last measurement was increasing) --> peak is found oldPlus = false; if (oldValueTimer >= maxWakeUpTime - wakeUpRange){ // If the peak is within waking up range digitalWrite(outputPin, HIGH); } if (printPeaks == true){ // Print out found peaks to the Serial monitor Serial.print(oldValueTimer); Serial.print(","); Serial.println(oldValue); } } if (currentValue > oldValue){ // Change oldPlus to true, indicating the function is increasing oldPlus = true; } if (timer >= maxWakeUpTime){ digitalWrite(outputPin, HIGH); // Turn on the light hard coded if time is at the end of your alarm } oldValue = currentValue; // Update old value and reset the current value for the next loop oldValueTimer = timer; currentValue = 0; }
References
- ↑ Arduino Support for Matlab, http://nl.mathworks.com/hardware-support/arduino-matlab.html
- ↑ The Arduino Mega, https://www.arduino.cc/en/Main/ArduinoBoardMega2560