5.Code & Files¶
Code¶
#include <Adafruit_LSM303_Accel.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
int led_pin9 = 9;
int led_pin10 = 10;
int led_pin11 = 11;
bool encendido_10, encendido_11 = false;
unsigned long previousMillis_x = 0;
const long interval_x = 500;
unsigned long previousMillis_y = 0;
const long interval_y = 500;
unsigned long previousMillis_z = 0;
const long interval_z = 500;
unsigned long previousMillis_bajo = 0;
const long interval_bajo = 1000;
float inicial_x = 0;
float inicial_y = 0;
float inicial_z = 0;
bool primeravez = false;
void displaySensorDetails(void) {
sensor_t sensor;
accel.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print("Sensor: ");
Serial.println(sensor.name);
Serial.print("Driver Ver: ");
Serial.println(sensor.version);
Serial.print("Unique ID: ");
Serial.println(sensor.sensor_id);
Serial.print("Max Value: ");
Serial.print(sensor.max_value);
Serial.println(" m/s^2");
Serial.print("Min Value: ");
Serial.print(sensor.min_value);
Serial.println(" m/s^2");
Serial.print("Resolution: ");
Serial.print(sensor.resolution);
Serial.println(" m/s^2");
Serial.println("------------------------------------");
Serial.println("");
//delay(500);
}
void setup(void) {
pinMode(led_pin9, OUTPUT);
pinMode(led_pin10, OUTPUT);
pinMode(led_pin11, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A5, OUTPUT);
//Pin para el cable suelto
pinMode(A4, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Accelerometer Test");
Serial.println("");
/* Initialise the sensor */
if (!accel.begin()) {
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while (1)
;
}
/* Display some basic information on this sensor */
displaySensorDetails();
accel.setRange(LSM303_RANGE_4G);
Serial.print("Range set to: ");
lsm303_accel_range_t new_range = accel.getRange();
switch (new_range) {
case LSM303_RANGE_2G:
Serial.println("+- 2G");
break;
case LSM303_RANGE_4G:
Serial.println("+- 4G");
break;
case LSM303_RANGE_8G:
Serial.println("+- 8G");
break;
case LSM303_RANGE_16G:
Serial.println("+- 16G");
break;
}
accel.setMode(LSM303_MODE_NORMAL);
Serial.print("Mode set to: ");
lsm303_accel_mode_t new_mode = accel.getMode();
switch (new_mode) {
case LSM303_MODE_NORMAL:
Serial.println("Normal");
break;
case LSM303_MODE_LOW_POWER:
Serial.println("Low Power");
break;
case LSM303_MODE_HIGH_RESOLUTION:
Serial.println("High Resolution");
break;
}
}
void loop(void) {
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&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");
//Si el pin A4 esta conectado a tierra, solo prende la parte de abajo
if (digitalRead(A4) == 0) {
digitalWrite(led_pin9, HIGH);
}
//De lo contrario, funciona normal
else {
if (!primeravez) {
inicial_x = event.acceleration.x;
inicial_y = event.acceleration.y;
inicial_z = event.acceleration.z;
// if (inicial_x < 0) {
// incial_x = inicial_x * -1;
//}
primeravez = true;
}
/* Delay before the next sample */
delay(100);
unsigned long currentMillis_x = millis();
unsigned long currentMillis_y = millis();
unsigned long currentMillis_z = millis();
unsigned long currentMillis_bajo = millis();
if (event.acceleration.x < 0 && event.acceleration.y < 0 && event.acceleration.z < 0) {
//no hago nada
digitalWrite(led_pin9, LOW);
digitalWrite(led_pin10, LOW);
digitalWrite(led_pin11, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(A5, LOW);
} else {
//if (event.acceleration.x > 5) {
int diferencia_x = inicial_x - event.acceleration.x;
diferencia_x = abs(diferencia_x);
if (diferencia_x > 5) {
digitalWrite(A5, HIGH); //turn the Led on
digitalWrite(A3, HIGH); //turn the Led on
if (currentMillis_x - previousMillis_x >= interval_x) {
// save the last time you blinked the LED
previousMillis_x = currentMillis_x;
//delay(500);
digitalWrite(A5, LOW); //turn the Led off
digitalWrite(A3, LOW); //turn the Led on
}
}
int diferencia_y = inicial_y - event.acceleration.y;
diferencia_y = abs(diferencia_y);
if ((diferencia_y > 5) && (!encendido_10)) {
//digitalWrite(led_pin10, HIGH); //turn the Led on
for (int i = 0; i < 255; i++) {
analogWrite(led_pin10, i); //turn the Led on
delay(2);
}
encendido_10 = true;
digitalWrite(A2, HIGH); //turn the Led on
}
if ((currentMillis_y - previousMillis_y >= interval_y) && (encendido_10)) {
// save the last time you blinked the LED
previousMillis_y = currentMillis_y;
//delay(500);
//digitalWrite(led_pin10, LOW); //turn the Led off
for (int i = 255; i > 0; i--) {
analogWrite(led_pin10, i); //turn the Led on
delay(2);
}
encendido_10 = false;
digitalWrite(A2, LOW); //turn the Led on
}
int diferencia_z = inicial_z - event.acceleration.z;
diferencia_z = abs(diferencia_z);
if ((diferencia_z > 5) && (!encendido_11)) {
//digitalWrite(led_pin11, HIGH); //turn the Led on
for (int i = 0; i < 255; i++) {
analogWrite(led_pin11, i); //turn the Led on
delay(2);
}
encendido_11 = true;
}
if ((currentMillis_z - previousMillis_z >= interval_z) && (encendido_11)) {
// save the last time you blinked the LED
previousMillis_z = currentMillis_z;
//delay(500);
//digitalWrite(led_pin11, LOW); //turn the Led off
for (int i = 255; i > 0; i--) {
analogWrite(led_pin11, i); //turn the Led on
delay(2);
}
encendido_11 = false;
}
int _actual_x = abs(event.acceleration.x);
int _actual_y = abs(event.acceleration.y);
int _actual_z = abs(event.acceleration.z);
int diferencia_bajo = (_actual_x + _actual_y + _actual_z) / 3;
Serial.print("diferencia abajo ");
Serial.println(diferencia_bajo);
if (diferencia_bajo > 20) {
digitalWrite(A5, LOW);
digitalWrite(led_pin10, LOW);
digitalWrite(led_pin11, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
for (int i = 0; i < 255; i++) {
analogWrite(led_pin9, i);
delay(5);
}
delay(1500);
for (int i = 255; i > 0; i--) {
analogWrite(led_pin9, i);
delay(5);
}
//digitalWrite(A5, HIGH); //turn the Led on
//delay(2000);
digitalWrite(led_pin9, LOW); //turn the Led off
}
}
}
}
Files¶
The dxf files of the costume patterns can be found here
Last update: April 11, 2021