Arduino mega 2560 stuck on Serial.begin(9600)

hello there. I have a problem using serial with my arduino mega ( normally i explain the context, but with my poor english i do not dare ).
i’m trying to interface my computer ( under archlinux 64bit ) with my new arduino, using usb, and the virtual serial port /dev/ttyACM0. the upload work great with the arduino’s ide ( i try out some fonctions and exemples and no problem.) i use the following code to try out the serial initialisation:

int ledPin = 49;   // select the pin for the LED
int val = 0;       // variable to store the data from the serial port

void setup() 
  pinMode(ledPin,OUTPUT);    // declare the LED's pin as output
  Serial.begin(9600);        // connect to the serial port

void loop () {
  //  val =;      // read the serial port
  // if the stored value is a single-digit number, blink the LED that number
  if (val > '0' && val <= '9' ) {
    val = val - '0';          // convert from character to number
    for(int i=0; i<val; i++) {
  //    Serial.println("blink!");
      digitalWrite(ledPin, LOW);

and it dosent work, the L led blink as if it reset permanently ( and nothing else append ). but, when i try to comment out the “Serial.begin(9600);” (and all Serial ligne. ) the led connected on pin 49 light for 3 second and began blinking normaly.
i don’t understand what’s appening here, if one of you can help me, I would be grateful.
thank for reading.

One thing I noticed on your sketch:

void loop () { // val =; // read the serial port

You should never do a command without first doing a Serial.avalible command first to make sure there is indeed a character ready to be read.

This reference shows an example of how you must read serial data:


hum, to my mind that's not the problem, the version I show you (with that line commented ) the problem is that i never reach this line. you can see that the arduino never run the program, if it had done it the led connected to the pin 43 light up for 3 second and, eventualy, the arduino crash, here, the led state never change. i think the arduino reset, but i can't figure out why.

so, after some investigation and some help from the irc's guy it seem that's a compiler problem, i must look at my lib version. thanks to the community.