Hello,
I made a project that consists in 4 servos, 1 IR sensor (coin detector), 1 bluetooth module HC-05.
The project is a vending machine made with arduino which, when a coin is throwed and the buttons in the mobile are pressed, the motor that corresponds to the button pressed moves (there's also an LCD screen).
The problem is that project doesn't work. I think the problem is in the code, in the part of the bluetooth. I use Roboremo for sending A, B, C or D to the bluetooth module.
Can you help me please fixing the code? Thanks
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Servo.h>
LiquidCrystal lcd(27, 26, 25, 24, 23, 22);
Servo myServo1, myServo2, myServo3, myServo4;
#define coinDetector 9
// the pins for the HC-05:
int TxD = 10;
int RxD = 11;
SoftwareSerial bluetooth(TxD, RxD);
int servoposition;
int servopos;
int new1;
void setup() {
int pos=0;
myServo1.write(0);
myServo2.write(0);
myServo3.write(0);
myServo4.write(0);
myServo1.attach(1);
myServo2.attach(3);
myServo3.attach(0);
myServo4.attach(2);
Serial.begin(9600);
bluetooth.begin(9600);
lcd.begin(16,2);
pinMode(coinDetector, INPUT);
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insert a coin!"); // just messages for the LCD, no problem
while (true) {
if (digitalRead(coinDetector) == LOW) { // if the coindetector detect a coin, continue
break;
}
}
delay(10);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item"); //another messages
lcd.setCursor(0, 1);
lcd.print(" 1, 2, 3 or 4?"); //more messages
// this is the part I'm not sure if it's coded properly
if (bluetooth.available()){
String value = bluetooth.readString();
servoposition = value.toInt();
int servopos = bluetooth.read();
Serial.println(servopos);
myServo1.write(servopos);
myServo2.write(servopos);
myServo3.write(servopos);
myServo4.write(servopos);
//this I'm not sure too:
if (value.toInt() == "A"){
Serial.println(servoposition);
myServo1.write(360);
}
if (value.toInt() == "B"){
Serial.println(servoposition);
myServo2.write(360);
}
if (value.toInt() == "C"){
Serial.println(servoposition);
myServo3.write(360);
}
if (value.toInt() == "D"){
Serial.println(servoposition);
myServo4.write(360);
}
}
servopos++;
delay(30);
Serial.println(servopos);
myServo1.write(servopos);
myServo2.write(servopos);
myServo3.write(servopos);
myServo4.write(servopos);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering..."); //the final message
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); //ups!, this is the really final
delay(2000);
}
Please help me, all the libraries are uploaded. What's the problem?