Make sure to add a 10K Ohms pull-up resistor to the COIN or signal line of the Coin Acceptor.
بإمكانك استخدام قيمة أخرى للمقاومة بدلاً من 10 كيلو أوم، وغالباً ما تعمل القيم المحصورة بين 4.7 كيلو أوم (4.7k) و 22 كيلو أوم (22k) بكفاءة ودون أي مشاكل.
code
نسخ
اقتباس
عرض
تنزيل
#include <Arduino.h>
#include <EEPROM.h>
#include <elapsedMillis.h>
float device_factor_pulse_accurate = 1 ;
// المتغيرات الأساسية
int totalAmount = 0;
int old_totalAmount = 0;
volatile bool updateDisplay = false;
volatile bool coinInserted = false;
elapsedMillis timer;
elapsedMillis countDownTimer;
const long intervalPerCoin = 10000; // الزمن المخصص لكل وحدة نقدية
bool countDownStarted = false;
// استخدام بن متوافق مع ESP8266 مثل D1 (الذي يوافق GPIO5)
const int sensorPin = 5;
volatile int coinType = 0;
// تعريف المتغير هنا أولاً ليصبح مرئياً لدالة الـ isr
volatile uint32_t pulseCount = 0;
// مقاطعة استشعار العملات
void ICACHE_RAM_ATTR isr()
{
pulseCount++;
}
void setup()
{
Serial.begin(9600);
pinMode(sensorPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensorPin), isr, FALLING);
Serial.println("Begin coin counter on ESP8266...");
}
int last_update = 0;
void loop()
{
if (pulseCount > 0)
{
noInterrupts();
uint32_t pulses = pulseCount;
pulseCount = 0;
interrupts();
Serial.print("Pulses: ");
Serial.println(pulses);
int coinValue = 1;
totalAmount += coinValue;
Serial.print("Coin inserted: ");
Serial.print(coinValue);
Serial.print(", Total: ");
Serial.println(totalAmount);
if (totalAmount > 0 && !countDownStarted)
{
countDownStarted = true;
timer = 0;
countDownTimer = timer;
}
last_update = millis();
}
if (totalAmount != old_totalAmount && millis() - last_update > 700)
{
int sub_amount = (totalAmount - old_totalAmount) * device_factor_pulse_accurate;
if (sub_amount >= 4 && sub_amount <= 6)
{
Serial.println("-- coin is 10$ --");
}
else if (sub_amount >= 8 && sub_amount <= 12)
{
Serial.println("-- coin is 5$ --");
}
else if (sub_amount >= 13 && sub_amount <= 17)
{
Serial.println("-- coin is 2$ --");
}
else if (sub_amount >= 18 && sub_amount <= 22)
{
Serial.println("-- coin is 1.5$ --");
}
else if (sub_amount >= 23 && sub_amount <= 27)
{
Serial.println("-- coin is 1$ --");
}
else if (sub_amount >= 28 && sub_amount <= 32)
{
Serial.println("-- coin is 0.5$ --");
}
old_totalAmount = totalAmount;
}
if (totalAmount > 0 && countDownStarted)
{
if (timer >= intervalPerCoin * totalAmount)
{
Serial.println("Time is up!");
countDownStarted = false;
totalAmount = 0;
}
}
if (countDownTimer >= 1000 && countDownStarted)
{
countDownTimer = 0;
long timeLeft = ((intervalPerCoin * totalAmount) - timer) / 1000;
Serial.print("Time left: ");
Serial.println(timeLeft);
}
}