Relais mit Taster Schalten und LCD Lauftext

Und hier mal insgesamt aus der hohlen Hand.

#include <LiquidCrystal.h>

unsigned int  Contrast = 75;
LiquidCrystal lcd(41, 42, 43, 40, 45, 46);

const byte contrastPin = 44;

const byte rel = 31;
const byte taster = 30;
byte posCounter = 0;
bool tasterstatus = LOW;
byte richtung = 0;
unsigned long relaisEinZeit = 0;

void setup()
{
  analogWrite(contrastPin, Contrast);
  lcd.begin(16, 2);
  lcd.print("   Lauftext");
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.print("      Zeile2");
  pinMode(rel, OUTPUT);
  digitalWrite(rel, LOW);
  pinMode(taster, INPUT);
}


void loop()
{
  anzeige();
  relais();
}

bool tik(const uint32_t startmillis, const uint32_t pause)
{
  if (millis() - startmillis >= pause)
  {
    return true;
  }
  return false;
}

void relais()
{
  if (digitalRead(taster))             // Taster gedrückt? ...
  {
    if (!digitalRead(rel))             // ... und Relais ist aus? ...
    {
      digitalWrite(rel, HIGH);         // ... relais an ....
      relaisEinZeit = millis();        // ... merke zeit für delay (2000);
    }
  }
  if (!digitalRead(rel) && tik(relaisEinZeit, 2000))  // taste ist losgelassen UND Zeit abgelaufen?
  {
    digitalWrite(rel, LOW);            // mache relais aus
  }
}

void anzeige()
{
  static uint32_t myMillis = millis();
  switch (richtung)
  {
    case 0:
      if (posCounter < 14)
      {
        if (tik(myMillis, 400))
        {
          lcd.scrollDisplayLeft();
          posCounter++;
          myMillis = millis();
        }
      }
      else
      {
        richtung = 1;
        posCounter = 0;
        myMillis = millis();
      }
      break;
    case 1:
      if (posCounter < 28)
      {
        if (tik(myMillis, 400))
        {
          lcd.scrollDisplayRight();
          posCounter++;
          myMillis = millis();
        }
      }
      else
      {
        richtung = 2;
        posCounter = 0;
        myMillis = millis();
      }
      break;
    case 2:
      if (posCounter < 14)
      {
        if (tik(myMillis, 400))
        {
          lcd.scrollDisplayLeft();
          posCounter++;
          myMillis = millis();
        }
      }
      else
      {
        richtung = 3;
        posCounter = 0;
        myMillis = millis();
      }
      break;
    case 3:
      if (tik(myMillis, 3000))
      {
        posCounter = 0;
        richtung = 0;
      }
      break;
  }
}

ungetestet und unkommentiert.

Gute Nacht :wink:

1 Like