Helping with the code

I have two devices with two different codes. With the help from this forum and I was able to combine the two codes and it is working. However, I noticed that if device 2 (code 2 here) is not connected, then device 1 (code 1) does not work. There is a flow, I believe, still unable to figure it out why this is happening. Would appreciate your help.

/

/ #include <avr/wdt.h>
#include <Adafruit_MCP4728.h>

int voltageLevel= 0;
String inString = "";    // string to hold input
Adafruit_MCP4728 mcp; // need to setup MCP4728

char inChar; // variable to receive data from the serial port
int pin2 = 2; // pin 2 for  ON/OFF 
int pin3 = 3; // pin 2 for Pairing
int pin4 = 4; // pin 4 for (E2)
int pin5 = 5; // pin 5 for (E4)
int pin6 = 6; // pin 6 for (E7)
int pin7 = 7; // pin 7 for (E8)
int pin8 = 8; // pin 8 for (E9)

void setup() {
 
  Serial.begin(115200);

  if (!mcp.begin()) {
    Serial.println("MCP4728 Not Found");
   while (1) {
      delay(10);
    }
  }
   Serial.println("MCP4728 Found!");
  
  pinMode(pin2, OUTPUT); //   pin 2 as OUTPUT,Relay1 channel input1
  digitalWrite(pin2, HIGH);// Making input1 of 2 channel relay1 OFF initially
  pinMode(pin3, OUTPUT); //   pin 3 as OUTPUT, Relay1 channel input2
  digitalWrite(pin3, HIGH);// Making input2 of 2 channel relay1 OFF initially
  pinMode(pin4, OUTPUT); //   pin 4 as OUTPUT, Relay2 channel2 input1
  digitalWrite(pin4, LOW);// Making input1 of of 2 channel relay2 ON at Start 
  pinMode(pin5, OUTPUT); //   pin 5 as OUTPUT, Relay2 channel1 input2
  digitalWrite(pin5, LOW);// Making input2 of of 2 channel relay2 ON at Start 
  pinMode(pin6, OUTPUT); //   pin 6 as OUTPUT, Relay3 channel1 input1
  digitalWrite(pin6, LOW);// Making input1 of of 2 channel relay3 ON at Start 
  pinMode(pin7, OUTPUT); //   pin 7 as OUTPUT, Relay3 channel1 input2
  digitalWrite(pin7, LOW);// Making input2 of of 2 channel relay3 ON at Start 
  pinMode(pin8, OUTPUT); //   pin 8 as OUTPUT, Relay4 channel1 input1
  digitalWrite(pin8, LOW);// Making input1 of of 2 channel relay4 ON at Start 

  
}
void loop() {
     
  if (Serial.available()>=0){   // read the oldest byte in the serial buffer:
    
    inChar = Serial.read();    
     
          // Code 1
           
          if(inChar == 'A' ) // if 'A' is received
          {
          digitalWrite(pin2, LOW); // turn ON (input 1 of relay)
          }              
          if(inChar == 'B')// if 'B is received 
          {
          digitalWrite(pin2, HIGH); // turn OFF
          } 
          // Pairing/Unpairing
          if(inChar == 'C' ) // if 'C' is received
          {
          digitalWrite(pin3, LOW); // turn ON (input 2 of relay1)
          delay (15);
          digitalWrite(pin3, HIGH);
          delay (15);
          digitalWrite(pin3, LOW); // turn ON (input 2 of relay1)
          delay (15);
          digitalWrite(pin3, HIGH);
          delay (15);
          digitalWrite(pin3, HIGH);
          }                
          // (E2)
          if(inChar == 'D' ) // if 'D' is received
          {
          digitalWrite(pin4, HIGH); // turn OFF Fan(input 1 of relay2)
          delay (45000);            // Fan OFF for 40seconds
          digitalWrite(pin4, LOW);  // Fan ON after 40 seconds
          }
          // (E4)
          if(inChar == 'E' ) // if 'E' is received
          {
          digitalWrite(pin5, HIGH); // turn OFF Communication(input 2 of relay2)
          delay (40000);            // Communication OFF for 40seconds
          digitalWrite(pin5, LOW);  // Communication ON after 40 seconds
          }
          // (E7)
          if(inChar == 'F' ) // if 'F' is received
          {
          digitalWrite(pin6, HIGH); // turn OFF Motor(input 1 of relay3)
          delay (32000);            // Motor OFF for 32seconds
          digitalWrite(pin6, LOW);  // Motor ON after 32 seconds
          }
          // (E8)
          if(inChar == 'G' ) // if 'F' is received
          {
          digitalWrite(pin7, HIGH); // Disconnect Thermocouple (input 2 of relay3)
          delay (10000);            // Motor OFF for 10seconds
          digitalWrite(pin7, LOW);  // Motor ON after 10 seconds
          }
          // Error (E9)
          if(inChar == 'H' ) // if 'F' is received
          {
          digitalWrite(pin8, HIGH); // Disconnect Sensor (input 1 of relay4)
          delay (70000);            // Sensor OFF for 70seconds
          digitalWrite(pin8, LOW);  // Sensor ON after 70 seconds
          }
                     
        //Code 2
    if(isDigit(inChar)){
            // convert the incoming byte to a char and add it to the string:
            inString += (char)inChar; 
            
      }             
      // Cavity Temp Code
      if (inChar == '\n') {
      voltageLevel = inString.toInt();         
      mcp.setChannelValue(MCP4728_CHANNEL_A, voltageLevel);

                Serial.print("VoltageLevel:");
                Serial.print(voltageLevel);
                Serial.println(". ");
                // clear the string for new input:
                inString = "";
      
       }      
                 
    }

 }

jag68:
I have two devices with two different codes. With the help from this forum and I was able to combine the two codes and it is working. However, I noticed that if device 2 (code 2 here) is not connected, then device 1 (code 1) does not work. There is a flow, I believe, still unable to figure it out why this is happening. Would appreciate your help.

I'm not sure what you mean by "I noticed that if device 2 (code 2 here) is not connected, then device 1 (code 1) does not work."

Here is one problem:

  if (Serial.available()>=0)

This should be:

  if (Serial.available()>0)

Otherwise Serial.read() will return -1 if there are no characters to read from the serial.

Hi Todd, thanks for your response!

Regarding device not connected, I meant, when I don't connect the device 2 (a MCP4728) to the arduino, the code 1 does not work.

I changed the if (Serial.available(), as you advised. But it is the same issue

I noticed that if device 2 (code 2 here) is not connected, then device 1 (code 1) does not work. There is a flow, I believe, still unable to figure it out why this is happening.

It is happening because you enter an endless do-nothing loop if the first one can't be detected:

  if (!mcp.begin()) {
    Serial.println("MCP4728 Not Found");
   while (1) {
      delay(10);
    }

You need to build yourself an initialization that is error tolerant. Also, who wrote the code? It's pretty silly to have a delay() call in there, what is the point?

Hi aarg,

Thank you very much for your response. The code is from the manufacturer of mcp4728 board (Adafruit)

jag68:
Hi aarg,

Thank you very much for your response. The code is from the manufacturer of mcp4728 board (Adafruit)

Arduino | Adafruit MCP4728 I2C Quad DAC | Adafruit Learning System

Yeah, unfortunately coding at a lot of places like that falls on the intern. Anyhoo, you just need to deal with the failed initialization in a way that doesn't just freeze the program on purpose.

Hi aarag,

I deleted the mcp4728 found codes:

if (!mcp.begin()) {
    Serial.println("MCP4728 Not Found");
   while (1) {
      delay(10);
    }
  }
   Serial.println("MCP4728 Found!");

The code 1 (device 1) is working now, without mcp4728 device connected. Previously the code 1(device 1) was not working as I disconnected the MCMP4728 device (device #2).

Thank you VERY much for your guidance! Hope it resolved the issue

I now changed the if.. while to if else,

if (!mcp.begin()) {
    Serial.println("MCP4728 Not Found");      
    }
  
   else {
    Serial.println("MCP4728 Found!");
   }

Now there is no infinite loop as the code will look only at the beginning for mcp4728 board.

It is OK now.

Thanks once again to every one! Great Forum!