Thursday 26 December 2019

DIY Vending Machine Correct Program

DIY VENDING MACHINE


Circuit Diagram:-


Program:-


#include <LiquidCrystal.h> 
#include <Servo.h>
LiquidCrystal lcd(27, 26, 25, 24, 23, 22); 
Servo servo1, servo2, servo3, servo4;   
#define dirPinVertical 0
#define stepPinVertical 1
#define dirPinHorizontal 2
#define stepPinHorizontal 3
#define coinDetector 9
#define button1 13
#define button2 12
#define button3 11
#define button4 10
#define microSwitchV 15
#define microSwitchH 14
int buttonPressed;
void setup() {
  lcd.begin(16, 2);
  servo1.attach(4);
  servo2.attach(5);
  servo3.attach(6);
  servo4.attach(7);
  pinMode(dirPinVertical, OUTPUT);
  pinMode(stepPinVertical, OUTPUT);
  pinMode(dirPinHorizontal, OUTPUT);
  pinMode(stepPinHorizontal, OUTPUT);
  pinMode(coinDetector, INPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(microSwitchV, INPUT_PULLUP);
  pinMode(microSwitchH, INPUT_PULLUP);
  digitalWrite(dirPinVertical, HIGH); 
  while (true) {
    if (digitalRead(microSwitchV) == LOW) { 
      moveUp(70);
      break;
    }
    digitalWrite(stepPinVertical, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPinVertical, LOW);
    delayMicroseconds(300);
  }
  digitalWrite(dirPinHorizontal, LOW);
  while (true) {
    if (digitalRead(microSwitchH) == LOW) {
      moveLeft(350);
      break;
    }
    digitalWrite(stepPinHorizontal, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPinHorizontal, LOW);
    delayMicroseconds(300);
  }
}
void loop() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Insert the coin!");
  while (true) {
    if (digitalRead(coinDetector) == LOW) { 
      break;
    }
  }
  delay(10);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Select the item you want");
  lcd.setCursor(0, 1);
  lcd.print(" 1, 2, 3 or 4?");
  while (true) {
    if (digitalRead(button1) == LOW) {
      buttonPressed = 1;
      break;
    }
    if (digitalRead(button2) == LOW) {
      buttonPressed = 2;
      break;
    }
    if (digitalRead(button3) == LOW) {
      buttonPressed = 3;
      break;
    }
    if (digitalRead(button4) == LOW) {
      buttonPressed = 4;
      break;
    }
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Please wait");
  switch (buttonPressed) {
    case 1:
      moveUp(4900); 
      delay(200);
      moveLeft(1700); 
      delay(300);
      servo1.writeMicroseconds(2000); 
      delay(950);
      servo1.writeMicroseconds(1500); 
      delay(500);
      moveRight(1700);
      delay(200);
      moveDown(4900);
      break;
    case 2:
      moveUp(4900);
      delay(200);
      servo2.writeMicroseconds(2000); 
      delay(950);
      servo2.writeMicroseconds(1500); 
      delay(500);
      moveDown(4900);
      break;
    case 3:
      moveUp(2200); 
      delay(200);
      moveLeft(1700);
      delay(300);
      servo3.writeMicroseconds(2000); 
      delay(950);
      servo3.writeMicroseconds(1500); 
      delay(500);
      moveRight(1700);
      delay(200);
      moveDown(2200);
      break;
    case 4:
      moveUp(2200); 
      delay(200);
      servo4.writeMicroseconds(2000); 
      delay(950);
      servo4.writeMicroseconds(1500);  
      delay(500);
      moveDown(2200);
      break;
  }
  
  lcd.clear(); 
  lcd.setCursor(0, 0);
  lcd.print("Delivered Sucessfully"); 
  delay(2000);
}
void moveUp (int steps) {
  digitalWrite(dirPinVertical, LOW);
  for (int x = 0; x < steps; x++) {
    digitalWrite(stepPinVertical, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPinVertical, LOW);
    delayMicroseconds(300);
  }
}
void moveDown (int steps) {
  digitalWrite(dirPinVertical, HIGH);
  for (int x = 0; x < steps; x++) {
    digitalWrite(stepPinVertical, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPinVertical, LOW);
    delayMicroseconds(300);
  }
}
void moveLeft (int steps) {
  digitalWrite(dirPinHorizontal, HIGH);
  for (int x = 0; x < steps; x++) {
    digitalWrite(stepPinHorizontal, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPinHorizontal, LOW);
    delayMicroseconds(300);
  }
}
void moveRight (int steps) {
  digitalWrite(dirPinHorizontal, LOW);
  for (int x = 0; x < steps; x++) {
    digitalWrite(stepPinHorizontal, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPinHorizontal, LOW);
    delayMicroseconds(300);
  }
}

No comments:

Post a Comment

google-site-verification: google18ae80885fc3d876.html