Serial Monitor no longer functions for previously working scripts

Hello,
I am having a problem where multiple scripts that previously worked in showing up on the serial monitor no longer start functioning (water should be released upon startup) or showing up on the serial monitor, despite getting "Done Uploading". Main Script Below:

//All of the baseline stuff - probably extra but makes it easier
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <TrueRandom.h>
#include <elapsedMillis.h>
#include <Average.h>
#define PI 3.1415926535897932384626433832795
Adafruit_MCP4725 dac;
elapsedMillis timeElapsed;

//  Pin assignments
int H2OL = 7;
int H2OR = 6;
int odorAirSupply = 4;             //"hem" in taskdata     ,     First valve - supplies air to others
int odorAB = 11; //LOW=A;HIGH=B     //"stim" in taskdata      ,     Switches between the two odors within trial
int odorPair = 10;  //Low = AB, HIGH = CD
int odorCD = 8;
int speakerR = 22;
int speakerL = 23;

//  Variables
int delayTimeL = 50; //35; //open time on left solenoid
int delayTimeR = 50; //35; //open time on right solenoid
int waiting = 0;
int LwaterGood = 0;
int RwaterGood = 0;
int Airsupplygood = 0;
int ABvalvegood = 0;
int odorPairValveGood = 0;
int CDvalvegood = 0;
int LspeakerGood = 0;
int RspeakerGood = 0;
int lickedR = 0;
int lickedL = 0;
int lickNumR = 0;
int lickNumL = 0;
int RcircuitGood = 0;
int LcircuitGood = 0;
float lickThresh = 60;                  //threshold voltage difference for lick detection
int lickPrev = 0;
Average<float> lickAveR(3);     //initialize a moving window for detecting right licks
Average<float> lickAveL(3);     //initialize a moving window for detecting left licks
int i;
int f = 155;        //wave frequency
long i2c = 3120;      //i2c clk rate
int bit_depth = 12;
int norm_weight = pow(2,bit_depth);   //signal multiplier based on bit_depth
float sig;  //normalized wave signal
float wave_steps = (i2c/f);
float final_sig;


void setup() {
    //Set up of pin assignments etc.
  pinMode(H2OL, OUTPUT);
  pinMode(H2OR, OUTPUT); 
  pinMode(odorAirSupply, OUTPUT);digitalWrite(odorAirSupply, LOW);
  pinMode(odorAB, OUTPUT); digitalWrite(odorAB, LOW); //Low=A,High=B;
  pinMode(odorPair, OUTPUT);digitalWrite(odorPair, LOW);
  pinMode(odorCD,OUTPUT);digitalWrite(odorCD, LOW);
  Serial.begin(9600);
  Wire.begin();
  Wire.setClock(400000);  // Set the SCL clock speed to 400kHz
  dac.begin(0x62);
  pinMode(speakerL, OUTPUT); digitalWrite(speakerL, LOW);
  pinMode(speakerR, OUTPUT); digitalWrite(speakerR, LOW);
}

void loop() {
    //            Checking water valves:
  Serial.println("Begin");
  digitalWrite(H2OL,HIGH);
  delay(delayTimeL);
  digitalWrite(H2OL,LOW);
  Serial.println("Water in Left lick spout? [1=yes, 2=try again]");
  waiting = 1;
    while (waiting == 1) {
      if (Serial.available() > 0) {    // is a character available?
        LwaterGood = Serial.parseInt();       // get the character
        if (LwaterGood == 1) {
          Serial.println("The left lick spout is ready to go!");
          waiting = 2;
        }
        if (LwaterGood == 2) {
          digitalWrite(H2OL,HIGH);
          delay(delayTimeL);
          digitalWrite(H2OL,LOW);
          LwaterGood = 0;
    }
    }
  }
  if (LwaterGood == 1) {
    digitalWrite(H2OR,HIGH);
    delay(delayTimeR);
    digitalWrite(H2OR,LOW);
    Serial.println("Water in Right lick spout? [1=yes, 2=try again]");
    while (waiting == 2) {
      if (Serial.available() > 0) {    // is a character available?
        RwaterGood = Serial.parseInt();       // get the character
        if (RwaterGood == 1) {
          Serial.println("The right lick spout is ready to go!");
          waiting = 3;
        }
        if (RwaterGood == 2) {
          digitalWrite(H2OR,HIGH);
          delay(delayTimeR);
          digitalWrite(H2OR,LOW);
          RwaterGood = 0;
        }
    }
  }
  }
  
//           Checking air valves

  if (RwaterGood == 1) {
  Serial.println("Water distribution is good to go!");
  Serial.println("Do you smell odor A? [1=Yes, 2=return valve for 10 seconds]");
    while (waiting == 3) {
      digitalWrite(odorAirSupply, HIGH);
      if (Serial.available() > 0) {    // is a character available?
        Airsupplygood = Serial.parseInt();       // get the character
        if (Airsupplygood == 1) {
          Serial.println("The A/B odor valve is good!");
          waiting = 4;
        }
        if (Airsupplygood == 2) {
          digitalWrite(odorAirSupply,LOW);
          delay(10000);
          Airsupplygood = 0;
  }
      }
    }
}
if (Airsupplygood == 1) {
  Serial.println("Do you smell odor B? [1=yes, 2=return valve for 10 seconds]");
    while (waiting == 4) {
      digitalWrite(odorAirSupply, HIGH);
      digitalWrite(odorAB,HIGH);
      if (Serial.available() > 0) {    // is a character available?
        ABvalvegood = Serial.parseInt();       // get the character
        if (ABvalvegood == 1) {
          Serial.println("The A/B valve is good!");
          digitalWrite(odorAB,LOW);
          digitalWrite(odorAirSupply, LOW);
          waiting = 5;
        }
        if (ABvalvegood == 2) {
          digitalWrite(odorAB,LOW);
          delay(10000);
          Airsupplygood = 0;
}
      }
    }
}

if (ABvalvegood == 1) {
  Serial.println("Do you smell odor C? [1=yes, 2=return valve for 10 seconds]");
    while (waiting == 5) {
      digitalWrite(odorAirSupply, HIGH);
      digitalWrite(odorPair, HIGH);
      digitalWrite(odorCD,LOW);
      if (Serial.available() > 0) {    // is a character available?
        odorPairValveGood = Serial.parseInt();       // get the character
        if (odorPairValveGood == 1) {
          Serial.println("The odorPair valve is good!");
          digitalWrite(odorCD,LOW);
          digitalWrite(odorPair, LOW);
          digitalWrite(odorAirSupply, LOW);
          waiting = 6;
        }
        if (odorPairValveGood == 2) {
          digitalWrite(odorPair, LOW);
          delay(10000);
          Airsupplygood = 0;
}
      }
    }
}

if (odorPairValveGood == 1) {
  Serial.println("Do you smell odor D? [1=yes, 2=return valve for 10 seconds]");
    while (waiting == 6) {
      digitalWrite(odorAirSupply, HIGH);
      digitalWrite(odorPair, HIGH);
      digitalWrite(odorCD,HIGH);
      if (Serial.available() > 0) {    // is a character available?
        CDvalvegood = Serial.parseInt();       // get the character
        if (CDvalvegood == 1) {
          Serial.println("The C/D valve is good!");
          digitalWrite(odorCD,LOW);
          digitalWrite(odorPair, LOW);
          digitalWrite(odorAirSupply, LOW);
          waiting = 7;
        }
        if (CDvalvegood == 2) {
          digitalWrite(odorCD,LOW);
          delay(10000);
          Airsupplygood = 0;
}
      }
    }
}

      // Checking lick detection


if (CDvalvegood == 1) {
  Serial.println("Connect aluminum tube to RIGHT lick spout");
  while (waiting == 7) {
    lickAveR.push(analogRead(A5) - analogRead(A0));   //Because of sliding window next 2 runs through loop LickAveR.mean will > lickThresh
    if (lickAveR.mean() > lickThresh) {    // is a character available?
      Serial.println("Right spout can sense licks");
      RcircuitGood = 1;
      waiting = 8;
    }
  }
}

// Make mean values for L and R lick spout voltages include values without connectivity
// Otherwise the mean for lickAveL will include one or more values from connection with right spout
delay(1000);

if (RcircuitGood == 1){
  Serial.println("Connect aluminum tube to LEFT lick spout");
  while (waiting == 8) {
    lickAveL.push(analogRead(A0) - analogRead(A5));   //Because of sliding window next 2 runs through loop LickAveL.mean will > lickThresh
    if (lickAveL.mean() > lickThresh) {
      Serial.println("Left spout can sense licks");
      LcircuitGood = 1;
      waiting = 9;
    }
  }
}


    //Checking Speakers

if (LcircuitGood ==1){
  Serial.println("Do you hear a tone in the left speaker?  [1=yes, 2=switch speakers for 10s]");
  digitalWrite(speakerL, HIGH);
  while (waiting == 9){
    timeElapsed = 0;
    while (timeElapsed <= 2500){
      for  (i = 1; i < wave_steps; i++){
        sig = (norm_weight/2)*(1+sin((2*PI)*(i/wave_steps)));
        final_sig = sig*0.5;
        dac.setVoltage(final_sig, false);
      }
    }
    if (Serial.available() > 0) {    // is a character available?
      LspeakerGood = Serial.parseInt();       // get the character
      if (LspeakerGood == 1){
        digitalWrite(speakerL, LOW);
        dac.setVoltage(0,false);
        waiting = 10;
      }
      if (LspeakerGood ==2){
        digitalWrite(speakerL, LOW);
        digitalWrite(speakerR, HIGH);
        delay(10000);
        digitalWrite(speakerR, LOW);
        digitalWrite(speakerL, HIGH);
  }
}
  }
}

if (LspeakerGood ==1){
  Serial.println("Do you hear a tone in the right speaker?  [1=yes, 2=switch speakers for 10s]");
  digitalWrite(speakerR, HIGH);
  while (waiting == 10){
    timeElapsed = 0;
    while (timeElapsed <= 2500){
      for  (i = 1; i < wave_steps; i++){
        sig = (norm_weight/2)*(1+sin((2*PI)*(i/wave_steps)));
        final_sig = sig*0.5;
        dac.setVoltage(final_sig, false);
      }
    }
    if (Serial.available() > 0) {    // is a character available?
      RspeakerGood = Serial.parseInt();       // get the character
      if (RspeakerGood == 1){
        digitalWrite(speakerR, LOW);
        dac.setVoltage(0,false);
        waiting = 11;
      }
      if (RspeakerGood ==2){
        digitalWrite(speakerR, LOW);
        digitalWrite(speakerL, HIGH);
        delay(10000);
        digitalWrite(speakerL, LOW);
        digitalWrite(speakerR, HIGH);
  }
}
  }
}

if (RspeakerGood == 1){
  Serial.println("Input the frequnecy [in Hz] of the desired tone");
  digitalWrite(speakerR, HIGH);
  digitalWrite(speakerL, HIGH);
  while (waiting == 11){
    if (Serial.available() > 0) {    // is a character available?
      f = Serial.parseInt();       // get the character
      float sig;  //normalized wave signal
      float wave_steps = (i2c/f);
      float final_sig;
      int norm_weight = pow(2,bit_depth);   //signal multiplier based on bit_depth
    timeElapsed = 0;
    while (timeElapsed <= 2500){
      for  (i = 1; i < wave_steps; i++){
        sig = (norm_weight/2)*(1+sin((2*PI)*(i/wave_steps)));
        final_sig = sig*0.5;
        dac.setVoltage(final_sig, false);
      
}
}
}
}
}
}

The correct COM port is in, and I have checked to make sure that the code is no different than it has been previously for some reason. I have also confirmed that the Blink example sketch works. Additionally, I have run simpler scripts that require the serial monitor but fewer of the pins relative to the above script and the serial monitor displays fine. I figured that perhaps the 12V connection wasn't working and therefore not providing enough power, but when I unplugged the USB cable the arduino's light remained on with the 12V plug in. I just replaced the Mega board with a new one and am having the same issues. Please advise or let me know if you need additional information

Does it still not work if you disconnect everything connected to the mega?

What have you got the baud rate set to in the Serial monitor ?

Which Arduino board are you using ?

What devices have you got connected to the board and which pins are they using ?

Is there more code than you have posted ?

If I unplug the SDA and SCL pins it shows up on the serial monitor, though I need these to run my rig properly

The baud rate is 9600. I'm using a Mega 2560. I have a shield that makes it a big tricky but the code outlines what pins I'm using. I have 2 2-way valves, 4 3-way valves, 7 pins to another arduino Mega 2560 (working fine), the SDA and SCL pins to a DAC on a breadboard, and a couple pins that go to an 8 pin switch also on the breadboard. I then use several of the 5V and GND connections to run things like the breadboard or supply power that then feeds back into the analog readout pins to check how much power is deviated when another connection is made to GND along that path

Ok, so it would seem that we can rule out the sketches themselves per se.

That would appear to confirm the USB cable, serial port and IDE setup are working fine.

I assume that to mean that you are using the same baudrate throughout?

Which makes me wonder whether execution is actually reaching the first two Serial.println() statements at the beginning of void loop()?. Do all of the scripts you refer to in the opening post use the dac and Wire? In which case, might the board be crashing or hanging for some reason before it reaches void loop()? ? You could try commenting out the following:

Adafruit_MCP4725 dac;

and

dac.begin(0x62);

then try again. Does it now print anything to the serial monitor??
If not then also try commenting out :

Wire.begin();`
Wire.setClock(400000);  // Set the SCL clock speed to 400kHz

Any difference? What about if you comment out the individual pin digitalWrite statements?

Hmm...take a look at this. How far does your sketch get before it doesn't work?
https://docs.arduino.cc/language-reference/en/functions/communication/wire/setWireTimeout/

Commenting out the dac lines worked to get the text working for the serial monitor! This makes sense now and I'm realizing that this is the difference between the sketches that work and didn't work. There's still the trouble of how to get it to run properly, and I'm puzzled since I have 5 more of these rigs that are all configured the same, running on the same code, and work fine. I'm guessing it's a hardware issue then. Any more thoughts would be super helpful but I understand if we're outside of the scope of this forum. Thanks!

I would guess its the .begin() statement that's causing the code execution to hang for some reason when the dac is initialised. You can probably add the first line back in (the one before setup() ) and it will still print.

The question is whether this is due to a dac library change, a wiring problem or a fault with the dac.

Do you know whether the library has been updated since the last time you uploaded the sketch to the board? Is the code on the other 5 rigs compiled with the same version of the dac library? If its been recently updated, then you can revert to a previous version and see whether that works.

Obviously check any wiring between Mega and dac and whether there is power to the dac.

Do you have another dac to try? If you swap it out and it works with the replacement, then you know its a fault on the dac hardware.

When checking the wiring I noticed that the power light on the DAC was very weak so I've replaced it with an extra and it appears to be working now. Thank you so much for all of your help!

Glad you found the problem and got it working again.

Evidently the power failure affected communication between the dac and the Mega. Its just unfortunate that in this instance it prevented the completion of the setup() routine rather than just timing out and moving on, which might have given a more obvious indication such as a corrupt or missing reading from the dac.

There is no "Done Uploading" in your script.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.