Friday, May 04, 2007

Code Again

Here is the code for the breadboard gesture sensor. I actually commented it.


//Values for the LEDs
//top
int ledPin1 = 7;
//right
int ledPin2 = 8;
//bottom
int ledPin3 = 9;
//left
int ledPin4 = 10;
//power
int ledPinP = 13;
//Sensor Pins
//Accelerometer Analog Input
int sensorPinZ = 0;
int sensorPinY = 1;
int sensorPinX = 2;
//Values for sensosrs
int valX = 0;
int valY = 0;
int valZ = 0;
//values for gesture recognition
//creates a high number for the test value as to
// not trigger the loops
int testVal = 111;
//array to take it 100 points of data of X,Y,Z
int newMotion[100];
//this is a previously recorded swirl loop
int swirl[100] = {
202,414,397,199,416,405,198,420,410,194,420,414,191,420,
416,192,421,417,189,422,418,187,424,421,187,427,424,187,
432,426,185,434,430,186,440,434,185,445,439,187,451,444,
185,455,448,185,460,453,184,460,455,186,463,457,185,464,
460,187,465,461,190,467,463,192,467,464,192,469,465,195,
470,466,196,470,467,197,468,466,197,467,466,197,465,464,
197,464,462,195,462,460,197,462,460,198,461,460,198,461,
460,198};
//this is a previously recorder shake loop
int shake[100] = {
0,343,340,0,347,340,0,344,338,0,342,337,0,336,333,
0,333,329,0,324,323,0,317,317,0,310,311,0,306,307,
0,311,307,0,306,306,0,303,304,0,308,305,0,308,306,
0,308,307,0,309,308,0,309,309,0,314,311,0,315,314,
0,320,317,0,324,322,0,327,326,0,328,328,0,331,330,
0,333,333,0,334,335,0,337,337,6,338,339,13,341,341,
20,341,342,27,341,342,34,342,343,43};
//this are booleans to control the behaviors of the LEDS
boolean swirlGate = false;
boolean shakeGate = false;

void setup () {
//Declaring input and output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPinP, OUTPUT);
pinMode(sensorPinX, INPUT);
pinMode(sensorPinY, INPUT);
pinMode(sensorPinZ, INPUT);
//Setting up serial this is here to test the
//values and to calibrate it
Serial.begin(9600);
}

//the is a swirl checking function
void recognizeSwirl(){
for(int z = 0; z < 100; z++){
//check new array to old
testVal = abs(newMotion[z] - swirl[z]);
//Data for calibration
//Serial.println(testVal);
//if the test value is greater than 50 you are not
//swirling it. this variable should be calibrated
if(testVal<50) {
//if the value is less than 50 you are swirling it
swirlGate = true;
}
else {
//else you are not, end the loop
swirlGate = false;
z = 100;
}
}
}

//the is a swirl checking function
void recognizeShake(){
for(int z = 0; z < 100; z++){
//check new array to old
testVal = abs(newMotion[z] - shake[z]);
//Data for calibration
//Serial.println(testVal);
//if the test value is greater than 20 you are not shaking it.
//this variable should be calibrated
if(testVal<20) {
//if the value is less than 20 you are shaking it
shakeGate = true;
}
else {
//else you are not, end the loop
swirlGate = false;
z = 100;
}
}
}

//This function is used to light the correct LED
void lightMe(int l1) {
//PWM ramp up
for (int i=1; i<400; i++) {
digitalWrite(l1, HIGH);
delayMicroseconds(i);
digitalWrite(l1, LOW);
delayMicroseconds(i);
delay(3);
}
//PWM ramp down
for (int i=399; i>0; i--) {
digitalWrite(l1, HIGH);
delayMicroseconds(i);
digitalWrite(l1, LOW);
delayMicroseconds(i);
delay(3);
}
}

//flashing LED function
void otherlightMe() {
for (int i=0; i<10; i++) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
delayMicroseconds(i);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);

delayMicroseconds(i);
delay(5);
}
}

//primary loop function
void loop(){
//this is a power pin to know it is on
digitalWrite(ledPinP,HIGH);
//read 100 points of data
for(int i=0; i<100; i=i+3) {
newMotion[i] = analogRead(sensorPinX);
newMotion[i+1] = analogRead(sensorPinY);
newMotion[i+2] = analogRead(sensorPinZ);
}
//check if you are swirling it, this is done first
//in an if statement because arduino is odd
recognizeSwirl();
//if it is false see if you are shaking it
if (swirlGate == false) recognizeShake();
//swirl behavior
if (swirlGate == true) {
lightMe(ledPin1);
lightMe(ledPin2);
lightMe(ledPin3);
lightMe(ledPin4);
swirlGate = false;
}
//shake behavior
if (shakeGate == true) {
otherlightMe();
shakeGate = false;
}
}

0 Comments:

Post a Comment

<< Home