Something quite strange is happening to me : the code hereunder makes the system hang (see attached picture) when I try to upload it on a Mega2560.
The code compiles ok.
bool rdmOn;
int txEnabledPin = 7;
int ledPin = 2;
uint8_t msg[32];
uint8_t nbrOct_msg;
uint8_t pos_msg;
uint8_t cmd[32];
uint8_t nbrOct_cmd;
unsigned long timeDeb;
unsigned long timeFin;
unsigned short measure[20];
int posRDM,nbrRDM;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600); //57600);
rdmOn=false;
pinMode(txEnabledPin,OUTPUT);
digitalWrite(txEnabledPin,LOW);
nbrOct_msg = 0;
pos_msg = 0;
nbrRDM = 2;
posRDM = 0;
}
void loop()
{
posRDM++; if (posRDM == nbrRDM) posRDM = 0;
while (Serial.available())
{
if (nbrOct_msg == 0)
{
nbrOct_msg = (uint8_t)Serial.read();
pos_msg = 0;
}
else
{
msg[pos_msg] = (uint8_t)Serial.read();
pos_msg++;
}
}
if (nbrOct_msg == 0)
return;
if (pos_msg < nbrOct_msg)
return;
nbrOct_cmd = nbrOct_msg + 2; // CRC
for (uint8_t i=0; i<nbrOct_msg; i++)
cmd[i] = msg[i];
Serial.print("???");
for (uint8_t i=0; i<nbrOct_cmd; i++)
Serial.write(cmd[i]);
Serial.print("!!!");
nbrOct_msg = 0;
}
Note that other codes can be uploaded without any problem (as the one hereunder), so I cannot beleive the Mega2560 is crashed :
int indexRelai;
bool relaiOn;
void setup() {
// initialize both serial ports:
Serial.begin(9600);
indexRelai=3;
relaiOn=false;
pinMode(7,OUTPUT);
digitalWrite(7,HIGH); // pour forcer sortie à zéro...
//pinMode(14,OUTPUT);
//digitalWrite(14,LOW);
Serial3.begin(9600);
pinMode(4,OUTPUT);
}
void loop() {
indexRelai++;
if (indexRelai==4)
{
indexRelai = 0;
relaiOn=(!relaiOn);
}
uint8_t msg[8];
msg[0]='#';msg[1]='0';msg[2]='1'; msg[3]='1';
msg[4]='0'+indexRelai;
msg[5]='0';
if (relaiOn) msg[6]='1'; else msg[6]='0';
msg[7]=13;
digitalWrite(7,HIGH);
delay(10);
for (int i=0; i<8; i++)
{
Serial3.write(msg[i]);
//delay(5);
}
//Serial3.write(msg,8);
delay(10);
digitalWrite(7,LOW);
Serial.write(msg,8);
if (relaiOn)
digitalWrite(2,HIGH);
else
digitalWrite(2,LOW);
delay(1000);
}
I tried to reduce the amount of memory used by global variables... no effect.
Does anyone have any idea ?
When such troubles appear, where can I find some resource to help me (discussions on the way the code is generated, maybe ?).
Thank you very much in advance !