Skip to content

Instantly share code, notes, and snippets.

@derfaq
Last active March 2, 2021 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derfaq/a501b41e2aba547194624e99122d8b1d to your computer and use it in GitHub Desktop.
Save derfaq/a501b41e2aba547194624e99122d8b1d to your computer and use it in GitHub Desktop.
NeoPixels controlled by Accelerometer - Gravity Acceleration Bounce
/*
by Der Faq
modified 1 Mar 2021
This code is in the public domain
*/
//GND - GND
//VCC - Pin 3
//SDA - Pin A4
//SCL - Pin A5
#define PIN_VCC 3
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
const int mpuAddress = 0x68; //Puede ser 0x68 o 0x69
MPU6050 mpu(mpuAddress);
int ax, ay, az;
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 8
Adafruit_NeoPixel pixels( NUMPIXELS , PIN , NEO_GRB + NEO_KHZ800 );
void setup() {
pinMode( PIN_VCC , OUTPUT );
digitalWrite( PIN_VCC , HIGH );
delay( 100 );
Serial.begin( 115200 );
Wire.begin();
mpu.initialize();
pixels.begin();
Serial.println(mpu.testConnection() ? F("IMU iniciado correctamente") : F("Error al iniciar IMU"));
}
unsigned long int currentTime;
float vel;
float pos;
void loop() {
mpu.getAcceleration(&ax, &ay, &az);
float ayf = (ay)/16384.0;
if( currentTime - millis() > 33 ) {
currentTime = millis();
vel = ayf*(0.33) + vel;
pos = vel*(0.33) + pos;
if( pos > 100.0 ) { pos = 100.0; vel = -vel*0.8; }
if( pos < -100.0 ) { pos = -100.0; vel = -vel*0.8; }
}
int posPixel = (int)((( (pos/10) + 10 )*8)/20);
if( posPixel == 8 ) posPixel = 7;
Serial.println( posPixel );
for( int i = 0 ; i < pixels.numPixels() ; i++ ) {
( i == posPixel ) ? pixels.setPixelColor( i , pixels.Color( 255 , 0 , 0 ) ) : pixels.setPixelColor( i , pixels.Color( 0 , 0 , 0 ) );
}
pixels.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment