programing help beginner

the code is as below:

int pulse=A0;
void setup()
 {
  // put your setup code here, to run once:
 pinMode(pulse,INPUT);
  Serial.begin(9600);

}
void loop() 
{
  // put your main code here, to run repeatedly:
float out =  analogRead(pulse);
float val=out*(5.0/1023);    //calculate analog voltage
while(val!=2.9||val!=3.0)
{
if(0<=val<2.9)           //executes if analog value <=2.9V
  Serial.println("value is less");
else
  Serial.println("value is more");  //executes if value greater than 3V
out =  analogRead(pulse);   //updating analog values
 val=out*(5.0/1023.0);
Serial.println(val);
delay(100);            //delay for visible display
}

}

Here analog values calculated and displayed are exact.
Actually hardware setup is where:
pin A0 (analog pin) is connected to potential divider circuit.
And the max voltage varies from 0-3.3V(to be on safer side not exceeding 4V)
My observations:
Say if voltage =0V initially then serial monitor starts displaying value is less as per the code.
But, if voltage changes to 3.1V or some greater value that analog voltage value is correctly displayed but,monitor displays "" value is less"".
Then if i close the monitor and restart it then only if pin is at 3.1 or greater it starts displaying ""value is more"".
Now to see the changed message i need to every time restart the serial monitor.
This should not happen as it shows that if-else statement is not correctly executed.
I dont find any mistakes in my code. Still the serial monitor is not displaying ""correct messages""(but displaying correct analog voltage values) for varying voltage values why is this so?

Try declaring your constants as float (with an "f" on the end).

Like:

float val=out*(5.0f/1023f);    //calculate analog voltage
while(val!=2.9f||val!=3.0f)
....

and ==/!= is not a great idea for floats.

while(val!=2.9||val!=3.0)

!= and == are very rarely used when floating-point values are involved.

Can you explain in words what you're trying to do?

This if(0<=val<2.9) doesn't do what you think.

if(0<=val && val<2.9)

(In fact, val can't be negative)

float out =  analogRead(pulse);

The analogRead() function does not return a float.

The analog pins are INPUT only. The pinMode() function does NOT affect analog pins. It is, therefore, useless to call it for analog pins.

while(val!=2.9||val!=3.0)

While? The while clause will always be true. If val is 2.9, it can not be 3.0, so there is no way to skip the while statement body. It is better to not use floats in while or if statements. The values correspond to some int value read from the pin. Base the decisions on the values read from the analog pin, not the values that the readings were mapped to.

I dont find any mistakes in my code.

I did.

dishak:
I dont find any mistakes in my code.

That happens to me a lot. I am ALWAYS wrong.

...R

AWOL:
Can you explain in words what you're trying to do?

battery voltage detector just as a protection circuit for battery.
Checking if voltage goes above predetermined values.
Hence as for a 12V battery safe operating voltage 12.35-13.3V
then i have to detect these precise values hence used floating point numbers.

then i have to detect these precise values hence used floating point numbers.

Wrong. The voltage is a function of the value read from the analog pin. Some reading maps to 12.35 volts. Some other reading maps to 13.3V. Use the readings that map to 12.35 and that map to 13.3 as the limits, NOT the voltage that the readings map to.

then i have to detect these precise values hence used floating point numbers

That's a rather oxymoronic statement.
Or is it a non sequitur?

Whatever.

AWOL:
That's a rather oxymoronic statement.
Or is it a non sequitur?

Whatever.

Stupid Cow ?

(a play on language, not a reference to the OP).

...R

dishak:
battery voltage detector just as a protection circuit for battery.
Checking if voltage goes above predetermined values.
Hence as for a 12V battery safe operating voltage 12.35-13.3V
then i have to detect these precise values hence used floating point numbers.

What battery technology are you using? NiCad? Lead Acid? Lithium?

Kiwi_Bloke:
What battery technology are you using? NiCad? Lead Acid? Lithium?

I am using lead acid battery.

PaulS:
It is better to not use floats in while or if statements. The values correspond to some int value read from the pin.

Yes i modified the code and now working fine.

Here is the code:
just a bit of explaination what i am trying to do:
Here circuit setup is: a potential divider with voltage across it is at max=18-21V(basically i am connecting a solar panel).Lets say this as terminal voltage.
The lower resistor measures=21.8K.higher resistor measures=99.9K.
The programing is to measure voltage across 21.8K through analog input pin my arduino board used.
a suitable load is connected(either a battery needed to charge or a resistive load)
Hence as current drawn increases the terminal voltage decreases and thus there is a set point of around 16V i.e voltage should not drop below 16V neither should increase above 16V hence PWM should be proportionately increased or decreased if terminal voltage increases or decreases respectively.I have written code below but results are not as expected

int freq=9;      //produces PWM of appropriate frequency (in this case 20Khz) 
int pulse=A0;   //analog i/p to detect voltage level at lower resistor
int j=0;       
int out2;       
float out1;
void setup() 
{
  // put your setup code here, to run once:
 pinMode(freq,OUTPUT);     //make freq a output pin
 pinMode(pulse,INPUT);     
 Serial.begin(9600);
}
int i=35;
void loop()   //main loop
{
int  out =  analogRead(pulse);    //checking for terminal voltage   
float out1=(out/1023.0)*5.0;  
int out2=(out1)*(21.8+99.9)/(21.8);   //calculating terminal voltage
while(out2<16)       //terminal volatge kept at fixed at 16V
{
i--;
if(i<=0)  
i=1;          //minimum duty cycle

for(j=0;j<10;j++)          //this loop waits for required PWM to be applied 
{
digitalWrite(freq,HIGH);              
delayMicroseconds(i);         //ton time    of "i"microseconds
digitalWrite(freq,LOW);                    
delayMicroseconds((36-i));             //toff time of :36-i microseconds. 36us=time period
}
int  out =  analogRead(pulse);    //checking for terminal voltage   
float out1=(out/1023.0)*5.0;  
int out2=(out1)*(21.8+99.9)/(21.8);   //calculating terminal voltage
}
}    
i++;                    //if terminal voltage goes beyond 16V             
if(i>=35)
i=35;
for(j=0;j<10;j++)          //this loop waits for required PWM to be applied 
{
digitalWrite(freq,HIGH);              
delayMicroseconds(i);         //ton time    of "i"microseconds
digitalWrite(freq,LOW);                    
delayMicroseconds((36-i));             //toff time of :36-i microseconds. 36us=time period
}
}

problem:
though if voltage goes far less than 16v the expected o/p should be that the ton=0 and toff=max hence load is disconnected completely from supply but,in actual still the load is getting max supply i.t ton is at max=35 and toff is at min.

When you have your code in the source window of the IDE, try using Ctrl-T before you post to the Forum. It will make it easier for us to read.

After you use Tool AutoFormat on your code put your mouse cursor on the FIRST bracket ({) in loop() and check WHERE is the terminating (}) bracket.

Fix the problem.

Next emulate and debug.
Use Serial to debug and set the voltage out2 to test level and run your code.
And also check scope of variables , you probably do not want "out2" declared multiple times.

int freq = 9;   //produces PWM of appropriate frequency (in this case 20Khz)
int pulse = A0; //analog i/p to detect voltage level at lower resistor
int j = 0;
int out2;
float out1;
void setup()
{
  // put your setup code here, to run once:
  pinMode(freq, OUTPUT);    //make freq a output pin
  pinMode(pulse, INPUT);
  Serial.begin(9600);
}
int i = 35;
void loop()   //main loop
{
  int  out =  analogRead(pulse);    //checking for terminal voltage
  float out1 = (out / 1023.0) * 5.0;
  int out2 = (out1) * (21.8 + 99.9) / (21.8); //calculating terminal voltage


TEST here 


 out2 = 15.956; 



  while (out2 < 16)    //terminal volatge kept at fixed at 16V
  {
    i--;
    if (i <= 0)
      i = 1;        //minimum duty cycle
    for (j = 0; j < 10; j++)   //this loop waits for required PWM to be applied
    {
      digitalWrite(freq, HIGH);
      delayMicroseconds(i);         //ton time    of "i"microseconds
      digitalWrite(freq, LOW);
      delayMicroseconds((36 - i));           //toff time of :36-i microseconds. 36us=time period
    }
    int  out =  analogRead(pulse);    //checking for terminal voltage
    float out1 = (out / 1023.0) * 5.0;
    int out2 = (out1) * (21.8 + 99.9) / (21.8); //calculating terminal voltage
  }
}
i++;                    //if terminal voltage goes beyond 16V
if (i >= 35)
  i = 35;
for (j = 0; j < 10; j++)   //this loop waits for required PWM to be applied
{
  digitalWrite(freq, HIGH);
  delayMicroseconds(i);         //ton time    of "i"microseconds
  digitalWrite(freq, LOW);
  delayMicroseconds((36 - i));           //toff time of :36-i microseconds. 36us=time period
}
}

dishak:
I am using lead acid battery.

Two things to consider, Lead Acid batteries float at 13.8VDC and typically accept boost charge at 14.4V for Sealed and can go higher for "Wet" unsealed batteries. Voltages higher than this will cause the batteries to gas out, potentially releasing corrosive and flammable gas (Sealed Batteries will still release Gas to the atmosphere if the pressure is high enough, however they are more likely to deform in shape and split.).

Also the Resistor Divider that you are using will reduce 21VDC to approximately 3.8Volts which MAY impact your calculations.

I would be interested to see what the wiser heads say about your formulae, I would use absolute voltage references based on maximum possible panel voltage (at absolute lowest expected temperature at the Solar Panel site) and then work from there.