Re-reading values and printing them in the monitor

Hi there,

As an (older) beginner I am struggelling with a for you all probably simple problem.
I try to read values from the A(0) analog input using a simple LDR and a resistor.
The problem is that the readings take place, the average is calculated, the results printed via serial monitor, but once read, the results stay the same over and over, whether I cover the LDR from light or not. It is as if the values either are not read again (seems very unlikely) or the new results are not "handed over"to println.
If the serial monitor is stopped and I cover the LDR, then start the monitor agin, the values are different, which to me means the set up of the LDR and resistor works well.
I have tried all kinds of changes to the sketch, without result sofar. Please Help!

Here is the sketch:


int maxReadings = 10;
int numReadings;
float reading;    
int total = 0;                   
float Average = 0;                 

void setup() {
  
Serial.begin(9600);
          
}
void loop()
{
  
  for(reading = 0; numReadings < maxReadings; reading++)
  {
    reading = analogRead(A0);
    numReadings++;
    total = total + reading;
    Average = total / numReadings;
  }  
  
  if(numReadings = 10);  
     
    Serial.println(total);
    Serial.println(Average);
    
    Serial.println();
    delay(1000);

   }

Thanks for helping out!
Jaap


  if(numReadings = 10);  

2 mistakes in this line

First
= is for assignment
== is for comparison

Second
Delete the semicolon at the end of the line. It is the only code dependant on the test

Thanks Bob,
Corrected it, but it still gives me the same total and average over and over............
Any more suggestions?
Actually, putting the line in that you refer to, was one of my attempts to get it working, but I think the line can be deleted completely right?
Jaap

Please post your corrected code in a new post

What do you want to print if numReadings equals 10 ?

OK, Sorry Bob,

Here it is:

int maxReadings = 10;
int numReadings;
float reading;    
int total = 0;                   
float Average = 0;                 

void setup() {
  
Serial.begin(9600);
          
}
void loop()
{
  
  for(reading = 0; numReadings < maxReadings; reading++)
  {
    reading = analogRead(A0);
    numReadings++;
    total = total + reading;
    Average = total / numReadings;
  }  
  
  if(numReadings == 10) 
     
    Serial.println(total);
    Serial.println(Average);
    
    Serial.println();
    delay(1000);

   }

See second question in reply #4

  if (numReadings == 10)

How many times will numReadings equal 10 in your sketch ? Its value starts at zero when declared, you increment it in a for loop that never reaches 10 but never reset its value to zero

Try just removing this line entirely. :slight_smile: It's not needed and you forgot the braces {}.

The printing of the total and average are a way right now for me to see if the sketch works the way I intend.
Let me give some backgroud.
I have a model railway and the present short cut detection is not too good.
So, I plan to use an ACS712 module on A(0) which needs to be read a number of times to average the small current changes as my railroad is completely digital controlled and so the rails are fed with a block wave. The program should repeat this over and over. Until when a big current change happens (a short cut somewhere on the railway) , the program needs to respond by for instance having a relay drop off to cut the feed of the railway.
Hope this helps to understand my aim!

The ACS712 does not have sufficient current sensitivity and dynamic range to capture DCC track signals.

OK aarg.....................didn't know that.............end of story then I'm afraid.

Need to go for now anyway
Thanks for helping you both!
Jaap

The end of the for loop does not depend on the variable reading, but on numReadings. and reading is used for the value your get from analogRead. You cannot use it as the loop variable.
try

for(numReadings = 0; numReadings < maxReadings; numReadings++)
  

and omit the

that is done by the for statement.

If you are doing occupancy detection, I mean. If you just want to detect a short circuit it might do. But the problem with that, the current booster will also detect a short and cut out. Then there is little current to detect.

I encourage you to continue the experiment since it's quite easy, and you can come to your own conclusions based on your own data.

Thanks aarg,
That is the idea; first get a simple example running and then try it out on sampling the values from the ACS712. The only aim is to detect a short circuit very fast and act on that by shutting down the power to the line.

Funny, because if I do that, the monitor is blank, nothing is printed at all

int maxReadings = 10;
int numReadings;
float reading;    
int total = 0;                   
float Average = 0;                 

void setup() {
  
Serial.begin(9600);
          
}
void loop()
{
  
  for(reading = 0; numReadings < maxReadings; reading++)
  {
    reading = analogRead(A0);
    
    total = total + reading;
    Average = total / numReadings;
  }  
  
  if(numReadings == 10) 
     
    Serial.println(total);
    Serial.println(Average);
    
    Serial.println();
    delay(1000);

   }

Hi Bob,
Just took another go with your remark about not resetting to zero.............woooow Thanks man!!
It works finally!
Here is the code:

int maxReadings = 10;
int numReadings;
float reading;    
int total = 0;                   
float Average = 0;                 

void setup() {
  
Serial.begin(9600);
          
}
void loop()
{
  
  for(reading = 0; numReadings < maxReadings; reading++)
  {
    reading = analogRead(A0);
    numReadings++;
    total = total + reading;
    Average = total / numReadings;
  }  
  
  if(numReadings == 10) 
    
    Serial.println(total);
    Serial.println(Average);
    
    Serial.println();
    delay(1000);
    numReadings = 0;
    total = 0;
    

   }

Now, if I move my hand over the LDR, the values move immediately! Exactly what I was looking for! The rest I think I'll be able to figure out. (see explanation of aim above for those who did not read that)
Thanks again!
Jaap

Faster than the booster can?

Well, you didn't make the change I suggested...

Hi aarg,
Built my own booster, has worked fine for years already, but does not have the capability of commercial ones. If you're interested it's called the EDITS Booster, once published in a magazine by that name. (EDITS) (90's?)

Well yes I did, but since I got blank monitor I put it back in!