Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by zzz ( 6 years ago )
#include <Arduino.h>
#include <LiquidCrystal.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int b1 = 8;

int count, count2, count3, forWhile, scrollLength, countButton, columns = 16, rows = 2; // Kolumny i wiersze przy lcd.begin(columns, rows);

double value, savedCount;

long savedTime, savedTime2, savedTime3;

int stala1 = 1000, stala2 = 3510; // (count * stala1) / stala2

long timeScroll = 500UL, timeView = 10000UL, timeWait = 1500UL, counter = 1000UL; // timeScroll - predkosc przesuwania, timeView - czas wyswietlenie obliczonej wartosci, timeWait - czas postroju scrollowanego tekstu

String text1 = "Czas pomiedzy impulsami: "; // 
String jednostka1 = " s";                   //
String text2 = "Zuzyta energia: ";          // Teksty na wyswietlaczu
String jednostka2 = " kWh";                 //
String text3 = "Czas pomiaru:";             //

void Text()
{
  lcd.setCursor(0, 0);
  lcd.print(text1);
  lcd.print((int)savedCount);
  lcd.print(jednostka1);
  lcd.setCursor(0, 1);
  lcd.print(text2);
  lcd.print(value);
  lcd.print(jednostka2);
}

void Text2()
{
  while (forWhile == 0)
  {
    lcd.clear();
    forWhile = 1;
  }
  lcd.setCursor(2, 0);
  lcd.print(text3);
  lcd.setCursor(7, 1);
  lcd.print(count);
}

bool isButtonPressed(int button)
{
  if (digitalRead(button) == LOW)
  {
    delay(20);
    if (digitalRead(button) == LOW)
    {
      return true;
    }
  }
  return false;
}

void Reset()
{
  if ((millis() - savedTime) >= timeView)
  {
    if (savedCount > 0)
    {
      savedCount = 0;
      value = 0;
    }
  }
}

void countZero(int button)
{
  value = (savedCount * stala1) / stala2;
  savedTime = millis();
  savedTime3 = millis();
  while (!isButtonPressed(button))
  {
    for (int i = 0; i <= 15; i++)
    {
      if (!isButtonPressed(button) && (millis() - savedTime3) >= timeScroll)
      {
        lcd.scrollDisplayLeft();
        count3++;
        savedTime3 = millis();
        while (!isButtonPressed(button) && count3 == 15)
        {
          if (!isButtonPressed(button) && (millis() - savedTime3) >= timeWait)
          {
            lcd.clear();
            Text();
            count3 = 0;
          }
        }
      }
    }
    Text();
    Reset();
  }
}

void countView(int button)
{
  while (!isButtonPressed(button) && countButton == 1)
  {
    if ((millis() - savedTime2) >= counter)
    {
      count3 = 0;
      count++;
      Text2();
      savedTime2 = millis();
    }
  }

  if (!isButtonPressed(button) && countButton == 2)
  {
    lcd.clear();
    savedCount = count;
    count = 0;
    countButton = 0;
    forWhile = 0;
    countZero(button);
  }
}

void setup()
{
  pinMode(b1, INPUT_PULLUP);
  lcd.clear();
  lcd.begin(columns, rows);
  countZero(b1);
}

void loop()
{
  if (isButtonPressed(b1))
  {
    count2 = 1;
  }
  while (count2 == 1)
  {
    if (digitalRead(b1) != LOW)
    {
      count2 = 0;
      countButton++;
      countView(b1);
    }
  }
}

 

Revise this Paste

Your Name: Code Language: