Serial communication using arduino

Hi, I am trying to control some lights using the GPIO pins of arduino. The computer and the arduino board are to remain on and connected to each other and the computer is never supposed to shutdown. The project is already done and was working without any communication issues. But now after around 2 weeks, when i try to turn on a particular light connecting to the gpio pin, it doesnt turn on. But after i close all the windows, unplug the USB cable and then plug it again, everything works fine.

Is there a way to fix this issue?

This is my code:

/*
 Modifying the example Example - Receive with start- and end-markers combined with parsing
*/
/* CAN EASILY CONTROL GPIO PINS BASED ON USER INPUT, IF USER SELECTS ON, THEN THE PIN TURNS ON, IF THE USER SELECTS OFF
 THEN THE PIN TURS OFF
*/

const byte numChars = 32;
char receivedChars[numChars]; 
char tempChars[numChars];         // temporary array for use when parsing

// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
int floatFromPC = 0;
int pinNumber;
boolean newData = false;

//=============

void setup() 
{
  for (pinNumber=0; pinNumber < 70; pinNumber++)
  {
    pinMode(pinNumber,OUTPUT);
    digitalWrite(pinNumber,HIGH);
  }
  
  Serial.begin(9600);
  Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
  Serial.println("Enter a data in this style <Hello World, 12, 24.7> ");
  Serial.println();
}

//============

void loop() 
{
  recvWithStartEndMarkers();

  if (newData == true)
  {
    strcpy(tempChars, receivedChars); 
    /* this temporary copy is necessary to protect the original data because
     *  strtok() used in parseData() replaces the commas with \0 
     */
    parseData();
    showParsedData();
    newData = false;
  }
}

//===========

void recvWithStartEndMarkers()
{
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false)
  {
    rc = Serial.read();

    if (recvInProgress == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars)
        {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker)
    {
      recvInProgress = true;
    }
  }
}

// =================

void parseData()    // Split the data into  its parts
{
  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars,","); // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPc

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);  // converts this part to integer

  strtokIndx = strtok(NULL, ",");
  floatFromPC = atoi(strtokIndx);  // converts this part to a float
}

//=================

void showParsedData()
{
  Serial.print("Message ");
  Serial.println(messageFromPC);
  Serial.print("Integer 1: ");
  Serial.println(integerFromPC);
  Serial.print("Integer 2: ");
  Serial.println(floatFromPC);

  digitalWrite(integerFromPC,floatFromPC);
}

Is this the line of code that you are using to operate the LED

digitalWrite(integerFromPC,floatFromPC);

It's a bit of a kludge, to say the least. And this is contradictory

 Serial.print("Integer 2: ");
  Serial.println(floatFromPC);

It would make a lot more sense to send two integers - the number of pin and either 1 or 0 depending on whether you want the LED to be on or off.

The idea of the demo was to give you some ideas that you could adapt to your own requirements.

Having said all that, I don't see any obvious reason why it should stop working and require a restart. You need to post some examples of the data you are sending.

Also make a drawing showing how you have everything connected and post a photo of the drawing.

...R

PS. I just realized you are sending two integers but did not update the name of the variable to reflect that. Using meaningful variable names makes it much easier to understand a program.

Yes I am sending two integers, but forgot to change the name of the second one.
Actually I have connected two arduino megas to the computer and one arduino works fine, but there is problem with the second arduino.
I am using qt for the gui. So whenever i press a button on the gui, the lights turn on for one of the arduinos but not necessarily for the other arduino. I restarted my computer, tried unplugging and plugging back the usb port, but the lights turn on as soon as i plug the usb cable to the cpu, but if i try to turn on the lights after few minutes it just doesn’t work.

Is there some problem with the arduino? Or do i need to change the usb settings?

jkprog: Actually I have connected two arduino megas to the computer and one arduino works fine, but there is problem with the second arduino. I am using qt for the gui. So whenever i press a button on the gui, the lights turn on for one of the arduinos but not necessarily for the other arduino. I restarted my computer, tried unplugging and plugging back the usb port, but the lights turn on as soon as i plug the usb cable to the cpu, but if i try to turn on the lights after few minutes it just doesn't work.

That suggests to me that there is a huge amount of info that you have not shared with us - including how the two Megas are connected to the PC and the exact data that the PC is sending to each of them.

Remember, you know all about your project and we know nothing unless you tell us.

...R

The aim of my project is to control a number of lights from the GPIO pins of the Arduino. For that I am using relay modules (src-05vdc-sl-c), each module having 8 relays. Each of the relays is connected to the GPIO pins on the Arduino.

Since I needed to control more than 70 lights, I had to use 2 arduino mega 2560. I uploaded the code in each of the Arduino megas and everything was working fine.
I first worked on 1 arduino and after ensuring that this whole thing is working fine, I bought another Arduino mega 2560 and uploaded the code and connected the relay modules to Arduino and the lights to the relays.

For the GUI, I used qt creator. Everytime I press a button on the GUI, it sends “<on, pinNumber,0>” to turn on the light. Since the relays are turned on when low is sent to them. And to turn the pins off “<off, pinNumber,1>” .

The computer that is used to communicate with the computers is brand new and doesn’t have anything installed on it. Just the qt and Arduino IDE.
54 lights are connected to the first Arduino (let’s call it A) and 12 lights are connected to the second one (lets name it B).

Now, the problem is even if both the arduinos are connected to the computer and the device manager and Arduino IDE detect the COM3 (Arduino A) and COM4 (Arduino B) ports, but when I press the buttons for the lights connected to A, the lights don’t turn on and there is no light on the Arduino that tells us if there is a data transfer on serial port.

But when I press buttons of lights connected to B, the lights on Arduino flash indicating a data transfer and the lights connected to B turn on.
Now I am not sure if there is some problem with the Arduino or what… The whole thing was running successfully for 2 weeks and I encountered the problem today in the morning.

What is QT? Maybe this is a question that you need to ask on a QT Forum where people know how to make it communicate over a Serial connection?

I presume your QT code actually sends something like “<on, 4, 0>” rather than “<on, pinNumber, 0>”. It is important to be accurate. And the first part - the “on” is just ignored.

And are you sure that ‘0’ makes the LED light?

Is your QT program properly showing the message from the Arduino when it starts?

If you try each of the Megas separately using the Arduino Serial Monitor do they work properly when you type in the message that QT would normally send?

…R

If you are using Serial communication on the MEGA, don’t use pin 0 and pin 1 for the GPIO control, because these are used by the Serial communication.

void setup() 
{
  for (pinNumber=2; pinNumber < 70; pinNumber++)  // Don't start from 0
  {
    pinMode(pinNumber,OUTPUT);
    digitalWrite(pinNumber,HIGH);
  }

yes the two arduinos are working seperately and they were working together until last week. QT is a gui software. And yes the serial communication is being done properly on QT.

Yes 0 makes the light connected to the relays light up because the Normally Open contact of the relay is closed when the 0 is sent from arduino.

okay i will modify the code so that the pin 0 and 1 arent set as output.

jkprog: Since I needed to control more than 70 lights, I had to use 2 arduino mega 2560.

One 74HC595 shift register per relay module, and you could have used a single Uno or Nano. Leo..