Analog Problems

Guys and gals

I am a newbie at programming.

I recently started to write a small program, which will be expanded as I develop it.

At first, I got it reading the analog ports and calculating the results as I wanted. However, as one of the value fluctuated a bit, I added anouther loop to take additional readings and average them. I, unfortunately, forgot to put any delays in the new loop. This resulted in no O/P on the serial monitor.

Now, no matter what I seen to do with the code, all the analog values are reported as 0.

By omitting this loop delay, could I have damaged the micro?

TIA

Fof

No

Pete

I have attached the code, which no longer works (after removal of the extra loop).

What am I doing wrong?

Fof

Test_System_V1.ino (2.16 KB)

//Assigning sensors
//temperature - A0
//voltage from solar panel -A1
// voltage from battery - A2



 void setup () 
 {

 Serial.begin(9600);

 }

void loop()
{
  
  float temperature = 0;
  float solarpanel = 0; 
  float battery = 0; 
  int counter = 0;
  float store = 0;


//Get Temperature
 
   while (counter <3)
          {
             store = (analogRead(A0)/1023*5*100);                  // read, convert to celcius and store
             temperature =  temperature + store;              // add temperature values
             counter ++;
             delay (50);
             
          }  
             counter = 0;
             store = 0;
//             temperature = 0;

//Get Solar Panel Voltage

   while (counter<3)
          {
            store = (analogRead(A1)/1023)*20*100;                  // read, convert to volts and store
            solarpanel =  solarpanel + store;                      // add voltages
            counter ++;
            delay (50);
          }
            counter = 0;
            store = 0;

//Get Battery Voltage

   while (counter<3)
          {
            store = (analogRead(A2)/1023)*20*100;                 //read, convert to volts and store
            battery = battery + store;                            //add voltages
            counter ++;
            delay(50);
          }


             temperature = temperature/3;                           //average the three reading
             battery = (battery/3)*4.8;                               //average the three readings (1/4 x12v) back to 12v
             solarpanel = ((solarpanel/3)*4.8) - 0.6;                 //average the three readings (1/4 x12v) back to 12v and remove blocking diode Vf

             Serial.print ("Temperature = ");
             Serial.println (temperature,1);
             Serial.print ("Solar Panel voltage = ");
             Serial.println (solarpanel,1);

              if (solarpanel>battery)
                {
                  Serial.println ("Battery is charging");
                 }
                       else
                      {
                       Serial.println ("Battery is not charging");
                       }

                 delay(5000);
}
store = (analogRead(A0)/1023*5*100);

An integer that cannot exceed 1023, divided by the integer 1023, can only be zero or one.

AWOL

The values returned from the ports should never exceed 1023.

A0 would return a value in the region of 40-50
A1 and A2 could, though my calculations are that they should return a value of approx. 300.

As an experiment, I will try with just one read.

Back to the drawing board.

Thanks

A0 would return a value in the region of 40-50

So, let's say 40.
40 / 1023 = ?

(If your answer includes a decimal point, you haven't understood the problem)

Hint: the answer is identical for the other extremity of your example.

See how things are done here:

.

AWOL

Thanks for your I/P, but I am more confused. As you say, the result of the calculation, eg (40/1023)5100 will give a result with a decimal point, but as this is being stored in a floating point variable, store, I don't see where it is wrong. Comments, please.

LarryD

Thanks, also, for your I/P.
As originally written, it worked. If I cut it back to very bare bones it now returns 0, as does a version written as per your link.

void setup () 
 {

 Serial.begin(9600);

 }

void loop()

{
  int tempstore = 0;
  float temperature = 0;
  float store = 0;

      tempstore = analogRead(A0);
      store = (tempstore/1023)*5*100;
      Serial.println(store);
}
             Serial.print ("Temperature = ");
             Serial.println (temperature,1);

                 delay(5000)
};

I am going to do a reinstallation of the Arduino program, as I am getting some errors that seem to be erronious. The latest is when I try to compile this I get "error: 'Serial' does not name a type" for both lines that call Serial.println().

Thanks all. Back to the drawing board. Hardware is a lot easier. :-))

As you say, the result of the calculation, eg (40/1023)5100 will give a result with a decimal point

I never said (nor wrote) any such thing.

You're storing an integer in a floating-point variable.
It's a very small integer, but it's an integer all the same.

Hint: Integer arithmetic.

I am going to do a reinstallation of the Arduino program, as I am getting some errors that seem to be erronious.

Are you sure?

Where does the function "loop()" end in the code you last posted?

IamFof:
Thanks for your I/P, but I am more confused. As you say, the result of the calculation, eg (40/1023)5100 will give a result with a decimal point, but as this is being stored in a floating point variable, store, I don't see where it is wrong.

What AWOL is telling you is that all of the calculations on the right hand side of that expression will be done in integer arithmetic.

C will use integer arithmetic when ever it can, unless you force it to do otherwise. C doesn't care about the data type of the left hand side of the expression until right at the end of the calculation, when it will try to convert it to float.

Hi,
In post #4
On the right hand side of the =;

40/1023 = 0 in integer maths. 0 * 5 * 100 = 0 in integer maths

40.0/1023.0 = 0.39 in floating maths 0.39 * 5.0 *100.0 = 19.55 in floating maths

store = (float)analogRead(A0) * 5.0* 100.0/ 1023.0;

but nicer in my opinion;

int rawA0 = analogRead(A0);
store = (float)rawA0 * 500.0 / 1023.0;

Tom... :slight_smile:

Hi all

Apologies for wasting your time. It would seem that I have made a basic error in understanding the float/int calculations.

Back to the books and do some more reading.

Fof

You should also checkout the map(), constrain() functions:
https://www.arduino.cc/en/Reference/Map