Program hangs on serial1 input.

when Serial1.read() receives ‘u’ twice, program hangs and does not receive anymore input. The program is needed to receive more inputs to control motors

#include <Servo.h>

#define BOTTOMFANS 3
#define MOSFET 5

Servo ESC;
char incomingVal = 0;


bool on = false;
bool fullspeed = false;


void setup() {
 Serial1.begin(9600);
 Serial.begin(9600);
 pinMode(MOSFET, OUTPUT);
 pinMode(BOTTOMFANS, OUTPUT);
 ESC.attach(9, 1000, 2000);

}

void loop() {
if(Serial1.available() > 0){  
 incomingVal = Serial1.read();
 Serial.print(incomingVal);
 delay(25);
//THIS SECTION DEALS WITH POWERON ONLY AND BRINGING IT TO HALF SPEED
 if(incomingVal == 'u' && !on)
 {
   on = true;
   analogWrite(BOTTOMFANS, 127);
   delay(25);
 }
 else if(incomingVal == 'u' && on)
 {
   analogWrite(BOTTOMFANS, 255);
   ESC.write(90);
   analogWrite(MOSFET, 127);
 }
//THIS SECTION DEALS WITH FULL SPEED ONLY
 else if (incomingVal == '4' && !fullspeed)
 {
   ESC.write(180);
   analogWrite(MOSFET, 255);
   fullspeed = true;
 }

 else if(incomingVal == '4' && fullspeed)
 {
   ESC.write(90);
   analogWrite(MOSFET, 255);
   fullspeed=false;
 }
//********************************************************//
 //THIS SECTION DEALS WITH TURNING LEFT ONLY
 else if(incomingVal == 'l' && !fullspeed)
 {
   ESC.write(180);
 }
 else if(incomingVal == 't' && !fullspeed)
 {
   ESC.write(90);
 }
 else if( incomingVal == 'l' && fullspeed)
 {
   ESC.write(90);
 }
 else if(incomingVal = 't' && fullspeed)
 {
   ESC.write(180);
 }
//*************************************************************//

//THIS SECTIONS DEALS WITH TURNING RIGHT ONLY
//*************************************************************//
 else if( incomingVal == 'r' && !fullspeed)
 {
   analogWrite(MOSFET, 255);
 }
 else if( incomingVal == 'z' && !fullspeed)
 {
   analogWrite(MOSFET, 127);
 }
 else if(incomingVal == 'r' && fullspeed)
 {
   analogWrite(MOSFET, 127);
 }
 else if(incomingVal == 'z' && fullspeed)
 {
   analogWrite(MOSFET, 255);
 }
//*************************************************************//
//THIS SECTION DEALS WITH POWERING OFF THE BOARD (FOR NOW ATLEAST)
else if(incomingVal == 'd')
{
 analogWrite(BOTTOMFANS, 0);
 ESC.write(0);
 analogWrite(MOSFET, 0);
}
else if(incomingVal == '1')
{
 //dropMechanism is actiated here
}
}
}

FINALWORKING.ino (2.25 KB)

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

This is my first post. Thanks for the tip

Thank you. You will get more responses this way.

I don’t see anything wrong in the code yet. Since you had trouble with “u”, I looked there and it may not change anything, but I would have coded it like this:

    if (incomingVal == 'u')
    {
      if (not on)
      {
        on = true;
        analogWrite(BOTTOMFANS, 127);
        delay(25);
      }
      else
      {
        analogWrite(BOTTOMFANS, 255);
        ESC.write(90);
        analogWrite(MOSFET, 127);
      }
    }

It’s surprising how often though, it ends up being the result of some hardware turning on under program control, and resetting the processor due to a power dip or interference. You could write a simple sketch to just test what happens when BOTTOMFANS comes on. Another way is to disconnect all the peripheral hardware and run your current sketch.

it still doesnt work. When Serial1.read() recieves u, it hangs and stops working

Did you try the alternative suggestion that I put forth? Otherwise you can't be sure it isn't a hardware problem. How is the servo connected?

The servo is controlling a ESC, i included the delay to ensure it writes the value first, then resumes. However, after a second 'u' program hangs and there is no more serial1.read()

Looks like you’re trying to control an RC drone with an Arduino. Because of this, I’m assuming you’re using UART radio transceivers for control messaging. Your handling of radio messages is not robust and is prone to bit errors and packet “desyncing”. You might be interested in looking into the library SerialTransfer.h that reliably packetizes/parses serial data for applications such as this one. In fact, I use this library to command my scratch designed Arduino RC airplane, which is probably similar to your project.

The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}