Led on Led send

Hi

I have this dryer that I attach A Piezo sensor, Op Amp and some resistor. From the OP Amp to pin 2 of the analog in on my arduino. So when the dryer is on and running, the Serial.printIn Reads tap.

What I need help is with this = When I turn on the Arduino, I want (led1) to turn on for 2 sec. then go off. Than that
same (Led1) to blink when dryer is on and running, if dryer is off, no blink. (Led2) I want to read the dryer state, from
minute to minute. If the dryer in the minute is running the (Led2) will be off. If the dryer is off in that minute to minute,
then I want the (Led2) to blink of one sec. and then go off.

Here is the code. Its not working all right

int sensor = 2; // Analog in
int val =0; // Current reading for analog pin
int avg; // Running average of the wave amplitude
int MIDPOINT = 520; // Base reading
const int led1 =  13;
const int led2 =  8;


void setup() {
Serial.begin(9600);
avg = MIDPOINT; // set average at midpoint
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
val = analogRead(sensor);


if (val > MIDPOINT) {
val = val - MIDPOINT;
} else {
val = MIDPOINT - val;
}


avg = (avg * 0.5) + (val * 0.5);

if (avg > 70) {
// vibration detected!
Serial.println("TAP");
digitalWrite(led1, HIGH);
delay(1000); 
digitalWrite(led1, Low);  

delay(100); // 
}
}

What is up with MIDPOINT and manipulating val? What is up with the averaging code?

The code code does something. You have not described that.
You want it to do something more. You haven't described what it doesn't do.

The delay() is going to kill you. Read, understand, and embrace the code in Blink Without Delay.

I am trying to use the Piezo sensor to read the vibration of the dryer being on and running.

I want led1 to tell you that the piezo sensor is working and I want led2 to send A 0 or 1 telling you the dryer is off, after it's
full cycle of running.

here is new code that led1 works good.

int sensor = 2; // Analog in
int val =0; // Current reading for analog pin
int avg; // Running average of the wave amplitude
int MIDPOINT = 520; // Base reading
int led1 = 13;
int led2 = 8;


void setup() {
Serial.begin(9600);
avg = MIDPOINT; // set average at midpoint
}

void loop() {
val = analogRead(sensor);

// Compute wave amplittue
if (val > MIDPOINT) {
val = val - MIDPOINT;
} else {
val = MIDPOINT - val;
}


avg = (avg * 0.5) + (val * 0.5);

if (avg > 70) {
// vibration detected!
Serial.println("TAP");
digitalWrite(led1, HIGH); 
delay(1);
digitalWrite(led1, LOW); 
delay(100);
}
}

I have A Piezo sensor, Op Amp and some resistor and going to pin 2, to the Arduino.

Here is what I am shooting for = Washing Machine Monitor – Viktor’s DIY Blog...

Op Amp setup
http://www.instructables.com/files/deriv/FAH/K0W2/FOHTR2JS/FAHK0W2FOHTR2JS.MEDIUM.jpg

Your link does not work but this one does http://diy.viktak.com/2012/07/washing-machine-monitor.html

Is there A way in my code that I can read the input of the (avg), and put (if (avg < 22) digitalWrite(led2, HIGH);

int sensor = 2; // Analog in
int val =0; // Current reading for analog pin
int avg; // Running average of the wave amplitude
int MIDPOINT = 520; // Base reading
const int led1 =  13;
const int led2 =  8;


void setup() {
Serial.begin(9600);
avg = MIDPOINT; // set average at midpoint
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
val = analogRead(sensor);


if (val > MIDPOINT) {
val = val - MIDPOINT;
} else {
val = MIDPOINT - val;
}


avg = (avg * 0.5) + (val * 0.5);

if (avg > 70) {
// vibration detected!
Serial.println("TAP");
digitalWrite(led1, HIGH);
delay(1000); 
digitalWrite(led1, Low);  

delay(100); // 
}
}

Is there A way in my code that I can read the input of the (avg), and put (if (avg < 22) digitalWrite(led2, HIGH);

Yes.

What is the purpose of Serial.begin() in setup() if you have no Serial.print() statements anywhere in the code? More importantly, why don't you have them?

How do I read the avg as numbers on the Serial.println.

How do I read the avg as numbers on the Serial.println.

I have no clue what you are talking about. The Serial.println() statements prints values, like avg, as simple text. If you can't read that, I can't help you.

Unless you post code, I can't help you.

This is all I want from you people. Piezo sensor, Op Amp and some resistor and going to (analog in) pin 2, to the Arduino. Take pin2 input and make it running this program, but when the Piezo sensor is not moving then have one led light up and then turn off. That is what I want, that I don't know how to program. If you can help give me good input, not make fun of me, you are here to help, I think.....

int sensor = 2; // Analog in
int val =0; // Current reading for analog pin
int avg; // Running average of the wave amplitude
int MIDPOINT = 520; // Base reading
const int led1 =  13;



void setup() {
Serial.begin(9600);
avg = MIDPOINT; // set average at midpoint
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
val = analogRead(sensor);


if (val > MIDPOINT) {
val = val - MIDPOINT;
} else {
val = MIDPOINT - val;
}


avg = (avg * 0.5) + (val * 0.5);

if (avg > 70) {

Serial.println("TAP");


delay(100); // 
}
}

Why don't you start simple, then, and learn to program.

You have not yet explained what this shifting and averaging stuff is about. Either the sensor senses vibration, or it does not.

Delete all that stuff, and just Serial.print() a prefix and the value read from the sensor. Does that number make any sense? Until you KNOW what the sensor is reporting, you can't begin to make use of the value.

As Paul suggests, keep it simple to begin with. Try this

int sensor = 2; // Analog in
int val =0; // Current reading for analog pin

void setup() {
  Serial.begin(9600);
  pinMode(sensor, INPUT);
}

void loop() {
  val = analogRead(sensor);
  Serial.println(val);
  delay(100);
}

It is basically your program with the stuff that is irrelevant at the moment taken out and a pinMode() command for the sensor, which was missing, added in. What sort of output do you get under different conditions ?

and a pinMode() command for the sensor, which was missing, added in.

What use is a function that affects digital pins only, to an input-only analog pin? The pinMode() statement was correctly omitted from OP's code.

Whoops.
Mea culpa.

Apologies all round, but I will be interested to see the output.

To UKHeliBob

When I use the simple program. In the ( Serial.println(val):wink: the val comes up as 264. When I tap on the Piezo sensor
the val 873. So how do I get A constant # for the val? So I can make the led turn on or off.

PaulS:
What use is a function that affects digital pins only, to an input-only analog pin? The pinMode() statement was correctly omitted from OP's code.

I agree it's be redundant to set the mode to INPUT since that is the default, but if you mean that the analog pins do not support digital output then I disagree.

You won't get a constant value. The sensor is responding to you tapping it but the value will not stay at a high value because the tap will finish and the value will drop back to a fairly steady lower one as you have seen. If you need to perform an action you need to get your program to notice the change and act on it.
Something like this

start loop
  read sensor
  if sensor value greater than threshold
    turn on the LED
  end of if
  else
    compare sensor value now with previous sensor value
    if it was it low last time and has been for some time
      turn off the LED
    end of if
    remember the current sensor value for the next check
  end of else
end of loop - go back and do it again

You will need to determine on the value of the threshold based on your experiments

I tried this code today on my dryer. The (Serial.println(val):wink: = 264, with the dryer off.
When the dryer was on and running, (Serial.println(val):wink: = 266. How do I go about making the Piezo sensor,
be more sensitive to the vibrations of the dryer, when it is on?

int sensor = 2; // Analog in
int val =0; // Current reading for analog pin

void setup() {
  Serial.begin(9600);
  pinMode(sensor, INPUT);
}

void loop() {
  val = analogRead(sensor);
  Serial.println(val);
  delay(100);
}

You could attach it to something that resonates when the motor is running?

Try moving the sensor to a different place on the dryer. The dryer has 3 planes in which it can move. Left/right, forwards/backwards and up/down and all combinations of course. Try the sensor on the top, side and front to see if you get better readings.