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();
}