Skip to content

Instantly share code, notes, and snippets.

@wildeyes
Last active December 26, 2022 23:07
Show Gist options
  • Save wildeyes/dccb0a7bf37ec4f404fde6f727f220a5 to your computer and use it in GitHub Desktop.
Save wildeyes/dccb0a7bf37ec4f404fde6f727f220a5 to your computer and use it in GitHub Desktop.
LED corresponding to angle on ADXL345
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <Adafruit_NeoPixel.h>
#include <math.h>
// adaFruit led parmeters
#define PIN 0
#define NUM_LEDS 8
#define ON_LEDS_COUNT 2
#define DELAYVAL 100 // optimal against too hard flickering
const int ZERO_NUM_LEDS = NUM_LEDS - ON_LEDS_COUNT;
Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
sensors_event_t event;
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); // Assign a unique ID to this sensor at the same time
void loop(void)
{
accel.getEvent(&event);
float y = event.orientation.y;
float x = roundf(event.orientation.x);
int deg = abs(atan2(y, x)) * 180 / M_PI;
int gyroOrientationOffsetX = -ZERO_NUM_LEDS;
int pos = map(deg, 0, 180, 0, ZERO_NUM_LEDS);
Serial.printf("[deg: %d][pos: %d]\r\n", deg, pos);
strip.clear();
int pixelIndex = abs(pos + gyroOrientationOffsetX);
for(int i = 0; i < ON_LEDS_COUNT; i++) {
strip.setPixelColor(pixelIndex + i, strip.Color(0, 150, 0));
}
strip.show();
delay(DELAYVAL);
}
void setup(void)
{
Serial.begin(9600);
Serial.println("Accelerometer Test");
Serial.println("");
strip.begin();
if (!accel.begin())
{
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while (1)
;
}
accel.setRange(ADXL345_RANGE_16_G);
Serial.println("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment