LED timer

Hallo habe 4 led lichter auf pin 13,12,11,10 möchte die das die LEDs für eine bestimmte zeit an aus schalten kann.

led_t leds[] = {

 { 13, 20000, },
 { 12, 20000, },
 { 11, 20000, },
 { 10, 20000, },

#define NUMLEDS (sizeof(leds)/sizeof(leds[0]))

const int ledPin = 13;12;11;10;// the pin that the LED is attached to - change this if you have a separate LED connected to another pin
int incomingByte;      // a variable to read incoming serial data into
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}

Longbardo:
Hallo habe 4 led lichter auf pin 13,12,11,10 möchte die das die LEDs für eine bestimmte zeit an aus schalten kann. ...

Und was ist Deine Frage?

Gruß

Gregor

Und was möchtest du mit dem gezeigten Sketch erreichen?

Den hast du sicher falsch (C&P) kopiert.

Entsprechend deiner Anforderung sollte doch "BlinkWithoutDelay" reichen und das für jede einzelne Led.

Oder mehr Informationen mitteilen.

Ich habe 4 LEDs ich möchte alle 4 LED gleichzeitig automatische ein und ausschalten lassen für eine bestimmte Zeit "x", die ich definieren möchte. Also sowas wie ein Timer in etwa ?

Longbardo:
Ich habe 4 LEDs ich möchte alle 4 LED gleichzeitig automatische ein und ausschalten lassen für eine bestimmte Zeit "x", die ich definieren möchte. Also sowas wie ein Timer in etwa ?

Dafür ist dein obiger Sketch nicht geeignet.

Wie schon vorgeschlagen, sieh die "BlinkWithoutDelay" an.
Das ist dafür geeignet und die Zeit kannst du auch einstellen.

Hier noch ein kurzes Beispiel mit dem du es testen kannst.

const byte Taster = 9;
const byte Led1 = 10;
const byte Led2 = 11;
const byte Led3 = 12;
const byte Led4 = 13;
const long OnTime = 2000; // 2 Sek.

void setup() {
  pinMode (Led1, OUTPUT);
  pinMode (Led2, OUTPUT);
  pinMode (Led3, OUTPUT);
  pinMode (Led4, OUTPUT);
  pinMode (Taster, INPUT_PULLUP);
}

void loop() {
  if (Taster == LOW) {
    digitalWrite(Led1, HIGH);
    digitalWrite(Led2, HIGH);
    digitalWrite(Led3, HIGH);
    digitalWrite(Led4, HIGH);

    delay(OnTime);        // dieses delay nur zum Testen, sollten auf Basis millis aufgebaut werden

    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led4, LOW);
  }
}

Der Taster sollte entprellt werden.
Das geht per Hardware oder Software (debounce).
Die Leuchtzeit kannst du in "OnTime" einstellen.
Der Sketch dient als Orientierung und kann sicher optimiert werden.

Bei mir seht jetzt Sketch wird kompiliert..... darunter steht nun

Die Build-Voreinstellungsdatei konnte nicht geschrieben werden
java.io.FileNotFoundException: /var/folders/gz/zdvq2r1j727dyq0zkpl848ym0000gr/T/build2553752469290924910.tmp/BlinkWithoutDelay.cpp (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.(FileOutputStream.java:213)
at java.io.FileOutputStream.(FileOutputStream.java:162)
at processing.app.debug.Compiler.preprocess(Compiler.java:1268)
at processing.app.debug.Compiler.preprocess(Compiler.java:1228)
at processing.app.debug.Compiler.compile(Compiler.java:358)
at processing.app.debug.Compiler.build(Compiler.java:121)
at processing.app.Sketch.build(Sketch.java:1109)
at processing.app.Sketch.exportApplet(Sketch.java:1127)
at processing.app.Sketch.exportApplet(Sketch.java:1113)
at processing.app.Editor$DefaultExportHandler.run(Editor.java:2380)
at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
at processing.app.debug.Compiler.adviseDuplicateLibraries(Compiler.java:477)
at processing.app.debug.Compiler.build(Compiler.java:128)
at processing.app.Sketch.build(Sketch.java:1109)
at processing.app.Sketch.exportApplet(Sketch.java:1127)
at processing.app.Sketch.exportApplet(Sketch.java:1113)
at processing.app.Editor$DefaultExportHandler.run(Editor.java:2380)
at java.lang.Thread.run(Thread.java:745)

Hallo, sollen wir immer noch raten?

Welchen Sketch hast du genommen?
Welchen Arduino?
Welche Einstellungen?

Bitte mal mehr und richtige Informationen.

Wir sind keine Hellseher!

Und lerne doch mal die Basics.....

So wie ich's verstehe hat er Processing genommen und nicht Arduino IDE

Grüße Uwe

uwefed:
So wie ich's verstehe hat er Processing genommen und nicht Arduino IDE

Grüße Uwe

Ok,

Hauptsache ist, er merkt es auch selber noch. :wink:

Hallo,
für eine (1 Stck) LED- und noch eine (1 Stck) LED.

// each "event" (LED) gets their own tracking variable
unsigned long previousMillisTonLED=0;
unsigned long previousMillisPauseLED=0;

// different intervals for each LED
int intervalTonLED = 500;
int intervalPauseLED = 1000;

// each LED gets a state varaible
boolean PauseLEDstate = false;     // the LED will turn ON in the first iteration of loop()
boolean TonLEDstate = false;     // need to seed the light to be OFF

void setup() {
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);
}
void loop() {
   // get current time stamp
   // only need one for both if-statements
   unsigned long currentMillis = millis();

   // time to toggle LED on Pin 12?
   if ((unsigned long)(currentMillis - previousMillisTonLED) >= intervalTonLED) {
      TonLEDstate = !TonLEDstate;
      digitalWrite(12, TonLEDstate);
      // save current time to pin 12's previousMillis
      previousMillisTonLED = currentMillis;
   }

// time to toggle LED on Pin 13?
  if ((unsigned long)(currentMillis - previousMillisPauseLED) >= intervalPauseLED) {
      PauseLEDstate = !PauseLEDstate;
      digitalWrite(13, PauseLEDstate);
      // save current time to pin 12's previousMillis
      previousMillisPauseLED = currentMillis;
  }
}

Gruß und Spaß
Andreas

Hallo,
"Die Leuchtzeit kannst du in "OnTime" einstellen."

Ich kann nicht sehen, wann sie ausgeschaltet sind- wie kann ich dann z.B. sehen, ob sie 2 Sek. leuchten?
Gruß und Spaß
Andreas