Pages: [1]   Go Down
Author Topic: Help with serial blinking led  (Read 1912 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Searching in internet I have found this:

 
/*
 * Serial Read Blink
 * -----------------
 * Turns on and off a light emitting diode(LED) connected to digital  
 * pin 13. The LED will blink the number of times given by a  
 * single-digit ASCII number read from the serial port.
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com>
 * http://todbot.com/
 *  
 * based on "serial_read_advanced" example
 */
 
int ledPin = 13;   // 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,HIGH);
     delay(150);
     digitalWrite(ledPin, LOW);
     delay(150);
    }
    //Serial.println();
  }
}
 
i would like to blink the led from 0 to 6000 times changing this:

 
if (val > '0' && val <= '6000' )
 
but, nothing, only blinks 9 times maximun.

How can I resolve it?

Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code you posted was reading a single digit from the serial port.  This should do what you wanted (I didn't test it though).

Code:
int ledPin = 13;   // select the pin for the LED
int val = 6000;       // variable to store the data upper limit
 
void setup() {
  pinMode(ledPin,OUTPUT);    // declare the LED's pin as output
}
 
void loop () {
  // blink the LED that number
    for(int i=0; i<val; i++) {
     digitalWrite(ledPin,HIGH);
     delay(150);
     digitalWrite(ledPin, LOW);
     delay(150);
  }
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you.

I resolve it using simplemessagesystem and this code:

Code:
#include <SimpleMessageSystem.h>

int ledPin = 12;   // Pin que se enciende
int val = messageGetInt();

void setup() {
  pinMode(ledPin,OUTPUT);    
  Serial.begin(9600);        
Serial.println("Numero de impulsos?");
}

void loop () {
 
  if (messageBuild() > 0) {   // si el mensaje se recibe completo (con su salto de carro)
  val = messageGetInt();      // la variable toma el valor del entero
    for(int i=0; i<val; i++) { // repite la lineas inferiores el número de veces indicado por la variable
      digitalWrite(ledPin,HIGH);
      delay(150);
      digitalWrite(ledPin, LOW);
      delay(150);
    }

    Serial.print("Impulsos= ");
    Serial.println(val, DEC);
    Serial.println("que quieres ahora??");
  }

}

« Last Edit: March 27, 2007, 07:39:13 am by pavlete » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can somebody do exactly the same with the current messenger library?
Logged

Pages: [1]   Go Up
Jump to: