You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
4 years ago
|
#include <Wire.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#define SLAVE_ADDRESS 0x16
|
||
|
|
||
|
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 index = 0;
|
||
|
char data[40];
|
||
|
|
||
|
void setup() {
|
||
|
//Serial.begin(9600);
|
||
|
pinMode(xpin, INPUT);
|
||
|
pinMode(ypin, INPUT);
|
||
|
pinMode(zpin, INPUT);
|
||
|
// initialize i2c as slave
|
||
|
Wire.begin(SLAVE_ADDRESS);
|
||
|
Wire.onRequest(sendData);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
// Nothing to do at last !!!!
|
||
|
}
|
||
|
|
||
|
void sendData() {
|
||
|
read_lilypad();
|
||
|
Wire.write(data[index]);
|
||
|
index++;
|
||
|
if (index >= 40) {
|
||
|
index = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//Function that reads the data from the Lilypad sensor
|
||
|
void read_lilypad () {
|
||
|
int x_input = analogRead(xpin);
|
||
|
int y_input = analogRead(ypin);
|
||
|
int z_input = analogRead(zpin);
|
||
|
|
||
|
//zero_G is the reading we expect from the sensor when it detects
|
||
|
//no acceleration. Subtract this value from the sensor reading to
|
||
|
//get a shifted sensor reading.
|
||
|
//float x_zero_G = 515.39;
|
||
|
//float y_zero_G = 508.57;
|
||
|
//float z_zero_G = 513.19;
|
||
|
|
||
|
float x_zero_G = 401.01;
|
||
|
float y_zero_G = 407.15;
|
||
|
float z_zero_G = 474.69;
|
||
|
|
||
|
//scale is the number of units we expect the sensor reading to
|
||
|
//change when the acceleration along an axis changes by 1G.
|
||
|
//Divide the shifted sensor reading by scale to get acceleration in Gs.
|
||
|
float scale = 102.3;
|
||
|
|
||
|
//Calculate values and convert them to string
|
||
|
|
||
|
String x_value = String(((float)x_input - x_zero_G) / scale);
|
||
|
String y_value = String(((float)y_input - y_zero_G) / scale);
|
||
|
String z_value = String(((float)z_input - z_zero_G) / scale);
|
||
|
|
||
|
//Putting all values to one string
|
||
|
String lilypad = "Lilypad x: " + x_value + " y: " + y_value + " z: " + z_value;
|
||
|
|
||
|
/* Saving The string to an array to be send to master */
|
||
|
lilypad.toCharArray(data, 40);
|
||
|
|
||
|
}
|