This time i got Memsic accelerometer. The task was to find out what this sensor is and what it does.
The first thing what I did was that I opened arduino.cc and searched that sensor. There was a clear picture how to connect the sensor and in the development environment we found under sensors the basic code for this sensor. Link to the sensor:
http://arduino.cc/en/Tutorial/Memsic2125?from=Tutorial.AccelerometerMemsic2125
Then we just opened device monitor to see which axis where which and then added two leds which start to blink slower when the angle is growing. Picture of the setup:
The actual code, we added if-else statement that if the angle is growing the led should blink slower. Few videos here about the testing:
The code:
// these constants won't change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g's:
// earth's gravity is 1000 milli-g's, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;
if (accelerationX > 100 && accelerationX < 200){
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
delay(50);
}
else if (accelerationX > 200 && accelerationX < 300)
{
digitalWrite(4, HIGH);
delay(200);
digitalWrite(4, LOW);
delay(200);
}
else if (accelerationX > 300 && accelerationX < 400)
{
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
}
else if (accelerationX > 400 && accelerationX < 500)
{
digitalWrite(4, HIGH);
delay(600);
digitalWrite(4, LOW);
delay(600);
}
else {
digitalWrite(4, LOW);
}
//--------------------------------------------------------------------------------
if (accelerationY > 100 && accelerationY < 200){
digitalWrite(5, HIGH);
delay(50);
digitalWrite(5, LOW);
delay(50);
}
else if (accelerationY > 200 && accelerationY < 300)
{
digitalWrite(5, HIGH);
delay(200);
digitalWrite(5, LOW);
delay(200);
}
else if (accelerationY > 300 && accelerationY < 400)
{
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
delay(500);
}
else if (accelerationY > 400 && accelerationY < 500)
{
digitalWrite(5, HIGH);
delay(600);
digitalWrite(5, LOW);
delay(600);
}
else {
digitalWrite(5, LOW);
}
// print the acceleration
Serial.print(accelerationX);
// print a tab character:
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();
delay(100);
}
Ei kommentteja:
Lähetä kommentti