Pages: [1] 2   Go Down
Author Topic: Led on Led send  (Read 1267 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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





Code:
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
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.




Code:
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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 = http://diy.viktak.com/2012/07/washing...







Op Amp setup
http://www.instructables.com/files/deriv/FAH/K0W2/FOHTR2JS/FAHK0W2FOHTR2JS.MEDIUM.jpg
« Last Edit: February 09, 2013, 01:40:27 pm by Tricky12 » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4078
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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





Code:
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
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.....



Code:
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
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 109
Posts: 4078
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As Paul suggests, keep it simple to begin with.  Try this
Code:
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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Faraday Member
**
Karma: 109
Posts: 4078
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whoops.
Mea culpa.

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To  UKHeliBob

When I use the simple program.  In the ( Serial.println(val)smiley-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. 
Logged

Pages: [1] 2   Go Up
Jump to: