Pages: [1]   Go Down
Author Topic: Problems with reading A/D...  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My project is a shield for the Arduino.  It is designed to monitor engine knock voltage from peizo sensor w/ conditioning circuit, determine RPM via a frequency to voltage convertor, determine when knock is present by verification against RPM, datalog on serial, and display on a LCD.

I am currently considering a s/w problem because I wrote the code. smiley-wink  I think I did a pretty good job on my code, it's structure, and comment it, but there must be a problem with it somewhere.  Any help is greatly appreciated!  Edit: The voltages do not even remotely reflect the actual voltage as measured by a meter.

Here is the code: http://www.squirrelpf.com/knockboard/code.txt

Thanks,
Frank
« Last Edit: January 16, 2009, 07:05:31 am by fkatzenb » Logged


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you might start by telling us what works and what doesn't work  ;D

Lefty
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow... nicely done Frank. LOL.  Fixed description.
« Last Edit: January 16, 2009, 01:35:46 pm by fkatzenb » Logged


Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So, what was the problem?  How did you fix it?  It's useful to know, because others will search these forum postings for help when they have the same kind of trouble.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem isn't fixed.  i just fixed my post which didn't describe what was wrong.
Logged


Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hm.  It "does not remotely reflect the voltages" the way you expect.

How about telling us more?  What's producing the voltage?  What range of voltages do you read with a meter?  Where are you sending that signal?  How are you reading that signal?  What range of readings do you get?  Is the reading high when the meter shows a high voltage?  Is the reading low when the meter shows a low voltage?  Are the readings noisy?  random?  clipped?  inverted?  not the same number you see on the meter?  ...

How to ask questions the smart way.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I figured people would give it a try.  Put to run it down, 5 volts would appear around 1 volt and it tens to fluctuates a bit.  If I change it to 4 volts, it would still be just a bit less then 1 volt.
Logged


0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any ideas?
Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 78
Retired Electronic Technologist focusing on Wireless network development.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect you have a issue of mixing byte with floating point.

byte    knock_volt;                         // this is the raw knock voltage at any instance in time

0-255 is all that knock_volt will ever contain. It will rollover to 0 when it reaches 256.


 knock_volt = analogRead(knockPin);                               // grab the sensor data for the knock sensor
  knock_volt = map(knock_volt, 0, 1023, 0, 255);                   // remap the 10-bit input to 8-bit


Okay, you even map the 10 bits down to 8.
May be better to keep the 10 bits and use INT as you lose precision.


 Serial.print(knock_volt, DEC);
    Serial.print("\t");
    

That should show a value between 0-255?



  printDouble(knock_volt * 0.019531, 3);                                                  // Knock Volts

- knock_volt is not a double
- knock_volt * 0.019531 ?

  255 the maximum knock_volt can be * 0.019531 is never going to be more than 4.

I suggest you do the scaling completely with integer (x10 or x100), then insert the decimal for printing.

For example...
knock_bits  0-1023 or 0-5V

knock_bits = 511 or 2.5V or  204.4 bits per volt.
since we want two decimal place precision voltsPerBit = 2044

long volts (because int will overflow)

volts = (knock_bits * 1000) / 2044


volts now contains the actual volts * 100
Very fast, no floating point.
It's just a matter of formatting the output.

Avoid using floating point.
When reading only 1024 bits, the precision is ony 1 bit or 1/1000 easy to handle without floating point.


Hope that helps.



« Last Edit: January 19, 2009, 07:03:41 pm by matthewsg » Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As George says, you're losing data in knock_volt.

Code:
byte knock_volt;
knock_volt = analogRead(knockPin);
knock_volt = map(knock_volt, 0, 1023, 0, 255);

After this, the highest value you could have in knock_volt is 63.  What's worse, the value 63 will appear when the voltage is at 1/4, 1/2, 3/4 and full levels.
« Last Edit: January 19, 2009, 08:11:03 pm by halley » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks folks!  That took care of it!  smiley-grin  

Lesson learned!
Logged


Pages: [1]   Go Up
Jump to: