Code hanging when asking too much Serial information?

Hi there,

I have expanded on the dashboard program provided by Jonathan Clark (http://www.lvl1.org/2011/07/04/arduino-dashboard-app/) so that it can run on an Arduino Mega. As expected, this means sending a lot more information out via the Serial port to the computer.

My program is running a few calculations, and outputs a PWM signal.

Every time I run the code, the code starts off by giving me that PWM, but once the code hits this bit, where I call the dashboard() function, the PWM signal stops working. However, the dashboard () feedback is operating.

I tried adding delays, and in my latest try, I asked the Arduino to only run it once every 10 seconds, but even then the code works fine till the unit calls this method, and then the PWM signal disappears.

Can anybody tell me what I am doing wrong? I am using pins 2 and 3 for the PWM generation, and other inputs to do other stuff.

Thank you :slight_smile:

// Calling the code every 10 seconds instead of all the time in LOOP():
if (millis()- time > 1000){
    x++;
    time = millis();
    if ( x > 10) {
      dashboard();
      x = 0;
    }
    
  }
  

// The dashboard method! 
void dashboard() {
  //long time = millis();
  
  int analog[16];
  int digital[54];
  for (int x = 0; x < 16; x++){
    analog[x] = analogRead(x);
  }
  for (int x = 0; x < 54; x++){
    digital[x] = digitalRead(x);
  }
    for (int x = 0; x < 16; x++){
      Serial.print(analog[x],DEC);
      Serial.print(",");
    }
    for (int x = 0; x < 54; x++){
      Serial.print(digital[x],DEC);
      Serial.print(",");
    }  
    
    Serial.print("!");
   return;
  
}

Do you really have something connected to all 16 analog pins? Do you really have something connected to all 54 digital pins? Don't you suppose that reading the serial pins 0 and 1 might interfere with outputting data on the serial port?

What do you suppose happens when you read from the pin you are doing PWM on. Hint: PWM stops.

Interesting ... yeah I was thinking along the same lines but wanted to confirm...

I'll play a bit more with the code.

Yes I do not have everything connected to all the pins, but I wanted to make one universal program for all my applications.

So I have tried to not probe the PWM pins, and ignore pins 0 and 1 as well when sending the Serial information out. The problem still remains. Any other ideas? Or is this a no?

So I have tried to not probe the PWM pins, and ignore pins 0 and 1 as well when sending the Serial information out. The problem still remains. Any other ideas?

I modified the code, and I'm not going to show you the new code. Help me, anyway.

It doesn't work very well that way.

made some more changes, and as per your advice, this time, not probing the pwm actually works and the dashboard functionality works as well.

This is my modified code:

void dashboard() {
  //long time = millis();
  
  int analog[16];
  int digital[54];
  for (int x = 0; x < 16; x++){
    analog[x] = analogRead(x);
  }
  for (int x = 0; x < 54; x++){
    if (x==2 || x==6)       digital[x] = 1; // << NOT PROBING PWM PINS
    else digital[x] = digitalRead(x);
  }
    for (int x = 0; x < 16; x++){
      Serial.print(analog[x],DEC);
      Serial.print(",");
    }
    for (int x = 0; x < 54; x++){

      Serial.print(digital[x],DEC);

      Serial.print(",");
    }  
    
    Serial.print("!");
   return;
  
}
  for (int x = 0; x < 54; x++){
    if (x==2 || x==6)       digital[x] = 1; // << NOT PROBING PWM PINS
    else digital[x] = digitalRead(x);
  }

You are still reading the serial pins.

Yes I am. I ran the code with removing them, and also without them. Seems to work at the moment.

However, based on your experience and knowledge, would you recommend I do not probe them either?

like such:

  for (int x = 0; x < 54; x++){
    // Ignore any PWM outputs, and Serial inputs 0 and 1
    if (x==0 || x==1 || x==2 || x==6)       digital[x] = 1;
    //Probe everything else:
    else digital[x] = digitalRead(x);
  }

would you recommend I do not probe them either?

Yes, I would. You can't use them for anything else, so probing them, even if it doesn't cause problems, does not give you any useful information.

Probing a pin with nothing connected to it doesn't give you any useful information, either.

Reply #1 questions were never answered. Maybe F-A is connected.

Maybe F-A is connected.

???

Paul, thanks for your help and advice. Fairly sensible stuff!

Go-for-smoke, yeah what is F-A?

As in Sweet F-A.