Tuesday, April 17, 2012

Final Project: Asthma Device Code

 Here is the FINAL CODE for InhaleSur:


#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,13,10,9,8);

int buttonpin = 4;
int buttonstate;
int uses = 3; //Literature search for the average number of uses per inhaler

const int groundpin = 14;
const int powerpin = 18;
const int ypin = 2;
const int xpin = 3;
const int zpin = 1;
double yaccel;
double xaccel;
double zaccel;
double VecSum;
double total_VecSum;
int newtime=0;
int newesttime=0;
int counts=0;
int counting = 0;

void setup(){
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  pinMode(buttonpin, INPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
  lcd.begin(16,2);  //inserted
  Serial.begin(9600);
}

void loop(){
  yaccel=analogRead(ypin);  //changed these from yvalue to yaccel (just to match inputs above)
  xaccel=analogRead(xpin);
  zaccel=analogRead(zpin);
  newesttime++;
  delay(2);

if (!counting && xaccel>800 || yaccel>800 || zaccel>800)
{
   newesttime = 0;
   counting = 1;
}
if (counting && newesttime<500 && xaccel>800 || yaccel>800 || zaccel>800)
 {
   newesttime=0;
   while (xaccel>800 || yaccel>800 || zaccel>800)
   {
     yaccel=analogRead(ypin);  //changed these from yvalue to yaccel (just to match inputs above)
    xaccel=analogRead(xpin);
    zaccel=analogRead(zpin);
   }
   counts++;
 Serial.println(counts);

 }
else if (counting && newesttime>=500)
 {
   counting = 0;
   counts=0;
   Serial.println(counts);
 }

if (counts>=12)
{
  Serial.println("Ready to use!");
  lcd.print("Ready to use!");
  delay(3000);
  lcd.clear(); //alysa added this
  counts=0; //added this
  lcd.setCursor(0,0);
}

buttonstate = digitalRead(buttonpin);
  if (buttonstate==0)
  {
    uses=uses-1;
    delay(200);
    lcd.print(uses);
    lcd.println(" uses remaining      ");
    delay(5000);
    lcd.clear();
 
  if (uses == 0)
    {
      lcd.print("Refill Required");
      lcd.setCursor(0,0);
    }
  }


}



The following is the final draft of the code for our asthma inhaler device.
------------
const int groundpin = 14;
const int powerpin = 18;
const int ypin = 2;
const int xpin = 3;
const int zpin = 1;
double yaccel;
double xaccel;
double zaccel;
double VecSum;
double total_VecSum;
int newtime=0;
int newesttime=0;
int counts=0;
int counting = 0;

void setup(){
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
Serial.begin(9600);
}

void loop(){
yaccel=analogRead(ypin); //changed these from yvalue to yaccel (just to match inputs above)
xaccel=analogRead(xpin);
zaccel=analogRead(zpin);
newesttime++;
delay(2);

if (!counting && xaccel>800 || yaccel>800 || zaccel>800)
{
newesttime = 0;
counting = 1;
}
if (counting && newesttime<500 && xaccel>800 || yaccel>800 || zaccel>800)
{
newesttime=0;
while (xaccel>800 || yaccel>800 || zaccel>800)
{
yaccel=analogRead(ypin); //changed these from yvalue to yaccel (just to match inputs above)
xaccel=analogRead(xpin);
zaccel=analogRead(zpin);
}
counts++;
Serial.println(counts);

}
else if (counting && newesttime>=500)
{
counting = 0;
counts=0;
Serial.println(counts);
}

if (counts>=12)
{
Serial.println("Shaken");
}
}

/* while (newtime < 5000)
{
yaccel=analogRead(ypin); //changed these from yvalue to yaccel (just to match inputs above)
xaccel=analogRead(xpin);
zaccel=analogRead(zpin);
VecSum = sqrt((xaccel*xaccel)+(yaccel*yaccel)+(zaccel*zaccel));
total_VecSum=total_VecSum+VecSum;
delay(100);
newtime=newtime+100;
}

Serial.println(total_VecSum);
}
*/

//down increases (700) up decreases



/*break it down to categories - light, light medium , medium, hard
if acceleration value is between here and here, then build conditions for each of the shakes
assign a value to each of the shakes*/

No comments:

Post a Comment