Pages: [1]   Go Down
Author Topic: Arduino mega 2560 stuck on Serial.begin(9600)  (Read 1498 times)
0 Members and 1 Guest are viewing this topic.
france
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
  digitalWrite(ledPin,HIGH);
  delay(3000);
  digitalWrite(ledPin,LOW);
  Serial.begin(9600);        // connect to the serial port
}

void loop () {
  //  val = Serial.read();      // read the serial port
  val='8';
  // 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,HIGH);
      delay(150);
      digitalWrite(ledPin, LOW);
      delay(150);
    }
    //Serial.println();
  }
}
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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One thing I noticed on your sketch:

Quote
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:

http://arduino.cc/en/Serial/Available

Lefty
Logged

france
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

france
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: