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 = "";
}
}
}