Hi all,

been playing with the Mega board for a project i am working on.

i have 6 Leds in the circuit to indicate a battery cell state of charge.

one LED is on when the state of charge is healthy between 593 and 870. if outside (above or below these values, this LED will turn off)

next Led comes on at 700
nest on at 757
next on at 808
next on at 839
next on at 870 (where the 1st one will turn off)

all of this works fine,.

i have alnother LED i want to go brighter from 593 up to 1023.

this is where i am stuck…

i can only get the LED to go brighter from point 0 to 1023.

how could i write this so that it only becomes brighter from point 593 up to 1023?

i have tried changing the analog output values, but this will only cycle the values more frequently.

the intention is that this particular LED gets brighter as the battery increases voltage, so you can see if it is overcharging or not.

below is the script for this set up.

int analogOutPin = 9; //analog output pin 9 selected

int outputValue = 0;

int val0 = 0; // variable to store the value read of a0

int op1 = 2; // LED connected to digital pin 2
int op2 = 3; // LED connected to digital pin 3
int op3 = 4; // LED connected to digital pin 4
int op4 = 5; // LED connected to digital pin 5
int op5 = 6; // LED connected to digital pin 6
int op6 = 7; // LED connected to digital pin 7
int op9 = 9; // LED connected to digital pin 9 //op9 set up

void setup()

{

Serial.begin(9600); // setup serial
pinMode(op1, OUTPUT); // sets the digital pin as output
pinMode(op2, OUTPUT); // sets the digital pin as output
pinMode(op3, OUTPUT); // sets the digital pin as output
pinMode(op4, OUTPUT); // sets the digital pin as output
pinMode(op5, OUTPUT); // sets the digital pin as output
pinMode(op6, OUTPUT); // sets the digital pin as output
pinMode(op9, OUTPUT); // sets the digital pin as output //op 9 for pwm
}

void loop()

{

// map it to the range of the analog out:
outputValue = map(val0, 0, 1024, 0, 255); //need to commence the LED getting brigheter from 757 up to 1024.
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor:
Serial.print("sensor = “);
Serial.print(val0);
Serial.print(”\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);

if (val0 > 700 && val0 < 1024) //this will turn on LED once this value is met, and turn off if below
{
digitalWrite(op1, HIGH);

}
else
{
digitalWrite(op1, LOW);
} //PG end of op1
if (val0 > 757 && val0 < 1024) //this will turn on LED once this value is met, and turn off if below
{
digitalWrite(op2, HIGH);

}
else
{
digitalWrite(op2, LOW);
} //pg end of op2

if (val0 > 808 && val0 < 1024) //this will turn on LED once this value is met, and turn off if below
{
digitalWrite(op3, HIGH);

}
else
{
digitalWrite(op3, LOW);
} //PG end of op3
if (val0 > 839 && val0 < 1024) //this will turn on LED once this value is met, and turn off if below
{
digitalWrite(op4, HIGH);

}
else
{
digitalWrite(op4, LOW);
} //PG end of op4
if (val0 > 870 && val0 < 1024) //this will turn on LED once this value is met, and turn off if below
{
digitalWrite(op5, HIGH);

}
else
{
digitalWrite(op5, LOW);
} //PG end of op5
if (val0 > 593 && val0 < 870) //this will turn on LED once the low value is met, and turn off when the high high is met. it stays on during these 2 values at al times.
{
digitalWrite(op6, HIGH);

}
else
{
digitalWrite(op6, LOW);
} //PG end of op6
Serial.println(val0 ); // debug value

}

any help much ap[preciated!

Philscoupe:
i have alnother LED i want to go brighter from 593 up to 1023.

this is where i am stuck...

i can only get the LED to go brighter from point 0 to 1023.

how could i write this so that it only becomes brighter from point 593 up to 1023?

``````analogWrite(pwmPin, map(constrain(analogInput(inputPin), 593, 1023), 593, 1023, 0, 255));
``````

Thanks for that Johnwasser.

all worked great.

Have a great christmas!