Skip to content
Permalink
Browse files

Updates to PCB and code for shock sensors

  • Loading branch information...
dpoulson committed Oct 3, 2019
1 parent d269aae commit 229c015a6ce8ac4f9a7b8b444532141735f67e45
@@ -26,20 +26,25 @@ int sensitivity = 11000;
int eeAddress = 0;
bool inCalibration = false;

#define LED_PIN A0
#define LED_COUNT 6
#define CALIBRATION_PIN 16
#define CELEBRATION 10
#define encoder0PinA 2
#define encoder0PinB 0
#define SENSE_SCALE 100 // How much sensitivity will change per click
#define SENSE_MIN 5000
#define SENSE_MAX 13000
#define MAG_MID 5000
#define CELEBRATION 10
#define LED_PIN 3 // Neopixel string data in pin
#define LED_COUNT 8 // Number of neopixels
#define CALIBRATION_PIN 16 // Input pin for push button on rotary encoder
#define encoder0PinA 2 // Input for one direction of rotary encoder
#define encoder0PinB 0 // Input pin for the other direction of the rotary encoder
#define SENSE_SCALE 100 // How much sensitivity will change per click
#define SENSE_MIN 5000 // Minimum sensitivity
#define SENSE_MAX 13000 // Maximum sensitivity
#define MAG_MID 5000 // Magnitude mid point
#define SHOCK_INTERVAL 500 // How often the shock peak will decrease
#define SHOCK_DECREASE 1000 // How much the shock peak will decrease with each SHOCK_INTERVAL

MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int32_t shock_peak;
int32_t shock_show;
long last_peak;

WiFiUDP Udp;
ESP8266WiFiMulti WiFiMulti;
@@ -95,6 +100,11 @@ void setup() {
Serial.print("Address: ");
Serial.println(address);

strip.begin();
strip.setBrightness(70);
strip.show();


// Check for a stored value for sensitivity, and read it. Or use the default if it isn't present
int s;
Serial.print("EEPROM Stored sensitivity: ");
@@ -118,17 +128,24 @@ void setup() {

int timeout = 0;
while (WiFiMulti.run() != WL_CONNECTED) {
delay(1000);
delay(100);
Serial.print("Connecting..");
pulseLights();
timeout ++;
if (timeout > 30) {
break;
}
}
if ((WiFiMulti.run() == WL_CONNECTED)) {
Serial.println("Connected!");
startupLights();
strip.fill(strip.Color(0, 0, 0),0,LED_COUNT);
strip.show();
} else {
Serial.println("TIMEOUT!");
flashLights(10, 100);
strip.fill(strip.Color(0, 0, 0),0,LED_COUNT);
strip.show();
}
Udp.begin(localPort);

@@ -152,9 +169,7 @@ void setup() {
});
ArduinoOTA.begin();

strip.begin();
strip.show();


// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
@@ -165,26 +180,10 @@ void setup() {
accelgyro.initialize();
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
delay(500);

Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
Serial.print("\n");

accelgyro.setXAccelOffset(1250);
accelgyro.setYAccelOffset(500);
accelgyro.setZAccelOffset(-16000);

Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
Serial.print("\n");

}

@@ -193,35 +192,35 @@ void loop() {

if ( debouncer.fell() ) { // Call code if button transitions from HIGH to LOW
inCalibration = !inCalibration;
startupLights();
Serial.println("Toggle calibration mode");
EEPROM.put(eeAddress, sensitivity);
EEPROM.commit();
}

if (millis() > last_peak + SHOCK_INTERVAL) {
shock_peak = shock_peak - SHOCK_DECREASE;
shock_leds();
last_peak = millis();
}

if (!inCalibration) {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
long int mag = (sqrt((ax * ax)/10 + (ay * ay)/10 + (az * az)/10)) - MAG_MID;
if (mag > shock_peak) {
shock_peak = mag;
last_peak = millis();
int lit_leds = mag/((sensitivity)/LED_COUNT);
shock_leds();
}

Serial.print("Magnitude: ");
Serial.print(mag);

if (mag < 0 ) {
mag = mag * -1;
}
int lit_leds = mag/((sensitivity)/LED_COUNT);
for (int i; i < LED_COUNT; i++) {
uint32_t colour = led_green; // Default to LED being green
if (i > LED_COUNT/2) { // If it is over half way, make the LED orange
colour = led_orange;
}
if (i = LED_COUNT - 1) { // If its the final LED then it should be red
colour = led_red;
}
if (i < lit_leds) {
strip.setPixelColor(i, colour);
} else {
strip.setPixelColor(i, led_off);
}
}

if (mag > sensitivity) {
Serial.println("SHOCK!!!!!!!!");
Serial.print("Magnitude: ");
@@ -241,10 +240,8 @@ void loop() {
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(" LEDS: ");
Serial.println(lit_leds);
strip.show();
delay(100);
Serial.print(shock_peak); Serial.print("\t");
Serial.println(shock_show);
} else {
// Calculate the number of LEDs to light
int lit_leds = (sensitivity - SENSE_MIN)/((SENSE_MAX-SENSE_MIN)/LED_COUNT);
@@ -259,7 +256,7 @@ void loop() {

void calibrate_leds(int lit_leds) {
// Light up 'lit_leds' to show calibration level
for (int i; i < LED_COUNT; i++) {
for (int i = 0; i < LED_COUNT; i++) {
if (i < lit_leds) {
strip.setPixelColor(i, led_blue);
} else {
@@ -269,6 +266,26 @@ void calibrate_leds(int lit_leds) {
strip.show();
}

void shock_leds() {
int lit_leds = shock_peak/((sensitivity)/LED_COUNT);
int l;
for (l = 0 ; l < LED_COUNT; l++) {
uint32_t colour = led_green; // Default to LED being green
if (l > (LED_COUNT/2) - 1) { // If it is over half way, make the LED orange
colour = led_orange;
}
if (l == LED_COUNT - 1) { // If its the final LED then it should be red
colour = led_red;
}
if (l < lit_leds) {
strip.setPixelColor(l, colour);
} else {
strip.setPixelColor(l, led_off);
}
}
strip.show();
}

void changePos(int x) {
sensitivity = (sensitivity+(x*SENSE_SCALE));
if (sensitivity > SENSE_MAX)
@@ -312,3 +329,58 @@ void doEncoderB() {
changePos(-1); // CCW
}
}


void flashLights(int count, int wait) {
int i;
for(i=0; i<count; i++) {
strip.fill(strip.Color(255,0,0),0,LED_COUNT);
strip.show();
delay(wait);
strip.fill(strip.Color(0,0,0),0,LED_COUNT);
strip.show();
delay(wait);
}
}


uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

void pulseLights() {
uint16_t i, j;
for(i=0; i<50; i++) {
strip.fill(strip.Color(i*5,0,0), 0, LED_COUNT);
strip.show();
delay(5);
}
for(j=50; j>0; j--) {
strip.fill(strip.Color(j*5,0,0), 0, LED_COUNT);
strip.show();
delay(5);
}
}

void startupLights() {
uint16_t i, j;

for(j=0; j<256; j++) {
for(i=0; i<9; i++) {
strip.fill(Wheel((i+j) & 255),0,LED_COUNT);
}
strip.show();
delay(5);
}
strip.clear();
strip.show();
}

0 comments on commit 229c015

Please sign in to comment.
You can’t perform that action at this time.