Sunday, October 21, 2018

Day 15 10/16/18

Arduino Homework

1.  Modify your program to use the accelerometer as an inclinometer so that it displays the angle that the board is tilted to in degrees. Interface your board so that it displays the output in degrees to your LCD display.


#include <math.h>
#include <LiquidCrystal.h>
#define PI 3.14
unsigned long time;
const int xpin = A2; // x-axis of the accelerometer
const int ypin = A1; // y-axis
const int zpin = A3; // z-axis
int xdata = 0;
int ydata = 0;
float xrad;
float yrad;
float degree;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
 // initialize the serial communications:
 Serial.begin(57600);
  //Set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 //Print a message to the LCD.
 lcd.print("Angle");
}
void loop() {
 time = millis();
 Serial.print(time); //prints time since program started
  Serial.print("\t");
 // print the sensor values:
 xdata = analogRead(xpin);
 Serial.print(xdata);
 // print a tab between values:
 Serial.print("\t");
 ydata = analogRead(ypin);
 Serial.print(ydata);
 // print a tab between values:
 Serial.print("\t");
 Serial.print(analogRead(zpin));
 Serial.println();
 xrad = xdata - 322;
 yrad = ydata - 322;
 degree = (atan2(xrad, yrad) * 180/PI);
  lcd.setCursor(0,7);
 //Print Current Time
 lcd.print(degree);
 // delay before next reading:
 delay(3);
}

No comments:

Post a Comment