Cap touch + Sensor + Servo is setting all my pins outputting high


I’m new to Arduino and I’m trying to debug a problem with a sketch. I want to trigger a servo to raise once a timer has elapsed and lower the servo when a cap touch button has reached a threshold value.

I have gotten the servo to be triggered by the timer - and I can trigger it with the cap_touch sensor, but when I combine these elements together in a sketch it sets all my pins outputting HIGH - or high numeric values for the analogue pins.

I can run blink and I can manually set all the pins low in a sketch - but any sketch that does not address each individual pin automatically outputs high on all pins.

I have eliminated my computer, my USB cable, the Arduino software, and the board itself as the problem by trying different boards, cables and computers, and reinstalling Arduino/using Arduino create.

Does anyone know why this combination is doing this? Or how to save my boards? They occasionally seem to “forget” what’s happened and start working normally again and I’m just stumped!

#include <Servo.h> // include servo library
#include <CapacitiveSensor.h> // include cap touch library

CapacitiveSensor capSensor = CapacitiveSensor(4, 2); //create a cap sensor at pins (4,2)
Servo servo; // create a servo called servo

unsigned long startMillis; // create a long var to store start time
unsigned long currentMillis; // create a long var to store running time
const unsigned long period = 10000; // set a time period

int threshold = 500; // sensor value threshold to trigger if statement
int servoPin = 9; //set servo at pin 9
int angle = 20; // set an int for the angle of the servo

void setup() {
  Serial.begin(9600); // start serial
  startMillis = millis();  //initial start time
  servo.attach(servoPin); //attach servo at pin 9
  servo.write(0);} //set servo to position 0

void loop() {
  currentMillis = millis(); //tracking time as the sketch runs
  long sensorValue = capSensor.capacitiveSensor(30); //store sensor value as long var sensorValue

  if (currentMillis - startMillis >= period) { //if current time - start time > than period/has period ellapsed
    servo.write(180);} //set servo to 180

  if (sensorValue > threshold) { //if sensorValue > threshold
    servo.write(0);}} //set servo to 180

Thank you!

  servo.write(0);} //set servo to position 0

It is STUPID to hide the end of the function this way. Put the } on a line BY ITSELF.

  startMillis = millis();  //initial start time

The start of what? The variable name should be meaningful.

    servo.write(0);}} //set servo to 180

There should rarely be anything on the line with the }. When the } is the end of the function, that would be NEVER.

but any sketch that does not address each individual pin automatically outputs high on all pins.

There is something wrong with how you determine this, then, as pins a INPUT and LOW by default.