Pages: [1]   Go Down
Author Topic: Analog line meter  (Read 946 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, Im trying to implement and analog line meter that takes a value from an LDR. I have drew the arc but I dont understand how I can get the line to move depending on what value comes from the LDR. a pointer in the right direction to  what i should be looking up is appreciated.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I dont understand how I can get the line to move
What line, can you be more descriptive about what hardware you have got or what you want.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The arc at the bottom left is where i want my meter, it will take a value from the LDR between 0 - 1023 and the line will move from the left around the arc.



Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14007
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Do you want to convert the value (0..1023) to an angle?

assuming you do:

consider 0,0 as the midpoint of the arc, and R its radius,   

float alpha = 180 - analogRead(A0) / 5.68333;
int x = R * cos(alpha):
int y = R * sin(alpha);
line 0,0, x, y;

you need to keep the XY value to clear the line before drawing the next;
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thanks for the help, Im still having problems

When I use your code i get a cant convert float to int

so I tried after converting value from LDR, float angle = 180 - (LDR / 5.68333);

Code:

float x = 90 + 65 * cos(angle);
float y = 490 + 65 * sin(angle);
line (90,490,x,y);

Now I got a perfect line from midpoint of arc to edge of arc, but the angle in the debug output: example 156.732 doesnt match where the needle in the meter is, the needle is shifting all the time even though the LDR is giving a constant value?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14007
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the range of the LDR ? minimum and maximum?
ok from 0-1023

My mistake sin() and cos() needs radians. OK try this:


float angle = 3.14159265 - (LDR / 325,6310);
float x = 90 + 65 * cos(angle);
float y = 490 + 65 * sin(angle);
line (90,490,x,y);
« Last Edit: August 12, 2011, 07:01:09 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats worked great, the needle was going around the bottom of the arc so I just done a -325.6310 and that put the needle in the proper position, Im getting data through every 3 seconds so the needle doesn't move in a smooth fashion but the data is being collected long term so that doesn't matter, My trig is so bad I think I'll need to spend some time brushing up on it, thanks for your help.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14007
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You can do some smoothing like this:
Code:
  float newAngle =  3.14159265 - (LDR / 325,6310);

  #define  ALPHA = 0.2;
  static float angle = ALPHA * newAngle + (1-ALPHA ) * angle;  // changes smoothly to new value, speed depends on ALPHA

  int x = 90 + 65 * cos(angle);
  int y = 490 + 65 * sin(angle);
  line (90,490, x, y);

OR average multiple readings (takes more time)

int LDR = 0;

#define NR_LDR_READS  4

for (uint8_t i =0; i< NR_LDR_READS; i++) LDR += analogRead(LDRPIN);
LDR /= NR_LDR_READS;
etc.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Played around with the formula a bit, I got some nice smooth motion with alpha set to 0.950, would probably be better to take an average though, Im going to get the arduino system to do a bit more processing, get a background template and just have processing do the data representation.

Logged

Pages: [1]   Go Up
Jump to: