How to achieve serialEvent and loop at the same time without interruption?

11,102

Solution 1

The Arduino forum motto always applies: it's easier to help if you post your entire sketch, not just the fragments where you think the problem lies.

That said, I think there's a serious problem here:

for(int k =0; k<=7; k++){
    displayModule.setLED(TM1638_COLOR_GREEN, k);
    delay(100);

That for cycle will essentially pause your sketch for at least 800ms.

If you want you sketch to "multitask" you have to stop using delay()s and switch your mindset from sequential programming to state- (or event-) based programming. The usual advice is to study the "Blink without delay" example.

On a second note, I don't understand what you mean by

I've introduced a void loop()

If you don't write a loop function the sketch won't compile in the first place.

Solution 2

I don't know if this will help or not. It's a serial code example that I made that draws 2 bars based off incoming serial data. It's a processing file, but it should work similarly to an Arduino sketch. At least in the sense of serialEvent. If you haven't worked with processing before, the draw method is analogous to the loop method. Maybe there is something within this code that will help you.

import processing.serial.*;

Serial myPort;
float[] floatArray;
float f1; float f2;

void setup() {
  size(400, 400);
  // Prints lists of serial ports
  println(Serial.list());
  // Usually arduino is port [0], but if not then change it here
  myPort = new Serial(this, Serial.list()[0], 9600);
  // Don't call serialEvent until new line character received
  myPort.bufferUntil('\n');
  // Creates and array of floating decimal numbers and initializes its length to 2
  floatArray = new float[2];
}

void draw() {
  // Draws 2 bars to represent incoming data in patriotic fashion
  background(0, 0, 255);
  fill(255, 0, 0);
  rect(0, f1, 100, height);
  fill(255);
  rect(100, f2, 100, height); 
}

void serialEvent(Serial myPort) {
  // Reads input until it receives a new line character
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    // Removes whitespace before and after string
    inString = trim(inString);
    // Parses the data on spaces, converts to floats, and puts each number into the array
    floatArray = float(split(inString, " "));
    // Make sure the array is at least 2 strings long.   
    if (floatArray.length >= 2) {
      // Assign the two numbers to variables so they can be drawn
      f1 = floatArray[0];
      f2 = floatArray[1];
      println(f2);
      // You could do the drawing down here in the serialEvent, but it would be choppy
    }
  } 
}
Share:
11,102
mozcelikors
Author by

mozcelikors

Interested in product development &amp; Linux customization with C/C++, Embedded Linux, Yocto Project, and Linux Kernel. Contributor to Eclipse Foundation and Yocto Project community.

Updated on June 05, 2022

Comments

  • mozcelikors
    mozcelikors almost 2 years

    I have been working on an sensor reader/geared RC Car Robot but im stuck on something. I've introduced a void loop() and a void serialEvent() scope. serialEvent gets the value, accumulates, and calls other functions such as turnRight(), turnLeft(), or reverseGear(). But today I tried to make a speed display for the car. However I discovered that the serial event interrupts the continuous display animations. In other words of saying, I want to receive data continuously on serialEvent, at the same time I success other continuous events on void loop. I'm attaching the code parts that I think might be the problem. I use an Arduino Mega 1280. Any answers would be much appreciated. Thanks.

    NOTE: Note that serialEvent should receive data continuously ( with 1sec delays ) for sensivity adjusted motor.

    Serial Event is like..

    void serialEvent(){
      if ( Serial.available() > 0 ){ 
          int val = Serial.read() - '0'; 
          if(val == 0){ .......................
    

    Loop scope is like..

    void loop() 
    { 
      displayModule.setDisplayToDecNumber(15, 0, false);
      for(int k =0; k<=7; k++){
        displayModule.setLED(TM1638_COLOR_GREEN, k);
        delay(100);
        ............................
    
  • mozcelikors
    mozcelikors about 11 years
    i'm sorry buddy. that doesnt answer my questions :((