[Solved] Sorry, it is another Serial that stops to output ....

Hi guys, I am desperate about this issue. I searched for this kind of problem in this forum and on Google. I've read several topics about this, but nothing...

My arduino is a UNO and I am using it to control some 8 valves when the computer request it. The computer sends a message (RS232) as "Vn=vX" where V is a command for valve, n is the valve number, v is "1" or "0" or "?" that means to open or close the valve or request the state of the n-th valve respectively, X is a end of string char control. When arduino receives this string it executes the code to change the state of the valve and completes the action by sending back to the computer a string as "V8=bbbbbbbbX where b represents the bits on/off for the eight valves

For up to the 5th or 6th request, I receive the response from arduino, everything is fine, but suddenly arduino stops to send data. If I stop my program on the computer, closing the comm port, and open the serial monitor on Arduino IDE, I also do not have any response, although arduino still execute the tasks, in other words: arduino is running, receiving data from the serial, but nothing is received from arduino, nothing comes out from arduino serial.

I will post below my code:

//
// Version 12-12-17


#include <Adafruit_ADS1015.h>
#include "PCF8574.h"
#include <Wire.h>

Adafruit_ADS1115 ads1115(0x49);
PCF8574 PCF_38(0x38);
PCF8574 PCF_39(0x39);  // (used as output)

uint8_t intValveStates;

char commandSerial[10];
char readString[50];

const int SET_VALVE_ON = 1;

const int SET_VALVE_OFF = 0;


String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete


String DecToBin(uint8_t mynum) {

  int zeros = 8 - String(mynum, BIN).length();
  String myStr;
  for (int i = 0; i < zeros; i++) {
    myStr = myStr + "0";
  }
  myStr = myStr + String(mynum, BIN);
  return myStr;
}

void ParseString(String StringToParse, String & strCommand, String & strValve, String & strParameter ) {
  char *ptr;
  int limit = strlen(StringToParse.c_str());
  if (limit == 1) {
    strCommand = StringToParse[0];
    strValve = "";
    strParameter = "";
  }
  if (limit > 3) {
    strCommand = StringToParse[0];
    strValve = StringToParse[1];
    strParameter = StringToParse[3];
  }
}

void SetValve(int intValve, int intState) {

  if (intState == SET_VALVE_ON)  {
    if (intValve == 0) {
      intValveStates = (intValveStates ^ (intValveStates & 1));
    }
    if (intValve == 1) {
      intValveStates = (intValveStates ^ (intValveStates & 2));
    }
    if (intValve == 2) {
      intValveStates = (intValveStates ^ (intValveStates & 4));
    }

    if (intValve == 3) {
      intValveStates = (intValveStates ^ (intValveStates & 8));
    }

    if (intValve == 4) {
      intValveStates = (intValveStates ^ (intValveStates & 16));
    }

    if (intValve == 5) {
      intValveStates = (intValveStates ^ (intValveStates & 32));
    }

    if (intValve == 6) {
      intValveStates = (intValveStates ^ (intValveStates & 64));
    }

    if (intValve == 7) {
      intValveStates = (intValveStates ^ (intValveStates & 128));
    }

    if (intValve == 8) {
      intValveStates = 0;
    }

    PCF_38.write8(intValveStates);

  }

  if (intState == SET_VALVE_OFF) {
    if (intValve == 0) {
      intValveStates = ((intValveStates | 1));
    }
    if (intValve == 1) {
      intValveStates = ((intValveStates | 2)) ;
    }
    if (intValve == 2) {
      intValveStates = ((intValveStates | 4)) ;
    }

    if (intValve == 3) {
      intValveStates = ((intValveStates | 8)) ;
    }

    if (intValve == 4) {
      intValveStates = ((intValveStates | 16)) ;
    }

    if (intValve == 5) {
      intValveStates = ((intValveStates | 32)) ;
    }

    if (intValve == 6) {
      intValveStates = ((intValveStates | 64)) ;
    }

    if (intValve == 7) {
      intValveStates = (intValveStates | 128) ;
    }

    if (intValve == 8) {
      intValveStates = 255;
    }

    PCF_38.write8(intValveStates);
  }

}



void softReset() {
  asm volatile ("  jmp 0");
}


void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);


  PCF_38.write8(255);   // defaut all valves in off
  ads1115.begin();
  ads1115.setGain(GAIN_TWOTHIRDS); // 6.144V max
  intValveStates = 255;
  PCF_38.write8(intValveStates);
  uint8_t value = PCF_38.read8();
  intValveStates = value;
}


void loop() {
  String Command;
  String Valve;
  String Parameter;
  int intValveToset;
  int intStateToset;
  String strStateValve;
  String ReplyMsg;
  int16_t adcCh;
  float VoltCh;
  String StrVolt;
  char *charVolt;

  // print the string when a newline arrives:
  if (stringComplete) {
    //Serial.println(inputString);
    ParseString(inputString, Command, Valve, Parameter);
    int iValve = atoi(Valve.c_str());

    if  ((Command == "v") || (Command == "V")) {
      // User wants to change valve state
      // Format: Vn=mX
      // n is the Valve number, m is 0=off  1=on, ?=read state, X=EndOfTransmit


      if ((Valve == "8") && (Parameter == "?")) {
        ReplyMsg = "V8=" + DecToBin(~intValveStates) + "X";
        Serial.println(ReplyMsg);
      }

      if ((iValve < 8) && (Parameter == "?")) {

        int iStateValve = bitRead(intValveStates, iValve);
        if (iStateValve == 0) {
          strStateValve = "1";
        }
        else {
          strStateValve = "0";
        }
        ReplyMsg = Command + Valve + "=" + strStateValve + "X";
        Serial.println(ReplyMsg);
      }

      if ((iValve == 8) && (Parameter != "?")) {
        //User wants to setup all 8 Valves in binary
        int value = Parameter.toInt();
        SetValve(8, value);
        ReplyMsg = "V8=" + DecToBin(~intValveStates) + "X";
        Serial.println(ReplyMsg);
      }

      if ((iValve < 8) && (Parameter != "?")) {
        int iParameter = atoi(Parameter.c_str());
        if  ((iParameter >= 0) && (iParameter <= 1)) {
          SetValve(iValve, iParameter);
          ReplyMsg = "V8=" + DecToBin(~intValveStates) + "X";
          Serial.println(ReplyMsg);
        }
      }
    }


    if ((Command == "S") || (Command == "s")) {
      if ((Parameter == "1")) {
        softReset();
        Serial.println("SR=1X");
      }
    }

    if ((Command == "p") || (Command == "P")) {
      // User wants to read Pressure Sensor
      //Format: Pn=?
      //
      adcCh = ads1115.readADC_SingleEnded(iValve);
      ReplyMsg =  adcCh + "X";
      //Serial.print(adcCh);
      //Serial.println("X");
      VoltCh = (adcCh * 6.144 / 32768) * 2; //factor 2 is due to voltage divider using 2 resistors of 1 kohm in serie (valid on channel 0) !!
      // ReplyMsg = Command + Valve + "=" + dtostrf(VoltCh, 10, 4, charVolt) + "X";
      ReplyMsg = Command + Valve + "=" + VoltCh + "X";
      Serial.println(ReplyMsg);
    }

    // clear the string:
    inputString = "";
    stringComplete = false;
  }

}








/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a 'X', set a flag so the main loop can
    // do something about it:
    if (inChar == 'X') {
      stringComplete = true;
    }
  }
}

You are doing an awful lot of string manipulation. There's a good chance you're running out of heap space.

I recommend modifying your system to use chars, ints, char arrays, flags, structs, or anything other than strings.

I would love to do that, but I'm a complete newbie on C+ and Arduino....I really don't know how to change it to what you said.

I've found a topic that seems to be similar to my case, it is marked as solved, but I did not see what the author did to solve it.

http://forum.arduino.cc/index.php?topic=236735.0

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

The author of the other thread bought a new Arduino, possible because he blew the other one.

Please post links to the relays and valves that you use; also provide a complete wiring diagram (a photo / scan of a hand-drawn one is OK) how everything is connected.

PS
what happens if you do not connect the relays? Same behaviour?

Hi Guys, I have news.

First, I would like to thank you all, for the patience and support.

Well, I have good and bad news.

I read at least 5x that post, http://forum.arduino.cc/index.php?topic=236735.0 , why ? Because the circuit he did is 99% similar to the one I did (you won´t believe when I saw his circuit !!!), the difference is that I have a PCF8574 that I use to set a pin on/off, and each pin attached to control a relay on that 8ch relay board. The other difference is that my relay board switches 220VAC on/off , and his circuit switches 24VDC. I use 220VAC because my solenoid valves operates at that voltage. I will post the circuit I drawn on easyeda at the end, but as I said it is almost the same as that one in the other post. Let me say that my electronics skills is too much basic (arduino and google are the responsible for this… ;D ).

About ten minutes I remember other post that someone said about electric noise captured on wiring or arduino board and I did what sterretje said above.

I powered off the main switch of the 220VAC and run again the sketch and my VB code in the PC. Everything worked, I received data on PC without any glitch. After, I set the 220VAC on again and after some time the problem appeared. To confirm, I did that procedure to disconnect/connect again the arduino from PC, and repeated the steps with 220VAC off and on, and voilá… confirmed, it is the some kind of a noise during the switch off/on the relays.

Now, the problem is: How do I fix this electronic issue ?

Regards.

Angelo

PS: Maybe this topic should be transfered to other part of this forum now.

Solution can be found in the following topic:

https://forum.arduino.cc/index.php?topic=517233.0