A little help detecting a punch to trigger a solenoid

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>

// Used for software SPI
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
// Used for hardware & software SPI
#define LIS3DH_CS 10

//software SPI

//A fist experiences significant acceleration during a good punch,
//followed by massive deceleration at the end of the punch.
//This happens within a fairly small window of time, so it's
//pretty easy to distinguish a punch from normal gesticulations.

unsigned long punchStart = 0;//variable for non-blocking punch timeframe check
const long punchInterval = 200;//timeframe of a punch in ms, from max acceleration to max deceleration, 200 is very generous
int punchAccel = 20;//the beginning of a punch in m/s^2, could be over 50m/s^2 depending on the puncher
int punchDecel = -40;//the end of a punch in m/s^2, could be less than -100m/s^2 depending on the puncher

void setup(void) {
  //Test to see if accelerometer is communicating
  Serial.println("LIS3DH test!");  
    Serial.println("Couldnt start");
    while (1);
  Serial.println("LIS3DH found!");
  lis.setRange(LIS3DH_RANGE_16_G);   //+-16G range for good punch detection
  Serial.print("Range = "); Serial.print(2 << lis.getRange());  
  pinMode(8, OUTPUT); //Solenoid valve
  digitalWrite(8, LOW);

void loop() {
  sensors_event_t event; 
 //look for punch starting, at least 20 m/s^2
  if (event.acceleration.x > punchAccel){
     punchStart = millis();
  unsigned long currentMillis = millis();

  //look for punch ending, less than -40 m/s^2
  if (event.acceleration.x < punchDecel && currentMillis - punchStart < punchInterval){
      digitalWrite(8, HIGH);

This is my code, I am trying to create a sort of punch activated pneumatic cylinder. I kinda umm "borrowed" and edited this code from somewhere else and have a feeling it's terrible hahahaha.

Code is almost meaningless without reference to a schematic.
Please post one along with links to where you got the sensor. A pen and paper, schematic is fine, a fritzing physical layout diagram is not.

Are you really using the Adafruit sensor? https://www.adafruit.com/product/2809
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>

She, Lady ADA/Adafruit always make example code that go with their hardware sensors... start there.

The web also has a bunch of projects that use various sensors within a box to sound alarms or shock or bump when "knocked" or moved. Good reference.

You will need a "driver", either made or purchased, to control the solenoid and the circuit must handle the current and provide a "flyback diode" to protect the electronics. You need to 1) identify the solenoid, 2) obtain the reference sheet, 3) find a relay (physical or solidstate) to handle the relay power; you can alternately find a circuit using a FET or MosFET on the Internet and build that.

Here are just a few examples of knock/knock:

Yep, you have some pretty obvious errors in your code. Just to get you started... here:

You have an extra bracket in your code after the while(1), so this will not work. Also, you have no condition around the while(1), so this will infinitely loop. Usually these are called in the setup when there is an initialization error. Looking at one of the Adafruit LIS3DH examples (which I'm assuming that your using - look at previous replies):

  while (!Serial) delay(10);     // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("LIS3DH test!");

  if (! lis.begin(0x18)) {   // change this to 0x19 for alternative i2c address
    Serial.println("Couldnt start");
    while (1) yield();
  Serial.println("LIS3DH found!");

Is probably similar to what you want to replace your code up to Serial.println("LIS3DH found!");... not what you have right now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.