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;

}

}


No comments:

Post a Comment