Piezo Vibration Sensor - Decimal Output?

I have a Piezo Vibration Sensor - Small Horizontal and i have connected/wired it to my UNO as decribed in this tutorial: http://www.arduino.cc/en/Tutorial/Knock.

It works, measures and outputs data. But i would like it to output its data in decimals (ie 1.234) not just a whole number (ie 12). Using "float" adds the decimal in the output, like this 0.00 but all the numeric values after the decimal are a constant zero and never change. It's still outputting the whole number, but just with .00 on the end. How can i make the .00 be a reading aswell?

This is the code its running:

const float knockSensor = A0;
float sensorReading = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorReading = analogRead(knockSensor);

  Serial.println(sensorReading);

  delay(100);
}

Is there a way to do this.

thank you

something like ?

const float knockSensor = A0;
float sensorReading = 0;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  sensorReading = analogRead(knockSensor);
  sensorReading = sensorReading / 100.0;

  Serial.println(sensorReading, 2);  // 2 digits

  delay(100);
}

You are awesome! Just what i was looking for :slight_smile:

I added some threshold stuff back into the code, and it has increased the "sensitivity" so much.

const float knockSensor = A0;
const float threshold = 0.01;

float sensorReading = 0;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  sensorReading = analogRead(knockSensor);
  sensorReading = sensorReading / 100.0;
  
  if (sensorReading >= threshold) {
    
    Serial.println(sensorReading, 2);  // 2 digits
  }
  
  delay(75);
}

Thank you, thank you, thank you! :slight_smile:

And add some duration since last knock ...

unsigned long lastTime = 0;
float threshold = 5.00;

void setup() 
{
  Serial.begin(9600);
  Serial.println("Knocking monitor 0.1");
}

void loop() 
{
  sensorReading = analogRead(knockSensor) * 0.01;  // combined two lines. note: multiply is faster than division
  
  if (sensorReading >= threshold) 
  {
    Serial.print(millis() - lastTime);  // duration since last knock (or start)
    lastTime = millis();
    Serial.print("\t: ");
    Serial.println(sensorReading, 2);  // 2 digits
  }
  delay(75);
}

robtillaart:
And add some duration since last knock ...

unsigned long lastTime = 0;

float threshold = 5.00;

void setup()
{
  Serial.begin(9600);
  Serial.println("Knocking monitor 0.1");
}

void loop()
{
  sensorReading = analogRead(knockSensor) * 0.01;  // combined two lines. note: multiply is faster than division
 
  if (sensorReading >= threshold)
  {
    Serial.print(millis() - lastTime);  // duration since last knock (or start)
    lastTime = millis();
    Serial.print("\t: ");
    Serial.println(sensorReading, 2);  // 2 digits
  }
  delay(75);
}

That just prints "Knocking monitor 0.1" and never changes.

What is it supposed to do?

It should add a duration since last knock, maybe the threshold is too high ...

The title is just for checking the serial is working.

Oh. I'll play around with it.

One last question... At the moment its just outputting/printing lines after lines of numbers. How could i get it to only output/print the highest value received, and update it when a higher value is made?

Thank you once again :slight_smile:

just introduce a var that hold the maximum.

unsigned long lastTime = 0;
float threshold = 5.00;
float maxValue = 0.0;

void setup() 
{
  Serial.begin(9600);
  Serial.println("Knocking monitor 0.1");
}

void loop() 
{
  sensorReading = analogRead(knockSensor) * 0.01;  // combined two lines. note: multiply is faster than division
  
  if (sensorReading >= maxValue)
  {
    maxValue = sensorReading;
    Serial.print(millis() - lastTime);  // duration since last knock (or start)
    lastTime = millis();
    Serial.print("\t: ");
    Serial.println(sensorReading, 2);  // 2 digits
  }

  if (millis() - lastTime > 30000UL) // no reading in 30 seconds, reset maxValue.
  {
    maxValue = 0.0;
  }


//  if (sensorReading >= threshold) 
//  {
//    Serial.print(millis() - lastTime);  // duration since last knock (or start)
//    lastTime = millis();
//    Serial.print("\t: ");
//    Serial.println(sensorReading, 2);  // 2 digits
//  }

//  delay(75);
}