# help needed

Hi people

I have a problem and I dont know how to solve it.

my code is this…

``````    {

if  (average > 0)

{
dir = HIGH;
}
else if (average < 0)
{

dir = LOW;
}

int i;

digitalWrite(dirpin, dir);     // Set the direction.
delay(100);

for (i = 0; i<average; i++)       // Iterate for 4000 microsteps.
{
digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(200);      // This delay time is close to top speed for this
}
``````

but it only goes in 1 direction.
this is because of this line…

``````for (i = 0; i<average; i++)
``````

it does not work if (average) is a -#

how do i make (average) always be a +# ??

thanks

You could use the abs() function to get the absolute value of average or just test it yourself as shown below.

Cheers

``````{
if  (average > 0)

{
dir = HIGH;
}
else if (average < 0)
{

dir = LOW;
}

int i;

digitalWrite(dirpin, dir);     // Set the direction.
delay(100);

for (i = 0; i<absavg; i++)      //CHANGED  // Iterate for 4000 microsteps.
{
digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(200);      // This delay time is close to top speed for this
}
``````

how can this work ?

``````for (i = 0; i<absaverage; i++)
``````

if (absaverage) was not declared ??

cheers

abs is a function (or a macro) Or you coulld define "absavg"

abs(x)
Description

Computes the absolute value of a number.

Parameters

x: the number

Returns

x: if x is greater than or equal to 0.

-x: if x is less than 0.

This is what i dont want to happen …

i need -x to be x even if its <0

I’s a function; it doesn’t change the value of its argument, unless you make it.

``````for (i = 0; i<abs(average); i++)
``````

so now my code looks like this…

``````{
int dir;

if  (average > 0)

{
dir = HIGH;

}
else if (average < 0)
{

dir = LOW;

}

int i;

digitalWrite(dirpin, dir);     // Set the direction.
delay(100);

for (i = 0; i<abs(average); i++)      //CHANGED  .
{
digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(200);      // This delay time is close to top speed for this
}
``````

and i still get the -#

what am i doing wrong???

Where do you get the -ve value?

``````     total= total - readings[index];

index = index + 1;

index = 0;
// calculate the average:
``````

my average range is from +30 to -30

I’m not following you - from your original post, you need average to be +ve or -ve to allow you to set the direction.

i need -x to be x even if its <0

if “x < 0”, then “-x” is a +ve number.

If you never need it to be -ve, simply set “avg = abs (avg);”

Thanks Groove

you helped lots :) :) :)

how can this work ?

Code:

for (i = 0; i<absaverage; i++)
if (absaverage) was not declared ??
cheers

Yeah - I noticed my typo and fixed it almost immediately. Wonder why you still got the older version that much later?

cheers.