SOLVED!!! - Arduino UNO suddenly very slow

Hello!!

I've been working on a capacitive sensor project. Everything was working great, until all of the sudden, when I plug in my Arduino, everything is very slow. The serial monitor, which was previously printing values very quickly, started only printing every two to four seconds. Also, the Arduino is being just as slow and unresponsive. I had made no changes to the code - I just plugged it in to run some more tests.

I have tried unplugging it, restarting the Arduino software, and modifying the code - none have worked

If you know what could be causing this problem, please let me know.

Thanks!!

Start by posting your code

The code shouldn't be the issue because it was working just fine, but here it is anyways.

#include <CapacitiveSensor.h>
CapacitiveSensor blackSensor = CapacitiveSensor(10, 12);
CapacitiveSensor redSensor = CapacitiveSensor(7, 8);

int thresholdBlack = 85;
int thresholdRed = 70;
const int redLedPin = 3;
const int whiteLedPin = 2;

void setup() {
  Serial.begin(9600);
  pinMode(redLedPin, OUTPUT);
  pinMode(whiteLedPin, OUTPUT);
}

void loop() {
  long blackValue = blackSensor.capacitiveSensor(15);
  long redValue = redSensor.capacitiveSensor(15);
  
  Serial.println((String)"Black:"+blackValue+" Red:"+redValue);
  

  if (blackValue > thresholdBlack) {
    digitalWrite(whiteLedPin, HIGH);
  } else {
    digitalWrite(whiteLedPin, LOW);
  }

  if (redValue > thresholdRed) {
    digitalWrite(redLedPin, HIGH);
  } else {
    digitalWrite(redLedPin, LOW);
  }
 
  delay(10);
}

Never mind, guys!!! Turns out, I plugged in a wire to the wrong digital pin!! When I plugged the wire into the right pin, the problem immediately stopped!

Sorry guys!!

I would theorize that the cap sensing library is putting an interrupt on a pin, and without the correct connections, the interrupt wound up firing far faster than it does during normal operation and it was spending all it's time in the ISR.

As an aside... (unrelated to your issue above, but this will complicate things you do with that code in the future, and points to a your not recognizing a common pitfall with serial debugging)

You have Serial configured for 9600 baud (why the hell is this still everyone's "default" baud rate?! It's slow as hell!)

You are printing a string that's something like 15 bytes long, with a delay(10) between each attempt to print it.

9600 baud prints 1 byte in just over 1ms. (10/9.6 ms - 8 data bits plus start and stop bits). So every pass through loop you add around 5 more bytes to the serial buffer than you print. After around a dozen passes through loop, the 64-byte serial buffer is now full (note that you now also have 4 rounds of data backed up in the buffer too). At that point, Serial.print() becomes blocking - it will wait until enough space has been made (through data being sent) to fit what you are printing into the buffer - so it'll take 5-6ms on that step after you've filled the buffer.

You want to avoid printing stuff to serial faster than you're sending it out for this reason; if nothing else, it will make the output you see less likely to lead you astray. In your case, simply cranking up the serial baud rate (or increasing the delay - maybe to 20ms) would solve this. If that's an Uno, I'd just run it at 115200.

On classic AVRs, 115200 baud works fine at 16 MHz (but not at 8 IIRC) - 57600 works at 8 on classic AVRs, and 115200 works on modern AVRs all the way down to 1 MHz (2 if they don't have the version of UART.cpp that uses U2X mode where appropriate)

1 Like

wow...

I had no idea! Thank you so much for telling me this!