John Loeffler
Published © GPL3+

ShockBIT

Put it on, Pull the Tab, Feel it vibrate/electrically pulse before you touch your face.

AdvancedWork in progress3 hours2,050

Things used in this project

Hardware components

ATtiny85
Microchip ATtiny85
×1
LIS2DH12TR
×1
Capacitor Kit, 0402
Capacitor Kit, 0402
×1

Software apps and online services

Arduino IDE
Arduino IDE
Microchip Studio
Microchip Studio

Hand tools and fabrication machines

Solder Paste, Silver Bearing
Solder Paste, Silver Bearing
Hot Air Station, Industrial
Hot Air Station, Industrial
Microchip Atmel Ice - Programmer

Story

Read more

Custom parts and enclosures

Drawings

Silicon Band

Gerber Files

Gerber files optimized for OSHPARK

CAD Files

Solid Edge Cad Files

Schematics

ShockBit - Rev3

Dev Board of Shockbit

Code

Shockbit - Interrups Working

Arduino
Test code
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MMA7660FC
// This code is designed to work with the MMA7660FC_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Accelorometer?sku=MMA7660FC_I2CS#tabs-0-product_tabset-2

#include <Wire.h>
#define Addr 0x4C     // MMA7660FC I2C address is 0x4C(76)


 //--------- --------   --------   --------  ACCELEROMETER  INTTERUPT CODE --------   --------   --------   --------  
int Accl_int = 3;
volatile byte state = LOW;
int Accel_Interrupt = 0;
 //--------- --------   --------   --------  --------- --------   --------   ----------------   --------   --------   --------  
 
 
 //--------- --------   --------   --------  Button  INTTERUPT CODE --------   --------   --------   --------  
int Button_int = 2;

int Button_Interrupt = 0;
 //--------- --------   --------   --------  --------- --------   --------   ----------------   --------   --------   --------  

int Accel_Thresh = -10;


const int numReadings = 10;
int readings_x[numReadings];      // the readings from the analog input
int readIndex_x = 0;              // the index of the current reading
float total_x = 0.00;                  // the running total
float average_x = 0.00;                // the average

int readings_y[numReadings];      // the readings from the analog input
int readIndex_y = 0;              // the index of the current reading
float total_y = 0.00;                  // the running total
float average_y = 0.00;                // the average

int readings_z[numReadings];      // the readings from the analog input
int readIndex_z = 0;              // the index of the current reading
float total_z = 0.00;                  // the running total
float average_z = 0.00;                // the average


int up_lim = 35;
int low_lim = -35;

int RED_Led = 9;          //High Brightness led
int green_Led = 13;       //indicator led Brightness led
int Vibration_Motor = 10;       //indicator led Brightness led

void setup() 
{

pinMode(green_Led, OUTPUT);
pinMode(RED_Led, OUTPUT);
pinMode(Vibration_Motor, OUTPUT);

  
 //--------- --------   --------   --------  ACCELEROMETER  INTTERUPT CODE --------   --------   --------   --------  
pinMode(Accl_int, INPUT);

pinMode(Accl_int, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Accl_int), blink, FALLING);
 //--------- --------   --------   --------  ACCELEROMETER  INTTERUPT CODE --------   --------   --------   --------  

 //--------- --------   --------   --------  Button  INTTERUPT CODE --------   --------   --------   --------  
pinMode(Button_int, INPUT);
pinMode(Button_int, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Button_int), button, FALLING);
 //--------- --------   --------   --------  --------- --------   --------   ----------------   --------   --------   --------  




 
 for (int thisReading_x = 0; thisReading_x < numReadings; thisReading_x++) 
    {
      readings_x[thisReading_x] = 0;
    }
   
 for (int thisReading_y = 0; thisReading_y < numReadings; thisReading_y++) 
    {
      readings_y[thisReading_y] = 0;
    }
 
 for (int thisReading_z = 0; thisReading_z < numReadings; thisReading_z++) 
    {
      readings_z[thisReading_z] = 0;
    }
    
  Wire.begin();           // Initialise I2C communication as MASTER

  Serial.begin(38400);   // Initialise Serial Communication, set baud rate = 9600




  
 
 //--------- --------   --------   --------  ACCELEROMETER  INTTERUPT CODE --------   --------   --------   --------  
 
  Wire.beginTransmission(0x4c);     // Setting up Interrupts
  Wire.write(0x06);
  Wire.write(0b00000011);                 // Position Change (0x4E (0b10011100) - TAP Detection)
  Wire.endTransmission();
    
  delay(2);
    
  Wire.beginTransmission(0x4c);     // Setting up Tap Threshold
  Wire.write(0x09);
  Wire.write(0b0000001);
  Wire.endTransmission();

  delay(2);

  Wire.beginTransmission(0x4c);     // Setting up MODE to Active with Interrups
  Wire.write(0x07);
  Wire.write(0b1100001);
  Wire.endTransmission();
  
 //--------- --------   --------   --------  ACCELEROMETER  INTTERUPT CODE --------   --------   --------   --------  




 
  Wire.beginTransmission(Addr);   // Start I2C Transmission
  Wire.write(0x07);               // Select mode register 
  Wire.write(0x01);               // Select active mode ******MUST Upload 0x00 then umpload 0x01 when changing Samples
  Wire.endTransmission();         // Stop I2C Transmission
   
  delay(100);
  
  Wire.beginTransmission(Addr);   // Start I2C Transmission
  Wire.write(0x08);               // Select sample rate register register
  Wire.write(0X02);               // 120 sample per second
  Wire.endTransmission();         // Stop I2C Transmission
  
  delay(100);  
  
  digitalWrite(green_Led, LOW);
  digitalWrite(RED_Led, LOW);
  digitalWrite(Vibration_Motor, LOW);


}

void loop()
{
  
 //------------------------------------ Accelerometr Data Aquire ----------------------------------------  


    
  int Pot_read = analogRead(A0);
  int Pot_Mapped =  map(Pot_read, 0, 1023, 0, 100);
  
  unsigned int data[3];
  
  Wire.beginTransmission(Addr);   // Start I2C Transmission
  Wire.write(0x00);               // Select Data Register
  Wire.endTransmission();         // Stop I2C Transmission
  Wire.requestFrom(Addr, 3);      // Request 3 bytes of data
 
  if(Wire.available() == 3)  // Read the three bytes // xAccl, yAccl, zAccl
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
  }  




 //------------------------------------ +- Orientation ----------------------------------------  
    int xAccl = data[0] & 0x3F;  
      if(xAccl > 31)
        { xAccl -= 64; }
   
    int yAccl = data[1] & 0x3F;  
      if(yAccl > 31)
        { yAccl -= 64; }
        
    int zAccl = data[2] & 0x3F;  
      if(zAccl > 31)
        { zAccl -= 64; }

 //------------------------------------ SMOOTHING X----------------------------------------       
  total_x = total_x - readings_x[readIndex_x];    // subtract the last reading:
  readings_x[readIndex_x] = xAccl;                // read from the sensor:
  total_x = total_x + readings_x[readIndex_x];    // add the reading to the total:
  readIndex_x = readIndex_x + 1;                  // advance to the next position in the array:
 
  if (readIndex_x >= numReadings)                 // if we're at the end of the array...
    { readIndex_x = 0; }                          // ...wrap around to the beginning:
    
  average_x = total_x / numReadings;            // calculate the average:
  
 //------------------------------------ SMOOTHING Y----------------------------------------   
  total_y = total_y - readings_y[readIndex_y];    // subtract the last reading:
  readings_y[readIndex_y] = yAccl;                // read from the sensor:
  total_y = total_y + readings_y[readIndex_y];    // add the reading to the total:
  readIndex_y = readIndex_y + 1;                  // advance to the next position in the array:
  
  if (readIndex_y >= numReadings)                 // if we're at the end of the array...
    {  readIndex_y = 0; }                         // ...wrap around to the beginning:
                                 
  average_y = total_y / numReadings;              // calculate the average:
  
 //------------------------------------ SMOOTHING Z----------------------------------------   
  total_z = total_z - readings_z[readIndex_z];        // subtract the last reading:
  readings_z[readIndex_z] = zAccl;                // read from the sensor:
  total_z = total_z + readings_z[readIndex_z];        // add the reading to the total:
  readIndex_z = readIndex_z + 1;                  // advance to the next position in the array:
 
  if (readIndex_z >= numReadings)               // if we're at the end of the array...
    { readIndex_z = 0; }                        // ...wrap around to the beginning:

  average_z = total_z  / numReadings;            // calculate the average:
  
 //----------------------------------------------------------------------------------------------- 
 // if (Serial.available() > 0) 
 // {
      Serial.print(" ");
      Serial.print(Pot_Mapped); 
      Serial.print(" ");
      Serial.print(low_lim); 
      Serial.print(" ");
      Serial.print(up_lim); 
      Serial.print(" ");
      Serial.print(average_x);                  // send it to the computer as ASCII digits
      Serial.print(" "); 
      Serial.print(average_y);                  // send it to the computer as ASCII digits
      Serial.print(" ");
      Serial.print(average_z);                  // send it to the computer as ASCII digits
      Serial.print(" ");
      Serial.print(Button_Interrupt); 
      Serial.print(" ");
      Serial.println(Accel_Interrupt); 
//  }

  

if (average_x <= Accel_Thresh)              
    {
      digitalWrite(RED_Led, HIGH);
      digitalWrite(Vibration_Motor, HIGH);
    } 
else
    {
      digitalWrite(RED_Led, LOW);
      digitalWrite(Vibration_Motor, LOW);    
    }
 //--------- --------   --------   --------  ACCELEROMETER  INTTERUPT CODE --------   --------   --------   --------  
  Accel_Interrupt = 0; 
  Button_Interrupt = 0; 
 //--------- --------   --------   --------   ACCELEROMETER INTTERUPT CODE --------   --------   --------   --------  
  
  delay(1); 
}

//--------------------------------------------------------------------------------------
void blink() 
{
  for (int i = 0; i <= 1; i++) 
  {
    digitalWrite(green_Led, HIGH);
     delay(2);
    digitalWrite(green_Led, LOW);
     delay(5);
  }
Accel_Interrupt = 20;
}

void button() 
{
    for (int i = 0; i <= 1; i++) 
  {
    digitalWrite(RED_Led, HIGH);
    digitalWrite(Vibration_Motor, HIGH);
     delay(100);
    digitalWrite(RED_Led, LOW);
    digitalWrite(Vibration_Motor, LOW); 
      delay(100);
  }
Button_Interrupt = -40;
}

Credits

John Loeffler

John Loeffler

1 project • 6 followers
I have a Master degree in Mechanical Engineering. I currently run a 3D printing and Prototyping Lab

Comments