Code Arrangement Help!

Hello! I’m back with a new problem hehe. So far I’ve been doing my project well then i suddenly cross a problem with it.

I think my code is a mess. I have XBEE and ESP8266 and a MOTOR. When i join the code for my esp8266 to the program. the XBEE is not working. But when i remove it, the XBEE is working and it triggers the motor.

Here’s a look at my code:

#include <SoftwareSerial.h>
SoftwareSerial XBee(10, 11); // RX, TX
SoftwareSerial esp(50, 52);

int relayPin = 4;
int stepPin = 5;
int dirPin = 6;
int enblPin = 7;
long Distance = 0;

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

int dataNumber = 0;             // new for this version

void setup()
{
  Serial.begin(9600);
  XBee.begin(9600); //SERIALS
  esp.begin(9600);

  esp.println("AT");
  response(3000);
  esp.println("AT+CIOBAUD=9600");
  response(1000);
  esp.println("AT+CWMODE=1");
  response(2000);
  esp.println("AT+CWJAP=\"Jebe\",\"12345678\"");
  response(10000);

  Serial.println("<Arduino is ready>");
  pinMode(relayPin, OUTPUT); //RELAY SERIAL
  digitalWrite(relayPin, HIGH);
  pinMode (stepPin, OUTPUT); //MOTOR SERIAL
  pinMode (dirPin, OUTPUT);
  pinMode (enblPin, OUTPUT);
  digitalWrite(stepPin, LOW);
  digitalWrite(dirPin, LOW);
  digitalWrite(enblPin, HIGH);
}

void loop()
{

  static bool runningMotor1 = false;

  recvWithEndMarker();
  showNewNumber();

  delay(3000);
  
  if (!runningMotor1 && dataNumber <= 50)
  { runMotor1();
    runningMotor1 = true;
  }

  if (runningMotor1 && dataNumber >= 51)
  { runMotor2();
    runningMotor1 = false;
  }

  delay(3000);

  esp.println("AT+CIPSTART=\"TCP\",\"rmc.cloudsoftwarestudio.com\",80");
  response(5000);
  esp.println("AT+CIPSEND=80");
  response(2000);
  esp.print("GET Your Link with the IP address You Used ");
  esp.print(dataNumber);
  esp.println("\r\nabcd1234");
  response(5000);

}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  if (XBee.available() > 0) {
    rc = XBee.read();

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

void showNewNumber() {
  if (newData == true) {
    dataNumber = 0;             // new for this version
    dataNumber = atoi(receivedChars);   // new for this version
    // Serial.print("This just in ... ");
    // Serial.println(receivedChars);
    //Serial.print("Data as Number ... ");    // new for this version
    Serial.print (dataNumber);     // new for this version
    Serial.println (" mm");
    newData = false;
  }
}

void runMotor1() {

  //CLOCKWISE

  {
    digitalWrite(relayPin, LOW);  // Turn Relay ON
    Serial.println("Relay is ON");
  }
  {
    for (long x = 0; x < 44800; x++)
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(100);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(100);
      //Distance = Distance + 1;
    }
  }
  if (Distance == 44800);

  { if
    (digitalRead(dirPin) == HIGH)
    {
      digitalWrite(dirPin, LOW);
    }

    else
    {
      digitalWrite(dirPin, HIGH);
    }
    {
      digitalWrite(relayPin, HIGH); // Turn Relay Off
    }
  }
}
void runMotor2()
{

  //C-CLOCKWISE

  {
    digitalWrite(relayPin, LOW);  // Turn Relay ON
    Serial.println("Relay is ON");
  }
  {
    for (long x = 0; x < 44800; x++)
    {
      digitalWrite(stepPin, LOW);
      delayMicroseconds(100);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(100);
      //  Distance = Distance + 1;
    }

  }
  if (Distance == 44800);

  { if
    (digitalRead(dirPin) == LOW)
    {
      digitalWrite(dirPin, HIGH);
    }

    else
    {
      digitalWrite(dirPin, LOW);
    }
    {
      digitalWrite(relayPin, HIGH); //Turn Relay OFF
    }
  }
}

void response(int waitTime) {

  for (int i = 0 ; i < waitTime; i++)

  { if (esp.available() > 0)

    { char x = esp.read();

      Serial.print(x);

    }

    delay(1);

  }
}

The esp is the only thing that i see in the serial monitor. the printing of the values from the xbee are not showing and the part of this code

static bool runningMotor1 = false;

  recvWithEndMarker();
  showNewNumber();

  delay(3000);
  
  if (!runningMotor1 && dataNumber <= 50)
  { runMotor1();
    runningMotor1 = true;
  }

  if (runningMotor1 && dataNumber >= 51)
  { runMotor2();
    runningMotor1 = false;
  }

i think is not satisfying.

any thoughts? THANKS!

-andrea

SoftwareSerial XBee(10, 11); // RX, TX
SoftwareSerial esp(50, 52);

Only one instance of software serial can be used at once. I have not used it but this page seems to show a solution.

The best solution is to use an Arduino that has more than one hardware serial port, ie Mega (3 + USB).

groundFungus:

SoftwareSerial XBee(10, 11); // RX, TX

SoftwareSerial esp(50, 52);



Only one instance of software serial can be used at once. I have not used it but[ this page](http://www.fiz-ix.com/2012/12/arduino-uno-with-multiple-software-serial-devices/) seems to show a solution.

The best solution is to use an Arduino that has more than one hardware serial port, ie Mega (3 + USB).

Hello! Thanks for replying. I am currently using Arduino MEGA which has i think 3 serial ports? something like that. So i think using 2 software serial at the same time would work. Thanks!

No. You need HardwareSerial, not SoftwareSerial. The objects you’re looking for are Serial1, Serial2 and Serial3. Reference

As a general rule: don’t use SoftwareSerial.
(Unless you want a temporary debug output, for example.)

Pieter

Your codes both block execution terribly. Over 99.9% of the time they spend doing NOTHING and zip through the rest to block again. NOTHING means not running any other code but interrupts.

Don't use an interrupt to try and fix this. Learn about blocking code and how to never need to use a delay() again.

In my signature space below are 3 addresses to tutorials. You only need to check out the first one to see what & how.

PieterP:
No. You need HardwareSerial, not SoftwareSerial. The objects you're looking for are Serial1, Serial2 and Serial3. Reference

As a general rule: don't use SoftwareSerial.
(Unless you want a temporary debug output, for example.)

Pieter

Hello! Thanks for replying. Does this mean that i should put my xbee and esp8266 on serial 1 and serial 2? if that's the case, should i remove the

SoftwareSerial XBee(10, 11); // RX, TX
SoftwareSerial esp(14, 15);

"SoftwareSerial" in the code?

I am new to using arduino in multiple tasking. hehe. Thanks!

Yes, remove the all of software serial stuff and use the harware ports (Serial1 and Serial2).

Pin numbers pretty much gave it away but you really should include in your question description what Arduino you use, as it often matters, like in this case.

You have three hardware Serial available. Use them.

  Serial.begin(9600);
  XBee.begin(9600); //SERIALS
  esp.begin(9600);

is replaced by:

  Serial.begin(9600);
  Serial1.begin(9600); // could be the Xbee
  Serial2.begin(9600); // then this would be the ESP

No, even simpler. Replace all of the code below:

#include <SoftwareSerial.h>
SoftwareSerial XBee(10, 11); // RX, TX
SoftwareSerial esp(50, 52);

With the following…

#define XBee Serial1
#define esp Serial2

Then the rest of your code will just work with no modification. Of course you should choose which Serial1/2/3 you prefer for each device.

  delay(3000);

Don't do this.

You need to have the parts of the program which deal with serial input called at least 10 times per second, every second. Preferably a few hundred times per second. Otherwise you will miss vital information.

Find Robin's tutorial on "Planning and implementing an Arduino program".

Hello, everyone! Thanks for your interest. <3 I did this. Look:

#define esp Serial3
#define XBee Serial2

int relayPin = 4;
int stepPin = 5;
int dirPin = 6;
int enblPin = 7;
long Distance = 0;

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

int dataNumber = 0;             // new for this version

void setup()
{
  Serial.begin(9600);
  XBee.begin(9600); //SERIALS
  esp.begin(9600);

my problem is that this part of the code

 static bool runningMotor1 = false;

  recvWithEndMarker();
  showNewNumber();

  if (!runningMotor1 && dataNumber <= 50)
  { runMotor1();
    runningMotor1 = true;
  }

  if (runningMotor1 && dataNumber >= 51)
  { runMotor2();
    runningMotor1 = false;
  }

is not satisfying. the only thing that can be seen in the serial monitor is the commands in esp.

Here is the sample of my output that is seen in the serial monitor

AT

OK
AT+CIOBAUD=9600

OK
AT+CWMODE=1

OK
AT+CWJAP=“Jebe”,“12345678”

0,CLOSED

OK

Relay is ON
Relay OFF
AT+CIPSTART=“TCP”,“rmc.cloudsoftwarestudio.com”,80

CONNECT

OK
AT+CIPSEND=80

OK

0
GET Your Link with the IP address You Used
0
AT+CIPSTART=“TCP”,“rmc.cloudsoftwarestudio.com”,80
busy s…

SEND OK
AT+CIPSEND=80

OK

0
GET Your Link with the IP address You Used
0
0 mm
AT+CIPSTART=“TCP”,“rmc.cloudsoftwarestudio.com”,80
busy s…

SEND OK

thanks!
-andrea

What Serial monitor? In the original sketch I don't see you print anything to Serial other than a "ready" message.

wvmarle:
What Serial monitor? In the original sketch I don't see you print anything to Serial other than a "ready" message.

Here is the code that prints the value coming in the xbee:

void showNewNumber() {
  if (newData == true) {
    dataNumber = 0;             // new for this version
    dataNumber = atoi(receivedChars);   // new for this version
    // Serial.print("This just in ... ");
    // Serial.println(receivedChars);
    //Serial.print("Data as Number ... ");    // new for this version
    Serial.print (dataNumber);     // new for this version
    Serial.println (" mm");
    newData = false;
  }
}

and with the "dataNumber", the if condition should be satisfied, but nothing is happening with respect to that part. Thanks!
-andrea

wvmarle:
What Serial monitor?

I thought that was why softserial was being used on a 328P board. At least it's not a G-shield!

As for Arduino Mega serial ports, there are 4. Serial, Serial1, 2 & 3. All of those can function as full speed master mode SPI ports, code and walkthrough tutorial are up on Nick Gammon's blogs, like with so much else.

autogation17:
Here is the code that prints the value coming in the xbee:

Post the complete code for the latest version of your proogram.

...R