0
Offline
Newbie
Karma: 0
Posts: 41
Arduino rocks
|
 |
« on: February 08, 2013, 12:53:25 pm » |
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); // } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35500
Seattle, WA USA
|
 |
« Reply #1 on: February 08, 2013, 12:57:05 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 41
Arduino rocks
|
 |
« Reply #2 on: February 08, 2013, 02:56:28 pm » |
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); } }
|
|
|
|
|
Logged
|
|
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1408
May all of your blinks be without delay
|
 |
« Reply #4 on: February 09, 2013, 02:47:35 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 41
Arduino rocks
|
 |
« Reply #5 on: February 10, 2013, 07:49:27 pm » |
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); // } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35500
Seattle, WA USA
|
 |
« Reply #6 on: February 10, 2013, 07:51:32 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 41
Arduino rocks
|
 |
« Reply #7 on: February 10, 2013, 07:58:33 pm » |
How do I read the avg as numbers on the Serial.println.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35500
Seattle, WA USA
|
 |
« Reply #8 on: February 11, 2013, 05:21:34 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 41
Arduino rocks
|
 |
« Reply #9 on: February 11, 2013, 06:21:19 pm » |
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); // } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35500
Seattle, WA USA
|
 |
« Reply #10 on: February 11, 2013, 07:07:09 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1408
May all of your blinks be without delay
|
 |
« Reply #11 on: February 12, 2013, 01:30:29 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 313
Posts: 35500
Seattle, WA USA
|
 |
« Reply #12 on: February 12, 2013, 06:00:06 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1408
May all of your blinks be without delay
|
 |
« Reply #13 on: February 12, 2013, 06:13:26 am » |
Whoops. Mea culpa.
Apologies all round, but I will be interested to see the output.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 41
Arduino rocks
|
 |
« Reply #14 on: February 12, 2013, 05:51:08 pm » |
To UKHeliBob When I use the simple program. In the ( Serial.println(val)  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.
|
|
|
|
|
Logged
|
|
|
|
|
|