Go Down

Topic: Arduino mega 2560 stuck on Serial.begin(9600) (Read 2578 times) previous topic - next topic


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:
Code: [Select]
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 = Serial.read();      // 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 = Serial.read();      // read the serial port

You should never do a Serial.read 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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131