Fiz este codigo para o meu projeto de final de curso só que gostava que mais alguem o visse antes de eu encomendar as peças e o montar, só para ter a certeza que está tudo certo. Neste projeto utlizo um moedeiro 616 como forma de saber quais as moedas que recebem, graças aos seus impulsos. Os servos são onde os produtos estarão na maquina, consequentemente, o servos serão rodados, dependendo de qual botão for pressionado e então o produto sairá
Agredeço desde já:
#include <Wire.h> // Biblioteca para I2C
#include <LiquidCrystal_I2C.h> // Biblioteca para o LCD I2C
#include <EEPROM.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int Moedeiro = 2; // Pino do moedeiro
volatile int impulsoCount = 0; // Contador de impulsos do sensor
float Total = 0.0; // total no momento
const float PrecoProduto = 1.0;
const int button1 = 3;
const int button2 = 4;
const int button3 = 5;
const int button4 = 6;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
void setup() {
Serial.begin(9600);
lcd.begin(); //inicia o lcd
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Venduino"); //escrever venduino (nome projeto)
delay(2000);
lcd.clear();
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
servo1.attach(9);
servo2.attach(10);
servo3.attach(11);
servo4.attach(12);
// Configurar o moedeiro
pinMode(Moedeiro, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Moedeiro), contarImpulsoCredito, FALLING);
// guardar na memoria do arduono
EEPROM.get(0, Total);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Total: ");
lcd.print(Total, 2); // show the total value in 2 numebers
Serial.print("Total: ");
Serial.println(Total, 2);
if (Total >= PrecoProduto) { // if the value its the same or more than the price
lcd.setCursor(0, 1);
lcd.print("Escolha o Produto");
// Verify if the button has been pressed
if (digitalRead(button1) == LOW) {
largarProduto(servo1);
} else if (digitalRead(button2) == LOW) {
largarProduto(servo2);
} else if (digitalRead(button3) == LOW) {
largarProduto(servo3);
} else if (digitalRead(button4) == LOW) {
largarProduto(servo4);
}
} else {
lcd.setCursor(0, 1);
lcd.print("Insira as Moedas");
}
delay(500); // delay for the lcd
}
// used to do the impulses of the coin detector
void contarImpulsoCredito() {
impulsoCount++;
delay(30); // Debouncing
// Every impluse value corresponds to money value
if (impulsoCount == 1) {
Total += 1.00; // add 1 Euro
Serial.println("Moeda: 1 Euro");
} else if (impulsoCount == 2) {
Total += 0.50; // adidciona 50 cêntimos
Serial.println("Moeda: 50 Centimos");
} else if (impulsoCount == 3) {
Total += 0.20; // add 20 cêntimos
Serial.println("Moeda: 20 Centimos");
} else if (impulsoCount == 4) {
Total += 0.10; // adiciona mais 10 cêntimos
Serial.println("Moeda: 10 Centimos");
}
// Reset the impulse counter
impulsoCount = 0;
// save in arduinos memory
EEPROM.put(0, Total);
}
// Function used to liberate the product with servos
void largarProduto(Servo servo) {
if (Total >= PrecoProduto) {
servo.writeMicroseconds(2000); // Rotate servo
delay(950);
servo.writeMicroseconds(1500); // Stop Servo
delay(500);
// Subctracts the total value for the product price
Total -= PrecoProduto;
// save in arduinos memory
EEPROM.put(0, Total);
// clean lcd
lcd.clear();
Serial.println("Produto Liberado!");
Serial.print("Credito Restante: ");
Serial.println(Total, 2);
}
}