0

currently i have a project that forced me to make a program that receives several data from Serial Input using Interrupt in Arduino. I use Arduino as the receiver and ESP32 as the sender through Hardware Serial. The program i have written for Arduino is as posted at Serial Input Basics with a few changes as written below :

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};
unsigned long sendDataPrevMillis = 0;
int controlCondition = 0;
int droneCondition = 0;
int gasValue = 0;
int pitchValue = 0;
int rollValue = 0;
int yawValue = 0;

boolean newData = false;

void setup() {
  attachInterrupt(digitalPinToInterrupt(1), handleInterrupt, RISING);
  Serial.begin(57600);
}

void loop() {
  
  Serial.print("Control Condition :");
  Serial.print(controlCondition);
  Serial.print(" Drone Condition :");
  Serial.print(droneCondition);
  Serial.print(" Roll:");
  if(rollValue < 1500)Serial.print("<<<");
  else if(rollValue > 1500)Serial.print(">>>");
  else Serial.print("-+-");
  Serial.print(rollValue);
  Serial.print("  Pitch:");
  if(pitchValue > 1500)Serial.print("^^^");
  else if(pitchValue < 1500)Serial.print("vvv");
  else Serial.print("-+-");
  Serial.print(pitchValue);
  
  Serial.print("  Gas:");
  if(gasValue < 1500)Serial.print("vvv");
  else if(gasValue > 1500)Serial.print("^^^");
  else Serial.print("-+-");
  Serial.print(gasValue);
  
  Serial.print("  Yaw:");
  if(yawValue < 1500)Serial.print("<<<");
  else if(yawValue > 1500)Serial.print(">>>");
  else Serial.print("-+-");
  Serial.println(yawValue);
}

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

    while (Serial.available() && 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;
        }
    }
    if (newData == true) {

    strcpy(tempChars, receivedChars);
    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars, ",");
    controlCondition = atoi(strtokIndx);     

    strtokIndx = strtok(NULL, ",");
    droneCondition = atoi(strtokIndx);
    
    strtokIndx = strtok(NULL, ",");
    gasValue = atoi(strtokIndx);

    strtokIndx = strtok(NULL, ",");
    pitchValue = atoi(strtokIndx);     

    strtokIndx = strtok(NULL, ",");
    rollValue = atoi(strtokIndx);     

    strtokIndx = strtok(NULL, ",");
    yawValue = atoi(strtokIndx);     

    newData = false;
    }
}

The data will be sent from ESP32 to change the values of controlCondition, droneCondition, gasValue, pitchValue, rollValue, and yawValue.

I have tried it and the Interrupt cannot be triggered. The Interrupt should be triggered whenever the controlCondition, droneCondition, gasValue, pitchValue, rollValue, or yawValue change its value. Any idea what should I do? or is there something wrong with my code?

6
  • 2
    Interrupt handlers have to be very short. Yours is far too complicated. Commented Aug 12, 2023 at 16:25
  • Isn't the Arduino serial input already interrupt driven? Poll the input buffer (non-blocking) until you have a complete message. Commented Aug 12, 2023 at 16:39
  • I'd suggest to use avr-libc directly instead of arduino framework for work with interrupts.
    – dimich
    Commented Aug 12, 2023 at 18:53
  • @0___________ The code for receiving the data with Serial Input itself is quite complicated, so I still need to find the solution for it.
    – Zero
    Commented Aug 13, 2023 at 2:01
  • @WeatherVane The code that i showed in the void loop() is just an example code. The real code is more complex with so many calculations involved. The variables that I mentioned are the important variables for the calculations.
    – Zero
    Commented Aug 13, 2023 at 2:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.