Compiler issue?


#1

Hi,

I have created an extension to parse some data packets from a device. My extension is pending approval for publishing. In testing, mblock creates the code I want. If I copy and paste the code into the arduino ide, I can upload it and it works perfectly. However, when I upload the same code from mblock, it doesn’t seem to work (seem to crash). Here is the code that is uploaded, any ideas why it might be crashing when compiled by mblock but not when compiled by arduino ide?:

// generated by mBlock5 for <your product>
// codes make you happy

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11);
byte Attention=0;
byte Meditation=0;
byte SignalQuality=0;
// checksum variables
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {0};
byte signalQuality = 0;
byte attention = 0;
byte meditation = 0;
// system variables
boolean bigPacket = false;

byte ReadOneByte()
{
    int ByteRead;

    while (!mySerial.available());

    ByteRead = mySerial.read();
    return ByteRead;
}

void _delay(float seconds) {
  long endTime = millis() + seconds * 1000;
  while(millis() < endTime) _loop();
}

void setup() {
  mySerial.begin(57600);
  pinMode(13,OUTPUT);
  while(1) {
      if(Attention > 50){
        digitalWrite(13,1);

      }else{
        digitalWrite(13,0);

      }

      _loop();
  }

}

void _loop() {
  // Look for sync bytes
  if (ReadOneByte()== 170)
  {
      if (ReadOneByte()== 170)
      {
        payloadLength = ReadOneByte();
        if (payloadLength > 169) //Payload length can not be greater than 169
          return;
        generatedChecksum = 0;
        for (int i = 0; i < payloadLength; i++)
        {
          payloadData[i] = ReadOneByte(); //Read payload into memory
          generatedChecksum += payloadData[i];
        }
        checksum = ReadOneByte(); //Read checksum byte from stream
        generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
        if (checksum == generatedChecksum)
        {
          signalQuality = 200;
          attention = 0;
          meditation = 0;
          for (int i = 0; i < payloadLength; i++)
          { // Parse the payload
            switch (payloadData[i])
            {
              case 2:
                i++;
                signalQuality = payloadData[i];
                bigPacket = true;
                break;
              case 4:
                i++;
                attention = payloadData[i];
                break;
              case 5:
                i++;
                meditation = payloadData[i];
                break;
              case 0x80:
                i = i + 3;
                break;
              case 0x83:
                i = i + 25;
                break;
              default:
                break;
            } // switch
          } // for loop
          // *** Add your code here ***
          if (bigPacket)
          {
            SignalQuality = signalQuality;
            Attention = attention;
            Meditation = meditation;
          }
          bigPacket = false;
        }
        else {
          // Checksum Error
        } //  end if else for checksum
      } // end if read 0xAA byte
    } // end if read 0xAA byte
}

void loop() {
  _loop();
}

#2

@idahlstromhakki Can you please provide the formatted code (in ``` symbols) and the error message you get so I can help you further?


#3

Thanks for the quick response. Sorry for the poor formatting, I have fixed that. I also don’t get any error message, it uploads just fine but the code doesn’t seem to work, i.e. the light doesn’t change regardless of input when uploaded from mblock but changes as expected when uploaded from the arduino ide. I am waiting for my code to be published to see if the issue is due to some problem with the testing environment for the extension.

Thanks for your help,
Ibrahim


#4

@idahlstromhakki It’s not likely that the issue is due to the testing environment. It is possible, though.
I can’t tell you if there is something wrong with your code because I’m not sure what it is supposed to do. What does your block code look like? I also noticed the line:

         // *** Add your code here ***

What is this for? Make sure that all the libraries, if necessary, are included as well. Thanks!


#5

This is an AI response. Verify information.

Hello Ibrahim! Based on the code you provided, it seems like you are using an Arduino board to control an external device based on the attention level measured by a neurosky mindwave sensor.

It’s important to note that I am an AI chatbot and don’t have the capability to directly test or execute code. However, I can try to assist you with troubleshooting.

Here are a few things you can check:

  1. Make sure you have the necessary libraries installed: The code you provided includes the “Wire” and “SoftwareSerial” libraries. Ensure that you have these libraries installed in your Arduino IDE.

  2. Check your connections: Verify that your Arduino board is properly connected to the neurosky mindwave sensor and the external device you want to control. Ensure that the pins used for communication (in this case, pin 10 and 11 for SoftwareSerial) are correctly connected.

  3. Test with serial communication: You can add some serial print statements in your code to debug and see if you are receiving the expected data from the mindwave sensor. For example, you can add Serial.println(Attention); to check the value of the Attention variable.

  4. Verify the code logic: Go through the code and ensure that the logic is correct. Double-check the conditions and statements inside the if-else blocks to make sure they are performing as expected.

Additionally, it’s worth mentioning that mBlock may have some limitations or differences compared to the Arduino IDE, which could be causing the issue. You mentioned that the code works when uploaded from the Arduino IDE but not from mBlock. This could be due to how mBlock handles the code or the environment it provides.

If you have further questions or encounter any specific issues, please provide more details, and I’ll do my best to assist you.

  • Response is good
  • Response is neutral
  • Response is bad

0 voters


#6

Thanks for following up. That comment about adding code here can be ignored, it is just poor comment cleaning from me.

The code is supposed to work very similar to the DHTXX extension you wrote. There is a sensor that measures brain activity and deliver a data packet to a pin (set as a softserial Rx in my code). The data packet that arrives needs to be parsed into different variables (in my case Attention, Meditation, and signal quality) similar to the humidity and temperature values you can get from the DHT11. I then have my code turn on the pin 13 led when I focus my attention and turn it off when I don’t. What I have noticed with some more testing is that the code compiled with mblock works for a few seconds before it stops working. If I take the same code created by mblock and upload it using the Arduino ide it works without any issues. That is why I am wondering if there is a compiler problem or maybe a memory leak that exists under the mblock compiler but not the Arduino ide compiler. If it would be helpful, I can record a little video to show how the code works when uploaded from mblock compared to how it works uploaded from the Arduino ide.

Thanks,
Ibrahim


#7

@idahlstromhakki Alright, I will be awaiting further data. In the meantime, I’ll see what I can do later this week. Good luck!


#8

Ok, I made a video to show that I am trying to do and what the problem is. Here it is:

As you can see the same code works perfectly with the Arduino compiler but not the mBlock compiler. Could this be an memory leak or compiler bug? Outdated library? Not sure what else it could, any ideas would be very much appreciated.

Thanks,
Ibrahim


#9

Thanks, @idahlstromhakki, for the video. This has made your issue a lot clearer.
My first guess would be that you have a library installed on Arduino IDE (e.g., AttentionSensor.h, for example) that you don’t have included in mBlock.
I couldn’t be sure from the video, but that doesn’t seem to be the case. So, my second guess is that either mBlock has added extra code that is causing a bug to the online version (which desktop mirrors), or that mBlock is using a different compiler than Arduino IDE (compilers may change per mBlock device).

To test both of these theories, there are a couple of things we can try.

  1. We can use a different mBlock device (I have on you could try) that has a different upload middleware:
    image

  2. We can upload a different code to the board (let’s try a really simple one, like this:
    image
    ) to confirm that the issue is occurring only with your code.

Let me know what happens when you try that out. Good luck!


#10

Thanks for the suggestions. I tried your mBlock device and still the same issue. I tried it on both windows and osx and both were exactly the same as before. I also tried your blink code and that works just fine, I’ve done programming with ultrasonic sensors, temperature sensor, etc and all work just fine.

There are only three libraries I use:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

And they are the same on both the Arduino IDE and the mBlock one. Is there a way to reinstall the libraries in mBlock or update them to ensure I have the latest ones?

Thanks,
Ibrahim


#11

You can’t reinstall the libraries, sadly. I gave you the test code I did because it uses

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

as well. Since that code works with no issues, I can’t see why the other code would freeze unless there is an mBlock error or a compiler difference between mBlock and Arduino IDE (Arduino would be autocorrecting a possibly existing error in your code while mBlock is not). If you can private message me the .mext extension file, I can review it and see if I see anything wrong (not likely), else I will have to contact makeblock about the issue. (support@makeblock.com). Hopefully we can get this resolved, and good luck!


#12

Hi @Best_codes,

I have an update regarding the code. I rewrote my code to use the hardware serial instead of softserial and the code works perfectly. So the problem has to be with the way MBlock is implementing softserial (it seems to crash soon after starting). I will dm you the mext code.

Ibrahim


#13

@idahlstromhakki Sure! I will do my best. Also, I haven’t been able to work much on the extension project. :frowning: I’ve been very busy.