Tuesday, April 17, 2012

Final Project: Prior Iterations of the Code

Rev 3: This code attempts to separate the force of shaking into a soft, medium, and hard range.

#include
LiquidCrystal lcd(12,11,13,10,9,8);
int buttonpin = 4;
int buttonstate;
int uses = 20; //Literature search for the average number of uses per inhaler
const int groundpin = 14;
const int powerpin = 18;
const int ypin = 2; //changed inputs for x, y, and z because of newer software (it splits it directly with analogRead)
const int xpin = 3;
const int zpin = 1;
int yaccel;
int xaccel;
int zaccel;
int VecSum;
int force;
int FTvariable;
int time = 0;
int LEDpin;
int HardShakeCounter;
int SoftShakeCounter;

//play with these numbers
int mass = 5; // mass of the standard arduino
int LowThreshold = 597; //added this so its easier to change
int UpperThreshold = 1000; //

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


//PUSH A BUTTON FIRST THAT SAYS "START SHAKING"
//press button, make sure counters are set to 0)


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

if (zaccel < 900 && zaccel > 800)
{
HardShakeCounter++;
while (zaccel >500)
zaccel=analogRead(zpin); //keep reading until
Serial.print("Number of Hard Shakes = ");
Serial.println(
HardShakeCounter);
}

else if (zaccel > 500 && zaccel < 700);
{
SoftShakeCounter++;
while (zaccel >500)
zaccel=analogRead(zpin);
Serial.print("Number of soft shakes = ");
Serial.println(SoftShakeCounter);
}
}

----
Ammended Code Rev 2:

#include
LiquidCrystal lcd(12,11,13,10,9,8);
int buttonpin = 4;
int buttonstate;
int uses = 20; //Literature search for the average number of uses per inhaler
const int groundpin = 14;
const int powerpin = 18;
const int ypin = 2; //changed inputs for x, y, and z because of newer software (it splits it directly with analogRead)
const int xpin = 3;
const int zpin = 1;
int yaccel;
int xaccel;
int zaccel;
int VecSum;
int force;
int FTvariable;
int time = 0;
int LEDpin;

//play with these numbers
int mass = 5; // mass of the standard arduino
int LowThreshold = 597; //added this so its easier to change
int UpperThreshold = 1000; //


void setup(){
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
LEDpin = 9;
pinMode(LEDpin, OUTPUT);
lcd.begin(16,2);
pinMode(buttonpin, INPUT);
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);
VecSum = sqrt((xpin)^2+(ypin)^2+(zpin)^
2);
force = (VecSum*mass);
FTvariable = (force*time);

Serial.println(FTvariable);

if (VecSum > LowThreshold) //1st threshold is the zero state --> change this to a button to start time.
{
time = millis();
}

while (FTvariable >= UpperThreshold)//find how much shaking over how much time you need to be fully mixed
{
uses=uses-1;
if (uses == 0)
{
lcd.print("Refill Required");
}
digitalWrite(LEDpin, HIGH);
delay(5000);
digitalWrite(LEDpin, LOW);
FTvariable = 0;
}
}

----

Revision 1:

/* read the amount of shaking

determine the force exherted, F=ma

count the time of shaking

when the force * time hits a certain threshold, turn on the led

count the number of button pushes

print the number of uses left

when the number of uses equals zero, print "refill needed"

--to be sufficiently mixed, F*time = x, use x as threshold

--also must use x,y,z direction, so take the vector sum to get the overall force

--time should start counting when above another total acceleration threshold

*/

#include

LiquidCrystal lcd(12,11,13,10,9,8);

int buttonpin = 4;

int buttonstate;

int uses = ????;

const int groundpin = 14;

const int powerpin = 18;

const int ypin = 16;

const int xpin = 15;

const int zpin = 17;

int yaccel;

int xaccel;

int zaccel;

int VecSum;

int force;

int mass = ???;

int FTvariable;

int time = 0;

int LEDpin;

void setup(){

pinMode(groundpin, OUTPUT);

pinMode(powerpin, OUTPUT);

digitalWrite(groundpin, LOW);

digitalWrite(powerpin, HIGH);

LEDpin = 9;

pinMode(LEDpin, OUTPUT);

lcd.begin(16,2);

pinMode(buttonpin, INPUT);

Serial.begin(9600);

}

void loop(){

yvalue=analogRead(ypin);

xvalue=analogRead(xpin);

zvalue=analogRead(zpin);

VecSum = sqrt((xpin)^2+(ypin)^2+(zpin)^2);

force = (VecSum*mass);

FTvariable = (force*time);

if (VecSum > 1st THRESHOLD)

{

time = millis();

}

while (FTvariable >= UPPER THRESHOLD)

{

uses=uses-1;

if (uses == 0)

{

lcd.print("Refill Required");

}

digitalWrite(LEDpin, HIGH);

delay(5000);

digitalWrite(LEDpin, LOW);

FTvariable = 0;

}

}


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*/