Blinkerschaltung ohne delay inegrieren

Hallo zusammen,

ich bin derzeit auf der suche nach einer möglichkeit den Blinker an einem RC LKW über die Fernsteuerung zu schalten. Ich bin absoluter Anfänger und daher brächte ich eure Hilfe.

Ich habe schon einen Sketch der das Stand/Abblend und Fernlicht Schalten kann. Nun verstehe ich nur nicht wie ich da noch die Schaltung für die Blinker integrieren kann. Über die Funktion Millis sollte das wohl funktionieren.

Hier der Aktuelle Sketch:

void setup() {
  pinMode (3, INPUT);
  pinMode (8, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, INPUT);
  pinMode (6, OUTPUT);
}

void loop() {
  int a = pulseIn(3, HIGH);
  if (a > 1450 && a < 1550)
    digitalWrite (8, HIGH);
  if (a > 1900 && a < 2100)
    digitalWrite (8, LOW);

  int b = pulseIn(5, HIGH);
  if (b > 1450 && b < 1550)
    digitalWrite (4, HIGH);
  if (b > 1900 && b < 2100)
    digitalWrite (4, LOW);
    if (b > 950 && b < 1050)
    digitalWrite (6, HIGH);
    if (b > 1900 && b < 2100)
    digitalWrite (6, LOW);
    if (b > 1450 && b < 1550)
    digitalWrite (6, LOW);

Ich hoffe ihr könnt mir da helfen.

Vielen Dank

MfG Sebastian

Wenn du noch eine dritte Servo-Erfassung per pulseIn einbaust, dauert ein loop-Durchlauf zwar 3 * 20 ms, aber das ist ja noch kein wirkliches Problem für das Blinken.

Du kannst halt nur nicht direkt eine Led ansteuern, sondern nur einen Merker setzen:

PulsLänge Merker
 < 1250      L
1250 - 1750  -
> 1750       R

Dann die BlinkwithoutDelay - Methode noch mit dem Merker verknüpfen.

Weiter geht es dann mit der Frage: und eine Warnblinker-Funktion ? :wink:

Als Beispiel dient wie immer das hier aus der IDE:

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Müsste dann ja theoretisch so ungefähr aussehen:

// constants won't change. Used here to set a pin number:
const int blinker_rechts = 8;// the number of the LED pin
const int blinker_links = 10;// the number of the LED pin

// Variables will change:
int ledState_rechts = LOW;             // ledState used to set the LED
int ledState_links = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  pinMode (3, INPUT);
  pinMode (4, OUTPUT);
  pinMode (5, INPUT);
  pinMode (6, OUTPUT);
  pinMode (7, INPUT);
  pinMode (blinker_rechts,OUTPUT);
  pinMode (9, INPUT);
  pinMode (blinker_links, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  int a = pulseIn(3, HIGH);
  if (a > 1450 && a < 1550)
    digitalWrite (8, HIGH);
  if (a > 1900 && a < 2100)
    digitalWrite (8, LOW);

  int b = pulseIn(5, HIGH);
  if (b > 1450 && b < 1550)
    digitalWrite (4, HIGH);
  if (b > 1900 && b < 2100)
    digitalWrite (4, LOW);
    if (b > 950 && b < 1050)
    digitalWrite (6, HIGH);
    if (b > 1900 && b < 2100)
    digitalWrite (6, LOW);
    if (b > 1450 && b < 1550)
    digitalWrite (6, LOW);
    
// -----------Blinker rechts-----------

      int r = pulseIn(7, HIGH);
  if (r > 1450 && r < 1550)
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
          if (ledState_rechts == LOW) {
      ledState_rechts = HIGH;
    } else {
      ledState_rechts = LOW;
    }
  }
  if (r > 1900 && r < 2100) ledState_rechts = LOW;
  
// -----------Blinker links-----------

      int l = pulseIn(7, HIGH);
  if (l > 1450 && l < 1550)
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
          if (ledState_rechts == LOW) {
      ledState_rechts = HIGH;
    } else {
      ledState_rechts = LOW;
    }
  }
  if (l > 1900 && l < 2100) ledState_rechts = LOW;

  // -----------Warnblinker-----------

      int w = pulseIn(7, HIGH);
  if (w > 1450 && w < 1550)
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
          if (ledState_rechts == LOW && ledState_links == LOW) {
      ledState_rechts = HIGH;     ledState_links = HIGH;
    } else {
      ledState_rechts = LOW;     ledState_links = LOW;
    }
  }
  if (w > 1900 && w < 2100) ledState_rechts = LOW;ledState_links = LOW; 
  
 digitalWrite(blinker_rechts, ledState_rechts);
 digitalWrite(blinker_links, ledState_links);
}

Um mehrere Schaltfunktionen über einen Prop-Kanal zu übertragen, muss man etwas mehr Aufwand treiben. Vor kurzem war das schon mal hier ein Thema. Such mal nach Nautik Modul im Zusammenhang mit Arduino.

Hallo,

für die ellenlangen if Vergleiche kannst du auch switch case verwenden

Bsp.:

switch (a) {
    case 1450 ... 1550: digitalWrite (8, HIGH);       break;
    case 1900 ... 2100: digitalWrite (8, LOW);        break;
    default:            Serial.println(F("default")); break;
  }

Danke!
Gut zu erfahren, dass auch der aktuelle avr-gcc diese Erweiterung des C++ Standards unterstützt.

(Microsoft C++ macht es -noch- nicht, GCC 4.9 hat es wohl auch schon drin)

stackoverflow:
Some compilers support case ranges like case x ... y as an extension to the C++ language.

Quelle

Hallo,

fiel mir auch erst vor paar Wochen zufällig in die Hände als ich einen Codeschnipsel von Peter Dannegger gesehen hatte. Sieht erstmal ungewohnt aus. :slight_smile:

Wegen der Blinkerfrage. Ich würde mir erstmal eine Funktion schreiben die für sich selbstständig nach Aufruf eine LED blinken lässt. Meinetwegen schon vorbereitet mit Parameterübergabe, Einschaltdauer, Ausschaltdauer und wie oft .Dann wird diese universell nutzbar. Du kannst auch auf die combies Tools zurückgreifen. Würde ich aber an deiner Stelle erst machen, wenn du das Grundprinzip verstanden hast, sonst lernste nichts. Die millies Abfrage und Verarbeitung wirst du auch in anderen Funktionen immer wieder benötigen.

mir bekannte aktuelle Version:
http://forum.arduino.cc/index.php?topic=522775.msg3566693#msg3566693

aber wie gesagt probiere es erst selbst

Theseus erklärt millis()
http://forum.arduino.cc/index.php?topic=400102.msg2752141#msg2752141

GuntherB - BlinkwithoutDelay - Die Nachtwächtererklärung
http://forum.arduino.cc/index.php?topic=423688.0

Guten Morgen zusammen,

es funktioniert einwandfrei. Die möglichkeit von Skorpi080 war eine hervorragende Vorlage.

Vielen Dank für eure Hilfe.

MfG Sebastian