-5

I'm working on my final project and I'm kind stuck in this code. I have tried many times but every time it gave me wrong results. My question is: if there anyone who has experience in Arduino code could just explain each line in this code.

#define SERIESRESISTOR 560
// What pin to connect the sensor to
#define NUMSAMPLES 15
int samples[NUMSAMPLES];
#define SENSORPIN A0
#define FLAP 1
#define FUDGE 0.3
void setup(void) {
Serial.begin(9600);
analogReference(EXTERNAL);
}

void loop(void) {
 uint8_t i;
 float average;
 float waterlevel;
 float lastwaterlevel=0;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(SENSORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
//Serial.print("Average analog reading ");
//Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
//Serial.print("Sensor resistance ");
//Serial.println(average);
waterlevel = 0;
waterlevel= -1 * 0.006958 * average + 11.506958+ FUDGE;
if (lastwaterlevel<(waterlevel-FLAP)||lastwaterlevel>(waterlevel+FLAP))
{
    Serial.print("Water level (inches) ");
    Serial.println(waterlevel);
}
delay(1000);
lastwaterlevel=waterlevel;
}

Thank you in advance!

Abdul
  • 1
  • 5
    "Explain every line in this code" is not an on-topic question here. You need to acquire a minimal understanding of the programming language you are using. Then you can ask specific questions about specific lines you don't understand and will get specific answers. – timgeb Feb 23 '16 at 23:39
  • If this is your code then there will be no need to explain. If it is someone elses code then there is no need to help in any way. SO is a place where you post your code and then explain what you are struggling with. We then see if we can notice where the issue is. – Blurry Sterk Feb 24 '16 at 05:25
  • Start with formatting the code. – MikeCAT Feb 24 '16 at 14:07
  • I'm sorry of the unconvincing situation: I'm struggling with part of the code: #define FLAP 1 #define FUDGE 0.3 What does it mean? – Abdul Feb 25 '16 at 04:33
  • @Abdul. The answers to these questions are easily found on the net just by searching for them. Search the following in google; "what is a define in c". Many answers to that. – Blurry Sterk Feb 26 '16 at 05:45

1 Answers1

1

I'm struggling with part of the code: #define FLAP 1 #define FUDGE 0.3 What does it mean?

You could look up how the C pre-processor works. To explain in this instance:

Before compiling, your code goes through a pre-processing stage. Amongst other things, it does textual replacements. For example:

#define FLAP 1

The pre-processor goes through your code and replaces every instance of FLAP by 1.

#define FUDGE 0.3

The pre-processor goes through your code and replaces every instance of FUDGE by 0.3.

And so on for the other defines.


To understand the code, just mentally do the substitution yourself.

For example:

#define NUMSAMPLES 15

So now the line:

for (i=0; i< NUMSAMPLES; i++) {

Effectively becomes:

for (i=0; i< 15; i++) {
Nick Gammon
  • 1,173
  • 10
  • 22
  • @Abdul. This is a great way of saving memory (RAM) as those values specified by the defines are replaced before the program is compiled. In contrast you get constants which in effect are variables that cannot change but which take up memory. Have a look at http://stackoverflow.com/questions/6442328/what-is-the-difference-between-define-and-const – Blurry Sterk Feb 26 '16 at 05:42
  • However `const NUMSAMPLES = 15;` would have the same effect. That doesn't take up memory because the compiler knows it is a constant (like 15 is). – Nick Gammon Feb 26 '16 at 05:45