Friday, February 21, 2014

More on Accelerometer!

I was able to figure out how to make the Arduino blink according to movement of the accelerometer!

From reading the helpful information on StackOverflow, I read that positive values indicate increased velocity and negative values indicate decreased velocity; while zero indicates constant velocity. With that said, I concluded that the positive and negative values will be when the baby breathes in and out. Whereas, on the other hand, zero would indicate that the baby has stopped breathing. And so, at the moment, I have the accelerometer have pin13 turn off when the values are not zero and turn on when it is zero. Later, I might make it more sensitive and have a warning for when the breathing of the baby slows.

In the last loop of the code I got from the guide, I added an if/else statement at the end. Also, outside the void setup placed earlier in the code, I had to write:
int led = 13
Inside the void setup, I wrote:
pinMode(led, OUTPUT).


void loop(void) 
  /* Get a new sensor event */ 
  sensors_event_t event; 
  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  ");
  Serial.println("m/s^2 ");

  if (event.acceleration.y != 0 && event.acceleration.z != 0)
  else if (event.acceleration.y == 0 && event.acceleration.z == 0)
       digitalWrite(led, LOW);
Very simple, and it has worked for me. I didn't include "x" into the condition because I remember reading somewhere that x will always have same orientation and thus be of trouble in other calculations, or... something like that.

Planning on also ordering a gyroscope, which will help orientation reading. Another device to learn, haha.

Next step:

  • How to work a gyroscope!

No comments:

Post a Comment