#include <IRremote.h>
#include <TM1637Display.h>
#include <Stepper.h>
// Pin definitions
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int buzzerPin = 8;
const int resetButtonPin = 7;
const int rotateButtonPin = 6;
const int motorPin1 = 2;
const int motorPin2 = 3;
const int motorPin3 = 4;
const int motorPin4 = 5;
const int potPin = A0;
const int irPin = 12;
const int clkPin = 11; // TM1637 Clock pin
const int dioPin = 10; // TM1637 Data pin
// IR remote setup
IRrecv irrecv(irPin);
decode_results results;
// 7-segment display setup
TM1637Display display(clkPin, dioPin);
// Stepper motor setup
const int stepsPerRevolution = 200; // Adjust as needed
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);
// Prices and items
const int prices[] = {1000, 2000, 3000};
const int numItems = 3;
int currentItem = 0;
int currentPrice = 0;
int balance = 0;
void setup() {
// Initialize pins
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(rotateButtonPin, INPUT_PULLUP);
pinMode(potPin, INPUT);
// Initialize IR receiver
irrecv.enableIRIn();
// Initialize 7-segment display
display.setBrightness(0x0f);
// Initial display
displayPrice();
}
void loop() {
// Check for reset button press
if (digitalRead(resetButtonPin) == LOW) {
balance = 0;
display.showNumberDec(balance);
delay(500); // Debounce delay
}
// Check for rotate button press
if (digitalRead(rotateButtonPin) == LOW) {
rotateItems();
delay(500); // Debounce delay
}
// Read potentiometer to determine current item
int potValue = analogRead(potPin);
currentItem = getCurrentItem(potValue);
currentPrice = prices[currentItem];
// Display current price
displayPrice();
// Check for IR remote input
if (irrecv.decode(&results)) {
if (results.value == 0xFF30CF) { // Example code, replace with your remote's code
balance += 1000;
display.showNumberDec(balance);
delay(500); // Debounce delay
}
irrecv.resume();
}
// Check if balance is sufficient to dispense item
if (balance >= currentPrice) {
dispenseItem();
} else if (balance < currentPrice && balance > 0) {
display.showNumberDec(8); // Show 8 if balance is insufficient
digitalWrite(buzzerPin, HIGH); // Sound the buzzer
delay(1000); // Sound duration
digitalWrite(buzzerPin, LOW);
display.showNumberDec(balance); // Reset display to current balance
}
}
int getCurrentItem(int potValue) {
if (potValue <= 300) return 0;
else if (potValue <= 600) return 1;
else return 2;
}
void displayPrice() {
switch (currentItem) {
case 0:
setColor(255, 0, 0); // Red for item 1
break;
case 1:
setColor(0, 255, 0); // Green for item 2
break;
case 2:
setColor(0, 0, 255); // Blue for item 3
break;
}
display.showNumberDec(currentPrice);
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void dispenseItem() {
myStepper.step(stepsPerRevolution); // Adjust the number of steps as needed
balance -= currentPrice;
display.showNumberDec(balance);
delay(500); // Debounce delay
setColor(255, 255, 255); // Set RGB LED to white after dispensing
}
void rotateItems() {
// Logic to rotate items (e.g., change display color to white)
setColor(255, 255, 255); // Set RGB LED to white
}