hat man mit serial.read ebenfalls ein delay?
ich habe nun ein vereinfachtes scrippt das zunächst nur eine led blinken lässt.
/*
* SerialReceiveMultipleFields sketch
* This code expects a message in the format: 12,345,678
* This code requires a newline character to indicate the end of the data
* Set the serial monitor to send newline characters
*/
const int NUMBER_OF_FIELDS = 3; // how many comma separated fields we expect
int fieldIndex = 0; // the current field being received
int values[NUMBER_OF_FIELDS]; // array holding values for all the fields
int ledPin = 13; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 0; // milliseconds of on-time
long OffTime = 0; // milliseconds of off-time
void setup()
{
Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void bluetooth() {
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
{
// yes, accumulate the value if the fieldIndex is within range
// additional fields are not stored
if(fieldIndex < NUMBER_OF_FIELDS) {
values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
}
}
else if (ch == ',') // comma is our separator, so move on to the next field
{
fieldIndex++; // increment field index
}
else
{
// any character not a digit or comma ends the acquisition of fields
// in this example it's the newline character sent by the Serial Monitor
// print each of the stored fields
for(int i=0; i < min(NUMBER_OF_FIELDS, fieldIndex+1); i++)
{
Serial.println(values[i]);
values[i] = 0; // set the values to zero, ready for the next message
}
fieldIndex = 0; // ready to start over
}
}
}
void led (long onTime, long OffTime){
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
Serial.println(onTime);
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
Serial.println(OffTime);
}
}
void loop()
{
bluetooth();
led(values[0],values[1]);
}