Hi there!
I want to make a program that reads the movement on X and Y axis and turns a LED on.
For mevement on X axis-turn on Red led and for movement on Y axis-turn on Yellow led.
For the X axis it's ok but i can't find watt's wrong for the Y axis.
Any ideas.
Here is the program(what must me changed in order to turn the Yellow Led for the Y axis?):
#define PRAG_X_1 0.5
#define PRAG_X_2 1
#define PRAG_X_3 2
#define PRAG_Y_1 0.5
#define PRAG_Y_2 1
#define PRAG_Y_3 2
#define SMOOTH_X 0.4
#define SMOOTH_Y 0.4
void setup(){
pinMode(2, OUTPUT);
pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
}
float oldAccX;
float oldAccY;
void loop() {
float accX = smooth(readAcc(0), SMOOTH_X, oldAccX);
float accY = smooth(readAcc(1), SMOOTH_Y, oldAccY);
if (accX < -PRAG_X_2){
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
if (accY < -PRAG_Y_2){
digitalWrite(8, HIGH);
}
else {
digitalWrite(8, LOW);
}
}
float readAcc(int port) {
int value=analogRead(port);
int miliVolts=map(value,0,,1023,0,3300)-3300/2;
float acc=(float)miliVolts/360;
return acc;
}
float smooth(float data, float filterVal, float smoothedVal) {
if (filterVal > 1) {
filterVal = .99;
}
else if (filterVal <= 0){
filterVal = 0;
}
smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal);
return (float)smoothedVal;
}
I want to make a program that reads the movement on X and Y axis and turns a LED on.
For mevement on X axis-turn on Red led and for movement on Y axis-turn on Yellow led.
For the X axis it's ok but i can't find watt's wrong for the Y axis.
Any ideas.
Here is the program(what must me changed in order to turn the Yellow Led for the Y axis?):
#define PRAG_X_1 0.5
#define PRAG_X_2 1
#define PRAG_X_3 2
#define PRAG_Y_1 0.5
#define PRAG_Y_2 1
#define PRAG_Y_3 2
#define SMOOTH_X 0.4
#define SMOOTH_Y 0.4
void setup(){
pinMode(2, OUTPUT);
pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
}
float oldAccX;
float oldAccY;
void loop() {
float accX = smooth(readAcc(0), SMOOTH_X, oldAccX);
float accY = smooth(readAcc(1), SMOOTH_Y, oldAccY);
if (accX < -PRAG_X_2){
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
if (accY < -PRAG_Y_2){
digitalWrite(8, HIGH);
}
else {
digitalWrite(8, LOW);
}
}
float readAcc(int port) {
int value=analogRead(port);
int miliVolts=map(value,0,,1023,0,3300)-3300/2;
float acc=(float)miliVolts/360;
return acc;
}
float smooth(float data, float filterVal, float smoothedVal) {
if (filterVal > 1) {
filterVal = .99;
}
else if (filterVal <= 0){
filterVal = 0;
}
smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal);
return (float)smoothedVal;
}

Attachments
-
1.5 MB Views: 33
Last edited by a moderator: