i think im dumb... lol serial help please.

hello guys, first post here, first sketch etc, little advice as to why this wont work? (tested all hardware, A.okay)

  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(11, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(6, OUTPUT);
  Serial.print("Please input a delay number in MS:");

void loop() {
  int del = Serial.read();
  digitalWrite(13, HIGH);   // set the LED on
  delay(del);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(del);              // wait for a second
  digitalWrite(11, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(6, LOW);

***EDIT*** Sorry man, just saw your links! my bad******** **EDIT 2***** script is functioning (Without serial component) 11 and 6 led pins had a shit ground connection my bad.

KE7GKP: 1) Using terms like "wont work" doesn't really give us anything to work with, does it? How do you know it doesn't work? What are the symptoms? Did it work before? 2) If you set del to some fixed number, does the code work? 3) How do you know you have a character ready to read? (Hint: http://arduino.cc/en/Serial/Available) 4) What are you sending it that you are reading with Serial.read()? Remember that if you are sending "numbers" from the keyboard, those are a series ASCII characters which is NOT the same as the number you think you are getting. If you want to press the key "5" and then the key "0" twice (to send "500") then you must do something at the Arduino end to accept three ASCII characters and turn them into a number that you expect for del. There are routines for converting strings to numbers, for example the standard C function atoi() (ASCII to integer) http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176289764

But first you have to figure out how long is the number (how many digits) and how do you know when you have the whole number entered?

Hello, sorry i wasn't aware that i was providing such USELESS information, as i stated it was my first post. Apologies for offending, wasn't my intention.


I tried setting the "del" variable to a static "50" and for some reason now only ledpin 13 is doing the blinking, darkness from the rest(they have 1k resistors) i have another script i wrote without the serial portion earlier, and it functioned flawlessly. so now, thats the symptom. i think im just not compitent enough to handle serial communications just yet and thats my problem. if its not to much trouble, could you provide me with one of these 3 things? (or more if your awesome xD)

  1. A working example of what im trying to make (im one of those guys that learns best from example)

  2. General serial tut/info link

  3. more specifically the int to string conversion thingy to wrap my head around that.

Thank you for your time!

KE7GKP: What do you mean by "tested all hardware, A.okay"? Did the LEDs on pins 11 and 6 blink at one time? How do you know 1K is the right resistor (sounds like it might be on the too-high side). How do you know the LEDs are connected the right way around?

You have like one equation and half a dozen unknowns. I was never bright enough to solve those in high-school and I still can't. I have to reduce it to a single equation and ONE unknown and attack each problem one at a time. If you know that pin 13 blinks properly, do the LEDs from pins 11 and 6 blink when connected the same way to pin 13? There are so many different possibilities of things to go wrong that you have to start isolating individual cases.

There are literally dozens of examples of Arduino code out there that show how people have accepted characters from human input and converted them into numbers in Arduino. I'm with you, I learn best from example, but I have a (non-Arduino) project that is bending my mind at the moment, so maybe you can spend a few more quality milliseconds with Google, or maybe somebody else here can write you an example.

I didn't mean to scold you for the lack of detail, but people don't remember that ALL we know is what you write here. We can't see anything, we don't have any history, etc.

i edited my post xD i figured out the blinking problem an isolated that i just dont understand the serial portion. i need to read for a while i think. i just need to find an example of user input converted etc...

Well I see one fundamental and common error for beginners using serial communications.

You show: int del = Serial.read(); How do you know if a character is ready to be received? You don't you are making an assumption. Also when the loop function starts from the top again, you continue to just read a character.

What you have to do is always precede a serial read function with a test of the serial avalible function to see if indeed a character is sitting in the receive buffer ready to be read.

From the arduino reference for serial.avalible:

int incomingByte = 0;   // for incoming serial data

void setup() {
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

void loop() {

    // send data only when you receive data:
    if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();

        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);

Note the if test and only performing a Serial.read if a character is actually avalible to read, >0.