Advertisement
pleasedontcode

"Vending Simulation" rev_01

Dec 9th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Vending Simulation"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-09 21:56:20
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project aims to create an interactive Arduino */
  21.     /* application that utilizes connected components for */
  22.     /* sensor data collection and real-time feedback. The */
  23.     /* code should efficiently manage input/output */
  24.     /* operations and ensure reliable performance. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h> // Include Wire library for I2C communication
  31. #include <Servo.h> // Include Servo library for controlling servo motors
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. // Define items, prices, and stock for the vending machine
  38. const char* items[] = {"Soda", "Chips", "Candy"}; // Example items
  39. float prices[] = {1.50, 1.00, 0.75}; // Example prices
  40. int stock[] = {5, 5, 5}; // Example stock levels
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize serial communication for debugging
  45.     Serial.begin(9600);
  46.    
  47.     // Initialize I2C communication
  48.     Wire.begin();
  49.    
  50.     // Initialize servo motors (if used)
  51.     // Servo myServo; // Uncomment and define your servo if needed
  52.     // myServo.attach(9); // Attach to pin 9 (example)
  53. }
  54.  
  55. void loop(void)
  56. {
  57.     // Display available items and their prices
  58.     Serial.println("Available items:");
  59.     for (int i = 0; i < sizeof(items) / sizeof(items[0]); i++) {
  60.         Serial.print(i + 1);
  61.         Serial.print(". ");
  62.         Serial.print(items[i]);
  63.         Serial.print(" - $");
  64.         Serial.print(prices[i]);
  65.         Serial.print(" (Stock: ");
  66.         Serial.print(stock[i]);
  67.         Serial.println(")");
  68.     }
  69.  
  70.     // Simulate user input for item selection
  71.     int choice = 1; // Example choice (this should be replaced with actual input handling)
  72.     if (choice < 1 || choice > sizeof(items) / sizeof(items[0])) {
  73.         Serial.println("Invalid choice.");
  74.         return;
  75.     }
  76.     if (stock[choice - 1] == 0) {
  77.         Serial.println("Out of stock.");
  78.         return;
  79.     }
  80.     int item_index = choice - 1;
  81.  
  82.     // Simulate payment
  83.     float payment = 2.00; // Example payment (this should be replaced with actual input handling)
  84.     if (payment < prices[item_index]) {
  85.         Serial.println("Insufficient payment. Transaction cancelled.");
  86.         return;
  87.     }
  88.     float change = payment - prices[item_index];
  89.     Serial.print("Payment accepted. Change to return: $");
  90.     Serial.println(change);
  91.    
  92.     // Dispense the selected item
  93.     stock[item_index] -= 1;
  94.     Serial.print("Dispensing ");
  95.     Serial.println(items[item_index]);
  96.  
  97.     delay(5000); // Delay for 5 seconds before the next loop iteration
  98. }
  99.  
  100. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement