0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« on: April 29, 2010, 11:35:14 am » |
Hello everyone i've been trying to find out the frequency from a two-wire output speed sensor that generating Alternating-Current (AC) However, the code that i wrote is not working. could anyone help me please. i am very appreciate it. here's the code: #define frqPin 1 //analog pin 1 for frqpin const int sensorPin = 1; //sensor input is analog pin 1 long timeStart, timeStop; #include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() { lcd.begin(16, 2); Serial.begin(9600); Serial.println("Start measuring"); }
void loop() { while (millis() < 5000) { int val; val=analogRead(1); if (digitalRead(1) == 0) { // wait until pin goes high while (digitalRead(1) == 0) {} // get the time when input goes high timeStart = micros(); // wait until input goes low again while (digitalRead(1) == 1) { } // wait until input goes high again while (digitalRead(1) == 0) { } // take the actual time in microseconds timeStop = micros(); // the timedifference is the the time between two rising // edges of the input pin (= 1 period) timeStart = timeStop-timeStart; // normally the result is > 0, otherwise the // function micros() had an overflow (every 70 min) // the discard this measuring (or correct the value) if (timeStart > 0) { // calculate frequency // 1000000 microseconds/second, f = 1/t double f = 1000000.0/timeStart; // report the measured frequency lcd.println(f); delay(1000); lcd.clear(); delay(1000);
} } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: April 29, 2010, 11:39:28 am » |
while (millis() < 5000) Could it be that it works, but only for five seconds? the code that i wrote is not working Can you explain what the difference is between what you expect it to do, and what it actually does do is, please? That's the only commonly-agreed definition of "working" that we currently have. Maybe a reference to your speed sensor? Why do you read "val"? You don't do anything with it. You've declared these: #define frqPin 1 //analog pin 1 for frqpin const int sensorPin = 1; //sensor input is analog pin 1 so why don't you use them?
|
|
|
|
« Last Edit: April 29, 2010, 11:46:32 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #2 on: April 29, 2010, 12:19:53 pm » |
Could it be that it works, but only for five seconds?
so you are saying is that should be deleted ? Can you explain what the difference is between what you expect it to do, and what it actually does do is, please? That's the only commonly-agreed definition of "working" that we currently have. Maybe a reference to your speed sensor?
Why do you read "val"? You don't do anything with it.
by the mean of not working, it cannot show the frequency through the LCD, it is blank. The thing that i would like to do is showing the frequency through the LCD. the reference to the speed sensor, you mean a data sheet? and one more question the AC must be converted, right? if i want to use the ADC in the ATmega168 Does the code i wrote can do the ADC function?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #3 on: April 29, 2010, 12:48:20 pm » |
Had some changes Are there any useless codes or codes that i haven't use them ? #define frqPin 1 //analog pin 1 for frqpin int analogPin = 1; //sensor input is analog pin 1 int val = 0; long timeStart, timeStop; #include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() { pinMode(analogPin, INPUT); pinMode(frqPin, INPUT); lcd.begin(16, 2); Serial.begin(9600); Serial.println("Start measuring"); }
void loop() { val = analogRead(1); { if (digitalRead(1) == 0) { // wait until pin goes high while (digitalRead(1) == 0) {} // get the time when input goes high timeStart = micros(); // wait until input goes low again while (digitalRead(1) == 1) { } // wait until input goes high again while (digitalRead(1) == 0) { } // take the actual time in microseconds timeStop = micros(); // the timedifference is the the time between two rising // edges of the input pin (= 1 period) timeStart = timeStop-timeStart; // normally the result is > 0, otherwise the // function micros() had an overflow (every 70 min) // the discard this measuring (or correct the value) if (timeStart > 0) { // calculate frequency // 1000000 microseconds/second, f = 1/t double f = 1000000.0/timeStart; // report the measured frequency lcd.println(f); delay(1000); lcd.clear(); delay(1000);
} } } }
|
|
|
|
« Last Edit: April 29, 2010, 12:50:34 pm by max88poon »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: April 29, 2010, 01:14:04 pm » |
pinMode(analogPin, INPUT); Wrong. val = analogRead(1); Unused. while (digitalRead(1) == 0) Why not use the defined constant for the pin? Put some serial debug prints - you've got the hardware, help us to help you.
|
|
|
|
« Last Edit: April 29, 2010, 01:15:06 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #5 on: April 29, 2010, 01:16:28 pm » |
by the mean of not working, it cannot show the frequency through the LCD, it is blank. In a simple sketch, write "Yo stupid!" to the LCD. Does that appear on the LCD? If not, you have one problem. If it does, you have a different problem. It would be nice to know which problem you are having. val = analogRead(1); What is this for? You don't use val anywhere. if (digitalRead(1) == 0) { // wait until pin goes high while (digitalRead(1) == 0) {} // get the time when input goes high timeStart = micros(); // wait until input goes low again while (digitalRead(1) == 1) { } // wait until input goes high again while (digitalRead(1) == 0) { }
Looks to me like you are trying to re-invent the pulseIn function. Why not just use pulseIn? We know that function works.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #6 on: April 29, 2010, 01:24:29 pm » |
Wrong it doesn't need to be set a pin mode? Unused If i would like to use the ADC, what the code is exactly ? Why not use the defined constant for the pin?
so it should be : while (digitalRead(frqpin) == 0) right ?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #7 on: April 29, 2010, 01:31:38 pm » |
In a simple sketch, write "Yo stupid!" to the LCD. Does that appear on the LCD? If not, you have one problem. If it does, you have a different problem. It would be nice to know which problem you are having. The LCD is doing fine, it can show what i typed but it cannot show the frequency that i am trying to find What is this for? You don't use val anywhere umm, if i would like to use the ADC, what the code is exactly Looks to me like you are trying to re-invent the pulseIn function. Why not just use pulseIn? We know that function works. If using the pulseIn function, could it be like this ? if (pulseIn(1, HIGH)); { // wait until pin goes high while (digitalRead(frqPin) == 0) {} // get the time when input goes high timeStart = micros(); // wait until input goes low again while (pulseIn(1, LOW) ){ } // wait until input goes high again while (digitalRead(frqPin) == 0) { } // take the actual time in microseconds timeStop = micros(); // the timedifference is the the time between two rising // edges of the input pin (= 1 period) timeStart = timeStop-timeStart; // normally the result is > 0, otherwise the // function micros() had an overflow (every 70 min) // the discard this measuring (or correct the value) if (timeStart > 0) { // calculate frequency // 1000000 microseconds/second, f = 1/t double f = 1000000.0/timeStart; // report the measured frequency lcd.println(f); delay(1000); lcd.clear(); } }
|
|
|
|
« Last Edit: April 29, 2010, 01:32:14 pm by max88poon »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: April 29, 2010, 01:32:51 pm » |
An analogue input pin doesn't need a pinMode.
You're not using the analogue input value "val", so ignore it. It's noise in the program, and distracting.
If "frqpin" has a valid digital signal on it, yes.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #9 on: April 29, 2010, 01:41:18 pm » |
If "frqpin" has a valid digital signal on it, yes. now i am using the analog pin 1 with two function one is counting the frequency, the other one is doing the ADC for the valid digital signal, the signal is analog from the begining. the signal became digital signal after the ADC, right ? so theoretically, it is valid digital signal, isn't it ? Meanwhile, after the ADC, it starts measuring the frequency, then the freq can be shown on the LCD However, i couldn't use the ADC, how to command the microcontroller to do it?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: April 29, 2010, 01:59:31 pm » |
the signal became digital signal after the ADC, right No. The analogue and digital sections are completely separate. Pin 1 digital != pin 1 analogue Can you post your schematic, and some reference to the signal you're trying to read?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #11 on: April 30, 2010, 02:49:47 am » |
Can you post your schematic, and some reference to the signal you're trying to read? Thanks AWOL Here's the Schematic, it is a little bit of rough, forgive me http://www.flickr.com/photos/43337932@N04/4564634381/And it is the signal that i try to read. It is around 3.2kHz that shown on the CRO.  This the data sheet of speed sensor that i use, part no. is 0 261 210 104 http://www.farnell.com/datasheets/21735.pdfThanks a lot, very appreciate it.
|
|
|
|
« Last Edit: April 30, 2010, 02:52:35 am by max88poon »
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #12 on: April 30, 2010, 03:52:35 am » |
Upfront caveat: I'm not much use with analogue!
However, could you use the analogue comparator, rather than the ADC? Other than that, half-wave rectification and some squaring of the edges? (you may also need a DC offset - difficult to tell from the oscilloscope trace)
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 185
Arduino rocks
|
 |
« Reply #13 on: April 30, 2010, 05:52:16 am » |
However, could you use the analogue comparator, rather than the ADC? what is the analogue comparator ? what is the function exactly ? half-wave rectification and some squaring of the edges?
I'd tried the half-wave rectification, but i don't know how to square it it showed me like this 
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #14 on: April 30, 2010, 06:09:31 am » |
The comparator is a feature of the AVR, but I don't think is directly supported by the Arduino, so you'd have to go off piste. Have a look at the AVR datasheet. Simply put, the comparator is just that - it compares the voltages on two pins and can give an interrupt based on whether the voltage on the positive pin is rising, falling or equal (IIRC) with respect to the voltage on the negative pin. I would assume that if you can get the DC offset right, this would be an ideal solution - what does that trace look like if you DC couple it, anstead of AC? But, like I said, I'm not really an analogue person. 
|
|
|
|
« Last Edit: April 30, 2010, 06:10:26 am by GrooveFlotilla »
|
Logged
|
Per Arduino ad Astra
|
|
|
|
|