Highlights

Posted by : Noctis King Tuesday, April 15, 2014

The program of LEDs and Accelerator

    In this week, our group made a monitor by using LEDs to react the change of accelerator.
    Now, we will show and explain how this program work.


const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A0;                  // x-axis of the accelerometer
const int ypin = A1;                  // y-axis
const int zpin = A2;                  // z-axis (only on 3-axis models)
int ledx=13;                          // the pin of x-axis LED
int ledy=10;                          // the pin of y-axis LED
int ledz=4;                           // the pin of z-axis LED

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600); // set the data rate in bits per second
  
  //initialize the digital pin as an output.
  digitalWrite(groundpin, LOW); 
  digitalWrite(powerpin, HIGH); 
  pinMode(ledx, OUTPUT); 
  pinMode(ledy,OUTPUT); 
  pinMode(ledz,OUTPUT); 
}

void loop()
{
  // set the initial value of accelerator
  int ax = analogRead(xpin); // the initial value of x-axis
  int ay = analogRead(ypin); // the initial value of y-axis
  int az = analogRead(zpin); // the initial value of z-axis
  
  while (true) // loop, tracking and reacting the change 
  {
    int Tax = analogRead(xpin); // reading the x-axis value after changing
    // determining the changing margin of x-axis' value 
    // if value change bigger than 50, the LED will blink
    // if not, it will closs
    if (abs(Tax - ax) > 50)  
    {
      digitalWrite(ledx, HIGH); 
    }
    else
    {
      digitalWrite(ledx, LOW);
    }
    int Tay = analogRead(ypin);
    if (abs(Tay - ay) > 50)
    {
      digitalWrite(ledy, HIGH);
    }
    else
    {
      digitalWrite(ledy, LOW);
    }
    int Taz = analogRead(zpin);
    if (abs(Taz - az) > 50)
    {
      digitalWrite(ledz, HIGH);
    }
    else
    {
      digitalWrite(ledz, LOW);
    }
  }
  
  
  // delay before next reading:
  delay(100);
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © 2013 Group 106-11 - Date A Live - Powered by Blogger - Designed by Johanes Djogan -