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:
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).
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);
}
}
I resolve it using simplemessagesystem and this 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??");
}
}