11. November 2010
Here is the Arduino source code for the "Breathing Pillow". We used Arduino to be able to use the pillow without any connection to a computer.#include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int minSpeed = 20; // Minimal Speed int startValue = 180; // Start Value for the Servo int delayValue = 15; // Start Value for th delay, this value will be changed during runtime depends on the speed int sleepPos = 180; // current position of the servo max speed to make the sleep movement slower int pos = 0; // variable to store the servo position int sleepChange = 1; int delayChange = 1; int changeAfterRepeat = 3; int repeat = 0; void setup() { Serial.begin(9600); myservo.attach(13); // attaches the servo on pin 9 to the servo object } void loop() { int sensorValue = analogRead(A0); int thisPitch = map(sensorValue, 0, 1024, minSpeed, 180); int dif = startValue-thisPitch; if(dif >= 20 || dif <= -20) { startValue = thisPitch; sleepPos = thisPitch; delayValue= map(thisPitch, 0, 180, 60, 15); } for(pos = 0; pos < sleepPos; pos += 1) { myservo.write(pos); if(pos == 0 && sleepPos >0) { if(repeat ==changeAfterRepeat) { sleepPos -= sleepChange; repeat = 0; delayValue += delayChange; } else { repeat ++; } } delay(delayValue); } for(pos = sleepPos; pos>=1; pos-=1) { myservo.write(pos); delay(delayValue); } }