Skip to content

Instantly share code, notes, and snippets.

@ItsMichal
Created November 20, 2021 01:28
Show Gist options
  • Save ItsMichal/b28838b117930354e6e7a5ffd1b3e8fc to your computer and use it in GitHub Desktop.
Save ItsMichal/b28838b117930354e6e7a5ffd1b3e8fc to your computer and use it in GitHub Desktop.
Prototype Boxing Bag Code
/*
This example reads the acceleration values as relative direction and degrees,
from the LSM6DS3 sensor and prints them to the Serial Monitor or Serial Plotter.
The circuit: Arduino Nano 33 IoT only, no additional circuitry
*/
#include <Arduino_LSM6DS3.h>
#include <Adafruit_NeoPixel.h>
#include <map>
#define SPK_PIN 14
#define LED_PIN 15
#define NUM_PIXELS 5
Adafruit_NeoPixel ledStrip(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
float x, y, z;
int degreesX = 0;
int degreesY = 0;
void setup() {
Serial.begin(9600);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
ledStrip.begin();
analogWriteResolution(10);
analogWrite(A0, 0);
}
std::string cmaj_scale[] = {"G3", "E3", "C3", "E3", "G3", "C4", "E4", "D4", "C4", "E3", "F#3", "G3", "G3", "G3", "E4", "D4", "C4", "B3", "A3", "B3", "C4", "C4", "G3", "E3", "C3"};
std::map<std::string, int> noteToFreq {
{"C3" , 131},
{"C#3", 139},
{"D3", 147},
{"E3", 165},
{"F3", 175},
{"F#3", 185},
{"G3", 196},
{"A3", 220},
{"B3", 247},
{"C4", 262},
{"D4", 294},
{"E4", 330},
{"B4", 494},
};
int cmaj_pos = 0;
float vector;
float max_x = 0.0f;
int times = 0;
float color = 0;
int cooldown = 0;
float max_x2 =0, max_y=0, max_z=0;
void loop() {
ledStrip.clear();
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
}
if(abs(x) > abs(max_x2)){
max_x2 = x;
}
if(abs(y) > abs(max_y)){
max_y = y;
}
if(abs(z) > abs(max_z)){
max_z =z;
}
vector = sqrt(x * x + y * y + z * z);
if(vector < 1.2 && cooldown <= 100){
cooldown++;
// Serial.println("COOLDOWN");
}
if(cooldown > 85){
// Serial.println(vector);
if (vector > max_x ) {
max_x = vector;
times = 0;
}
if (vector < max_x ) { //inversed pattern detection (aka I'm moving back the board)
times++;
}
}
//Y goes from -4 to 4, idles under 0.5
//X and Z idle below -1, goes to -3
if (times > 5 && max_x > 4 && color < 10) { //sample some value to avoid bouncing
// Serial.print("MAX - X ");
// Serial.print(max_x2);
// Serial.print(" - Y ");
// Serial.print(max_y);
// Serial.print("- Z ");
// Serial.print(max_z);
if(abs(max_z) < 3.9){
if(max_y > 0){
Serial.println("LEFT");
}else{
Serial.println("RIGHT");
}
}else{
Serial.println("FORWARD");
}
max_x2 = 0;
max_y = 0;
max_z = 0;
cooldown =0;
color = 255;
max_x = 0.0f; //reset max X value
// Serial.println("Hit detected");
tone(SPK_PIN, noteToFreq[cmaj_scale[cmaj_pos]], 1000);
cmaj_pos+=1;
if(cmaj_pos > 24){
cmaj_pos=0;
}
}
for(int i=0; i<NUM_PIXELS; i++) {
ledStrip.setPixelColor(i, ledStrip.Color(color,0,0));
ledStrip.show();
}
if(color > 0){
color-=1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment