Incorrect calculations anf outputs

I have a problem with my arduino uno r3, its gives me a wrong outputs always, in my command below is a solar extraterrestrial radiation theoretical formular that doesnt needs any input from the arduino which answer should be constant but it always gives different wrong answers althrough and it also give wrong reading voltage output for a0. Please could anyone help me through this, i really dont know where the problem comes from. Thank you

Did you forget something?

.....................
Here have some full stops, you seem to be short of them. You use them to indicate the end of a sentence. That is the bit where the reader takes a breath, they make words easy to understand.

below is a solar extraterrestrial radiation theoretical formula

Sorry can’t see it.

i really dont know where the problem comes from

The problem comes from a mistake you made, what that is, is impossible to tell because you have told us nothing.

Please read the how to use this forum sticky post, it tells you how to ask a question.

Sorry i did uploaded a file containing the codes and this is the formular. I powered the aiduino with a 9v battery. Thank you

float sine= ((6.28318530718 * ND)/ 365) - 1.39;
float cose= ((6.28318530718 * ND)/ 365);
float earth = 0.409 *sin(sine);
float tane= tan(lat) * tan(earth);
float angle = 1 / cos(- tane);
float disEarth= 1 + (0.033 * cos(cose));

float TheoryRad = (((24*60) / 3.1415926536) * (0.94907 * disEarth)) * ((angle * sin(lat) * sin(earth))+
(cos(lat) * cos(earth) * sin(angle)));

Post all your code.
Use code tags.
Post the results you get from a given set of inputs.
Post the results you expect from the same set of inputs.

Did you read what I told you? Apparently not.

We need to know what this sensor is, provide a link to the data sheet.

We need to know how how you have wired it up, so please post a schematic, not a Fritzing physical layout diagram.

We need to know if you have made a mistake in wiring so please post a photograph of your wiring.

Finally we need to see your code, so post that, using the copy for forum function of the IDE and paste it into your reply.

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>

#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)


#define SYNC_INTERVAL 1000 //  to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 0 // Echo data to serial monitor (Setting it to 0 will turn it off)
#define WAIT_TO_START 0 // Wait for serial input in setup() (Setting it to 1 you have to send a character to the Arduino Serial port to start the logging)

#define redLEDpin 2
#define greenLEDpin 3

#define sensorPin A0
// #define PANEL_WIDTH 0.254
// #define PANEL_LENGTH 0.1905
#define AREA 0.04837
#define RESISTANCE  12.2//
//#define solarAng 180    // ALWAYS REMEMBER TO CHANGE THIS
#define lat  0.14798817    // Always remember to change this in RADIANS
#define ND  247  //the day 

;

RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);
  while (1);
}
void setup(void)
{
  Serial.begin(9600);
  Serial.println();
  // use debugging LEDs
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START
  // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break; // leave the loop!
    }
  }
  if (! logfile) {
    error ("couldnt create file");
  }
  Serial.print("Logging to: ");
  Serial.println(filename);
  // connect to RTC
  Wire.begin();
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
  }
  logfile.println("millis    |    stamp          |   date           |    time         | voltage |    Sensorvol    |     Theory-Rad    |   sensor-Rad   ");
#if ECHO_TO_SERIAL
  Serial.print("    millis     ");
  Serial.print("     ");
  Serial.print("      stamp    ");
  Serial.print("     ");
  Serial.print("     ");
  Serial.print("   datetime    ");
  Serial.print("     ");
 
  Serial.println("   voltage   ");
  Serial.print("     ");
  Serial.print("\t");
  Serial.println("  Sensorvol    ");
  Serial.print("\t");
  Serial.print("\t");
  Serial.println("    Theory-Rad    ");
  Serial.print("\t");
  Serial.print("\t");
  Serial.println("    sensorRad    ");
#endif //ECHO_TO_SERIAL
}
void loop(void)
{
  // stores the value read on analog input A0
     int sensorVal = analogRead(A0);
  // set the range of values to be considered for datalogging
  if (sensorVal >= 3.25) // 3.25 corresponds roughly to 12V on the PV cell
  {
    DateTime now;
    // delay for the amount of time we want between readings
    delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));
    digitalWrite(greenLEDpin, HIGH);
    // log milliseconds since starting
    uint32_t m = millis();
    logfile.print(m); // milliseconds since start
    logfile.print("; ");
#if ECHO_TO_SERIAL
    Serial.print(m); // milliseconds since start
    Serial.print("\t");
#endif
    // fetch the time
    now = RTC.now();
    // log time
    logfile.print(now.unixtime()); // seconds since 1/1/1970
    logfile.print("          | ");
    // logfile.print();
    logfile.print(now.day(), DEC);
    logfile.print(".");
    logfile.print(now.month(), DEC);
    logfile.print(".");
    logfile.print(now.year(), DEC);
    logfile.print("  |  ");
    logfile.print(now.hour(), DEC);
    logfile.print(":");
    logfile.print(now.minute(), DEC);
    logfile.print(":");
    logfile.print(now.second(), DEC);
    // logfile.print('"');
#if ECHO_TO_SERIAL
    Serial.print(now.unixtime()); // seconds since 1/1/1970
    Serial.print("    |");
    Serial.print(now.day(), DEC);
    Serial.print(".");
    Serial.print(now.month(), DEC);
    Serial.print(".");
    Serial.print(now.year(), DEC);
    Serial.print("  |    ");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.print(now.second(), DEC);
    Serial.print("    |   ");
#endif //ECHO_TO_SERIAL
                 // converting the value of sensorVal to voltage
                 float voltage = (5.0 / 1024.0) * sensorVal;
                 logfile.print("     |    ");
                 logfile.print(    voltage    );
#if ECHO_TO_SERIAL
                 Serial.print(    voltage    );
#endif //ECHO_TO_SERIAL
                 float sensorvol = ((voltage * RESISTANCE) / 2.2);
                 logfile.print("   |     ");
                 logfile.print(   sensorvol     );
#if ECHO_TO_SERIAL
                 Serial.print(    Sensorvol     );
#endif //ECHO_TO_SERIAL
              float sine= ((6.28318530718 * ND)/ 365) - 1.39;
              float cose= ((6.28318530718 * ND)/ 365);
             float earth = 0.409 *sin(sine);
               float tane= tan(lat) * tan(earth);
             float angle = 1 / cos(-  tane);
             float disEarth= 1 + (0.033 * cos(cose));
 float TheoryRad = (((24*60) / 3.1415926536) * (0.94907 *  disEarth)) * ((angle *  sin(lat) *  sin(earth))+ (cos(lat) * cos(earth) *  sin(angle)));
#if ECHO_TO_SERIAL
                 Serial.print(   TheoryRad      );
#endif //ECHO_TO_SERIAL
                 float sensorRad = ((sensorvol*sensorvol)/RESISTANCE)/AREA;
                 logfile.print("          |    ");
                 logfile.println(   sensorRad   );
#if ECHO_TO_SERIAL
                 Serial.println (   sensorRad   );
#endif //ECHO_TO_SERIAL
             
                 digitalWrite(greenLEDpin, LOW);
                
                 if ((millis() - syncTime) < SYNC_INTERVAL) return;
                 syncTime = millis();
               
                 digitalWrite(redLEDpin, HIGH);
                 logfile.flush();
                 digitalWrite(redLEDpin, LOW);
               }
                 else {
#if ECHO_TO_SERIAL
                 Serial.print("sensorVal is out of range: ");
                 Serial.println(sensorVal);
#endif // ECHO_TO_SERIAL

               }
               }

I am supposed to get the Theory-Rad result = 0.017869943 all through, but instead i got different answers as follows
187.04
77.03
78.90
87.05 and so on.

  • The date is supposed to be today's date and time but it shows 25.7.2011 with wrong time.
  • Voltage is supposed to be 0, if i dont connect anything to the a0 neither did i connect to ground, except its dc power supply. Where i connected 9v battery. But, i got series of readings like 1.89, 1.22, 1.23, 1.29 and so on.

Are you saying there is nothing connected to A0, and you expect the readings from that pin to be zero?

A (32-bit) float has a accuracy of about 6 digits. 6.28318530718 has 12 digits... So substantial error is to be expected.

septillion:
A (32-bit) float has a accuracy of about 6 digits. 6.28318530718 has 12 digits... So substantial error is to be expected.

please how do i correct it, should i use double instead?

On most Arduinos (the 8-bitters) a double is the same as a float so no change there. Easiest way for high precision calculations, stick with integer math.
234.54 / 1.234567 is just 234540000 / 1234567 with the decimal removed.

AWOL:
Are you saying there is nothing connected to A0, and you expect the readings from that pin to be zero?

with all due respect sir, i expected it to be zero since nothing is connected to it.

Unfortunately, high impedance inputs don't know anything about respect.
Tie it to ground and report back.

A football in the back of your car, is it on the left or the right?

Same with a not connected input, it floats. It can be anything :slight_smile:

with all due respect sir, i expected it to be zero since nothing is connected to it.

I don't think you understand the term "with all due respect", it is quite an insult you know.

Your expectations and Physics are two different things, and in these sorts of conflicts Physics always wins.

Read this, it specifically talks about digital inputs but it applies to analogue ones as well.
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html