I have fried about 3 boards already, can anyone help me sort out my situation.
Is there something Im missing in my connection? TIA
#include <Servo.h>
#include <Arduino.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
// Servo for dispensing cigars for the single brand
Servo brand0Servo;
// Pin assignments and declarations
const int brand0ButtonPin = 4; // Button for Brand 0
const int coinPin = 2; // Pin connected to the coin mechanism
volatile int impulsCount = 0; // Counter for coin mechanism impulses
float totalAmount = 0.0; // Total amount inserted
int coinType = 0; // Coin type based on impulses
float coinValue; // Value of the detected coin
// Customizable brand name
const char *brand0Name = "Chesterfield"; // Set your brand name here
// Price for the cigar brand
const int brand0Price = 8;
// Coin pulse and value mapping
const int coinPulses[] = {1, 2, 5, 10};
const float coinValues[] = {1.0, 2.0, 5.0, 10.0};
// LCD object for displaying information
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis = 0; // Timer variable
const unsigned long interval = 3000; // 3-second interval
bool showExactAmount = true; // Flag to toggle message
// Lookup function to get coin value based on the number of impulses
float lookup(int pulses) {
for (int i = 0; i < sizeof(coinPulses) / sizeof(coinPulses[0]); i++) {
if (pulses == coinPulses[i]) {
return coinValues[i];
}
}
return -1; // Return -1 if no match found
}
// Setup function initializes the hardware and displays the welcome message
void setup() {
pinMode(brand0ButtonPin, INPUT_PULLUP);
pinMode(coinPin, INPUT);
attachInterrupt(digitalPinToInterrupt(coinPin), coinInterrupt, FALLING);
brand0Servo.attach(9); // Attach Brand 0 servo to pin 9
brand0Servo.write(0); // Set initial position for Brand 0
lcd.init();
lcd.clear();
lcd.backlight();
resetDisplay();
Serial.begin(9600);
}
// Interrupt handler for the coin mechanism
void coinInterrupt() {
impulsCount++;
}
// Main loop for handling user input and dispensing cigars
void loop() {
unsigned long currentMillis = millis();
// Toggle between messages every 3 seconds
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
showExactAmount = !showExactAmount;
resetDisplay();
}
// Handle coin insertion and update total amount
if (impulsCount > 0) {
coinValue = lookup(impulsCount);
if (coinValue > 0) {
totalAmount += coinValue;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Total Balance:");
lcd.setCursor(0, 1);
lcd.print(totalAmount, 2);
impulsCount = 0;
}
// Check button presses and provide feedback for zero or insufficient balance
if (digitalRead(brand0ButtonPin) == LOW) {
if (totalAmount == 0) {
displayMessage("Insert Coin", 2000);
} else if (totalAmount < brand0Price) {
displayMessage("Add More Coins", 2000);
}
}
// Dispense Brand 0 cigar if button is pressed and sufficient balance exists
if (digitalRead(brand0ButtonPin) == LOW && totalAmount >= brand0Price) {
dispenseCigar(brand0Servo, brand0Name, brand0Price);
}
}
// Function to display a temporary message and revert to the original
void displayMessage(const char *message, int duration) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
delay(duration);
resetDisplay();
}
// Function to dispense cigars for a specific brand
void dispenseCigar(Servo &servo, const char *brandName, int price) {
int cigars = totalAmount / price; // Calculate number of cigars to dispense
for (int i = cigars; i > 0; i--) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing ");
lcd.setCursor(0, 1);
lcd.print(String(brandName) + " " + String(i));
servo.write(80); // Rotate servo to dispense position
delay(500);
servo.write(0); // Return servo to initial position
delay(500);
}
// Reset total amount after dispensing
totalAmount = 0.0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" THANK YOU!");
delay(2000);
resetDisplay();
}
// Function to reset the LCD display to toggle messages
void resetDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
if (showExactAmount) {
lcd.print(" Exact Amount");
lcd.setCursor(0, 1);
lcd.print(" ONLY!");
} else {
lcd.print(" Cigar Vendo");
lcd.setCursor(0, 1);
lcd.print(" NO MINORS!");
}
}