#include <LiquidCrystal.h>
#include <FastLED.h>
#include <Servo.h>
Servo serega;
#define seregaPin 5
#define LED_PIN 4
#define NUM_LEDS 4
CRGB leds[NUM_LEDS];
#define TRIGpin 13
#define ECHOpin 6
#define meraPin A0
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
volatile bool timer_on = false;
volatile int s_but = 0;
int summa = 0;
volatile unsigned long counter = millis();
volatile int s = 10;
int lominat;
bool a = false;
volatile bool poln = false;
unsigned long s_sbros;
int timer_sbros = 1000;
bool sbros = false;
void setup() {
delay(3000);
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
Serial.begin(9600);
lcd.begin(16, 2);
print_to_lcd(0, 0, "Summa v kop " + String(summa) + " ");
print_to_lcd(0, 1, "0 rub ");
pinMode(TRIGpin, OUTPUT);
pinMode(ECHOpin, INPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
attachInterrupt(1, proverka, FALLING);
attachInterrupt(0, press, FALLING);
serega.attach(seregaPin);
serega.write(0);
pinMode(meraPin, INPUT);
}
void loop() {
if (s_but >= 3) {
s_but = summa = 0;
timer_on = false;
s = 10;
serega.write(0);
lcd.clear();
print_to_lcd(0, 1, "0 rub ");
}
if(!poln && !timer_on) {
print_to_lcd(0, 0, "Summa v kopilke ");
delay(250);
int som = analogRead(meraPin);
if (som == 0) {
a = false;
if(!sbros) {
sbros = true;
}
}
else {
if(!a) {
serega.write(180);
s_sbros = millis();
if(256 > som && som > 0){
lominat = 1;
}
else if(512 > som && som >= 256) {
lominat = 2;
}
else if(768 > som && som >= 512) {
lominat = 5;
}
else if(1024 > som && som >= 768) {
lominat = 10;
}
summa += lominat;
print_to_lcd(0, 1, String(summa) + " rub ");
}
a = true;
}
if(millis() - s_sbros >= timer_sbros) {
sbros = false;
serega.write(0);
s_sbros = millis();
}
}
digitalWrite(TRIGpin, LOW);
delayMicroseconds(2);
digitalWrite(TRIGpin, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGpin, LOW);
int duration = pulseIn(ECHOpin, HIGH);
int dist = (duration / 2) / 29.1;
if(3 > dist){
if(!poln) {
lcd.clear();
print_to_lcd(0, 0, "Kopilka full!!!");
}
poln = true;
}
else {
if(dist >= 3 && poln) {
lcd.clear();
print_to_lcd(0, 0, "Summa v kopilke ");
print_to_lcd(0, 1, String(summa) + " rub ");
}
poln = false;
}
shkala(dist);
if(timer_on) {
timer();
print_to_lcd(0, 0, "Sbrosit summu? ");
print_to_lcd(0, 1, "Press 3/" + String(s_but));
print_to_lcd(12, 1, String(s) + " s ");
}
if(s_but < 3 && s <= 0) {
s_but = 0;
s = 10;
timer_on = false;
lcd.clear();
print_to_lcd(0, 1, String(summa) + " rub ");
}
}
void print_to_lcd(int x, int y, String str) {
lcd.setCursor(x, y);
lcd.print(str);
}
void shkala(int dist) {
if (dist < 3) {
leds[0] = CRGB::Red;
leds[1] = CRGB::Orange;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Green;
}
else if (3 <= dist && dist < 133) {
leds[0] = CRGB::Black;
leds[1] = CRGB::Orange;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Green;
}
else if (133 <= dist && dist < 267) {
leds[0] = CRGB::Black;
leds[1] = CRGB::Black;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Green;
}
else if (dist >= 267 && dist < 400) {
leds[0] = CRGB::Black;
leds[1] = CRGB::Black;
leds[2] = CRGB::Black;
leds[3] = CRGB::Green;
}
else if (dist >= 400) {
FastLED.clear();
}
FastLED.show();
FastLED.delay(10);
}
void timer() {
if(millis() - counter >= 1000) {
s--;
counter = millis();
}
}
void proverka() {
if(!poln) {
s_but = 0;
s = 10;
timer_on = true;
counter = millis();
}
}
void press() {
if (timer_on) {
s_but++;
}
}