cannot print to serial even to debug

Hiya folks,

Your help would be much appreciated on this problem…

I’ve built a sketch that attempts to do a couple of different lengths of windowing and some simple calculations (mean/variance) on the analog values from a couple of sensors. Previously I had the same code for 1 sensor working as intended but the code below has been expanded with a bunch of little for loops so that everything should run for both sensors now.

I simply can’t get anything at all to print to serial - even the two serial prints i put in setup and start of loop just to debug - yet the code compiles and uploads without any errors or warnings.

I apologise for including the whole sketch, I couldn’t think how to break it apart to show.

long int currentTime = 0;
    long int stopTime[2] = {0,0};
    long int shortWindowTime = 0;
    int shortVal[2][40];
    int reflexWindowStart = 0;
    int reflexWindowTime = 0;
    int reflexVal[2][500];
    int mean[2] = {0,0};
    unsigned int variance[2] = {0,0};
    int lowVal[2] = {0,0};
    int peakVal[2] = {0,0};
    int lowIndex[2] = {0,0};
    int peakIndex[2] = {0,0};
    int stopIndex[2] = {0,0};
    boolean stopped[2] = {false,false};
    
    void setup(){
      Serial.begin(9600);
      Serial.println("wtf?");
      for(int i=0;i<2;i++){
        for(int j=0;j<40;j++){
          shortVal[i][j] = 0;
        }
        for(int j=0;j<500;j++){
          reflexVal[i][j] = 1023;
        }
      }
      
    }
    
    void loop() {
    
      Serial.println("wtf?");
      
      currentTime = micros();
      
      if(currentTime - shortWindowTime > 500){
        
        shortWindowTime = currentTime;
        
        writeShortWindow();
        
        meanVariance();
        
        if(reflexWindowStart == 0){
          reflexWindow();
        }
        reflexWindowStart++;
        if(reflexWindowStart > 9){
          reflexWindowStart = 0;
        } 
      }
    } 
    
    void writeShortWindow(){
      for(int i=0;i<2;i++){
        for(int j=39; j>0; j--){
          shortVal[i][j] = shortVal[i][j-1];
        }
        int ground = analogRead(A5);
        shortVal[0][0] = analogRead(A1);
        analogRead(A5);
        shortVal[i][0] = analogRead(A2);
      }
    }
    
    void meanVariance(){
      for(int i=0;i<2;i++){
        for(int j=0; j<39; j++){
          mean[i] = mean[i] + shortVal[i][j];
        }    
        mean[i] = mean[i] / 40;
        for(int j=0; j<39; j++){
          variance[i] = variance[i] + sq(mean[i] - shortVal[i][j]) ;
        }
        variance[i] = variance[i] / 40;
      }
    }
    
    
    void reflexWindow(){
      
        for(int i=0;i<2;i++){
          if(stopped[i] == true){
            if((millis() - stopTime[i] > 20) && (peakVal[i] - shortVal[i][0] > 20) && (variance[i] <= 1)){ 
              stopped[i] = false;
              stopIndex[i] = 0;
              Serial.println("................................NOTstopped");
            }
          }
        }
        
        for(int i=0;i<2;i++){
        if(stopped[i] == false){
          
          lowVal[i] = 1023;
          peakVal[i] = 0; 
        
          for(int j=stopIndex[i]; j>0; j--){
            reflexVal[i][j] = reflexVal[i][j-1];
              if(reflexVal[i][j] < lowVal[i]){
                lowVal[i] = reflexVal[i][j];
                lowIndex[i] = j;
              }
          }
          
          reflexVal[i][0] = shortVal[i][0];
          
        
          
          for(int j=lowIndex[i]; j>=0; j--){
            if(reflexVal[i][j] > peakVal[i]){
              peakVal[i] = reflexVal[i][j];
            }
          }
        }
      }
    
      
      for(int i=0;i<2;i++){
        if(stopped[i] == false){
          if(peakVal[i] - lowVal[i] >= 50){
            Serial.print(i);
            Serial.println("...................................stopped");
            stopTime[i] = millis();
            stopped[i] = true;
          }
        }
      }
      
      for(int i=0;i<2;i++){
        if(stopIndex[i] < 499){
          stopIndex[i]++;
        }
      }
      
      Serial.print(shortVal[0][0]);
      Serial.print(" ... ");
      Serial.print(lowVal[0]);
      Serial.print(" ... ");
      Serial.print(peakVal[0]); 
      Serial.print(" ........ ");
      Serial.print(shortVal[1][0]);
      Serial.print(" ... ");
      Serial.print(lowVal[1]);
      Serial.print(" ... ");
      Serial.println(peakVal[1]);   
    }

Cheers!
MrJack

What Arduino are you using?
I assume you have set the baud rate in the serial monitor to the same rate as the sketch.

Try something much simpler to print out just a hello world.

Looks like a memory problem

This int reflexVal[2][500] is already 4 2kb of memory, try reduce this size and test again

2 kB, but you are probably right. There is another array as well.

Instead of:

for(int i=0;i<2;i++){
        for(int j=0;j<40;j++){
          shortVal[i][j] = 0;
        }
        for(int j=0;j<500;j++){
          reflexVal[i][j] = 1023;
        }
      }

why not use:

memset(shortVal, 2 * 40 * sizeof(int), 0);
memset(reflexVal, 2 * 500 * sizeof(int), 1023);

Not only would it be marginally faster, it should save a few bytes of memory for you, too. Obviously, you’d do the math for 240 and 2500…just wanted to show where the numbers came from.

mrjack:
Hiya folks,

Your help would be much appreciated on this problem…

 for(int i=0;i<2;i++)

Ooops! Ignore my brainfart.

Henry_Best:

mrjack:
Hiya folks,

Your help would be much appreciated on this problem…

 for(int i=0;i<2;i++)

You’re setting i = 0 and then telling the for loop to stop if i<2. 0 is always less than 2, (well, it was when I went to school :)) so it won’t do anything!

No. The loop is executed while the expression is true, and it will be true for 0 and 1.

Henry_Best:
when I went to school ...

Back to school for you! :stuck_out_tongue:

The code you're quoting is a perfectly ordinary use of a for loop.

Great tips on the memory front guys.. thanks a lot.

So I reduced the size of the large array to 250 and that made all the difference.

Cheers folks!

Try the Arduino Enhanced IDE, it will report memory usage and show a message if you used too much memory :slight_smile: