Go Down

### Topic: Analog line meter (Read 1 time)previous topic - next topic

#### DirtGambit

##### Aug 12, 2011, 10:34 pm
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.

#### Grumpy_Mike

#1
##### Aug 12, 2011, 10:47 pm
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.

#### DirtGambit

#2
##### Aug 12, 2011, 11:09 pm
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.

#### robtillaart

#3
##### Aug 13, 2011, 12:20 am

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;
Rob Tillaart

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

#### DirtGambit

#4
##### Aug 13, 2011, 01:27 am
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: [Select]
`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?

#### robtillaart

#5
##### Aug 13, 2011, 01:54 amLast Edit: Aug 13, 2011, 02:01 am by robtillaart Reason: 1
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);
Rob Tillaart

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

#### DirtGambit

#6
##### Aug 13, 2011, 10:22 am
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.

#### robtillaart

#7
##### Aug 13, 2011, 10:50 am

You can do some smoothing like this:
Code: [Select]
`  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  4for (uint8_t i =0; i< NR_LDR_READS; i++) LDR += analogRead(LDRPIN);LDR /= NR_LDR_READS;etc.`
Rob Tillaart

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

#### DirtGambit

#8
##### Aug 13, 2011, 03:12 pm
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.

Go Up