main:
#include <LiquidCrystal.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo servo;
Adafruit_NeoPixel strip(4, 0, NEO_GRB + NEO_KHZ800);
volatile bool leftButtonPressed = false;
volatile bool leftbuttonconfirmation = false;
volatile int rightButtonCounter = 0;
static int accumulatedMoney = 0;
static unsigned long resetConfirmationStartTime = 0;
int ultrasonicTrigPin = 4;
int ultrasonicEchoPin = 5;
int schet =0;
bool stopschet=false;
void setup() {
attachInterrupt(0, leftButtonISR, RISING);
attachInterrupt(1, rightButtonISR, RISING);
lcd.begin(16, 2);
servo.attach(1);
strip.begin();
strip.show();
lcd4();
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
}
void loop() {
static bool POL = false;
static bool forceSensorZero = true;
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
long duration = pulseIn(ultrasonicEchoPin, HIGH);
int cm = duration / 58;
int forceValue = analogRead(A0);
if (cm>275) {
strip.clear();
strip.show();
POL = false;
lcd4();
}
if (cm<275 && cm>225) {
strip.clear();
strip.setPixelColor(0, 0, 255, 0);
strip.show();
POL = false;
lcd4();
}
if (cm<235 && cm>125) {
strip.clear();
strip.setPixelColor(0, 0, 255, 0);
strip.setPixelColor(1, 255, 255, 0);
strip.show();
POL = false;
lcd4();
}
if (cm<125 && cm>55) {
strip.clear();
strip.setPixelColor(0, 0, 255, 0);
strip.setPixelColor(1, 255, 255, 0);
strip.setPixelColor(2, 255, 50, 0);
strip.show();
POL = false;
lcd4();
}
if (cm<55 && cm>0) {
strip.clear();
strip.setPixelColor(0, 0, 255, 0);
strip.setPixelColor(1, 255, 255, 0);
strip.setPixelColor(2, 255, 50, 0);
strip.setPixelColor(3, 255, 0, 0);
strip.show();
lcd.home();
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print("Net mesta");
POL = true;
stopschet=true;
}
if (leftButtonPressed) {
resetConfirmationStartTime = millis();
leftButtonPressed = false;
leftbuttonconfirmation = true;
for (int i = 10; i > 0; i--) {
lcd.clear();
lcd.print("Sbros?");
lcd.setCursor(14, 1);
lcd.print(i);
lcd.setCursor(10, 0);
lcd.print(rightButtonCounter);
lcd.setCursor(12, 0);
lcd.print("iz 3");
if(rightButtonCounter>2)
{
break;
}
delay(1000);
}
}
if (leftbuttonconfirmation && millis() - resetConfirmationStartTime >= 10000 && rightButtonCounter <= 2) {
resetConfirmationStartTime = 0;
leftbuttonconfirmation = false;
}
if (leftbuttonconfirmation && rightButtonCounter == 3) {
resetConfirmationStartTime = 0;
accumulatedMoney = 0;
rightButtonCounter = 0;
lcd4();
leftbuttonconfirmation = false;
}
if (forceValue == 1023) {
forceSensorZero = true;
servo.write(0);
}
if (forceValue <= 1022) {
if (!POL) {
if (forceValue >= 800 && forceValue <= 1000 && forceSensorZero) {
accumulatedMoney = accumulatedMoney + 1;
servo.write(180);
delay(2000);
servo.write(0);
forceSensorZero = false;
}
if (forceValue >= 315 && forceValue <= 357 && forceSensorZero) {
accumulatedMoney = accumulatedMoney + 2;
servo.write(180);
delay(2000);
servo.write(0);
forceSensorZero = false;
}
if (forceValue >= 141 && forceValue <= 185 && forceSensorZero) {
accumulatedMoney = accumulatedMoney + 5;
servo.write(180);
delay(2000);
servo.write(0);
lcd4();
forceSensorZero = false;
}
if (forceValue >= 109 && forceValue <= 126 && forceSensorZero) {
accumulatedMoney = accumulatedMoney + 10;
servo.write(180);
delay(2000);
servo.write(0);
forceSensorZero = false;
}
}
else {
}
}
delay(100);
}
void lcd4() {
lcd.home();
lcd.clear();
lcd.print("vkopilke:");
lcd.setCursor(0, 1);
lcd.print(accumulatedMoney);
lcd.print(" rub ");
}
void leftButtonISR() {
leftButtonPressed = true;
}
void rightButtonISR() {
rightButtonCounter++;
}
Пробовал менять делеи и всякое такое, не могу решить
Вложения
-
82.1 KB Просмотры: 3