Hi all, i am new to arduino and probably biting off more than i can chew with my current project, but I have got quite far and have now hit a problem, would very much appreciate some wiser eyes than mine to take a look and see if there is anything I am missing!
My project is basically an automated bar, using a stepper motor to move a glass alone a rail, once it has moved the desired amount of steps a servo will be turned through 90 degrees, the servo is attached to an optic and the 90 degree turn will open the optic.
The drink is selected via a web browser. The webpage is served through an ESP8266 NodeMCU, which is connected to an UNO. The UNO is also connected to a 20x4 Blue LCD which is attached on SDA SCL pins, a 16 channel servo driver is attached to the UNO using pins A4 & A5.
I have most parts working, the ESP8266 connects to my router and serves up a small web page with 2 drink options, when i select a drink the drink name is displayed on the LCD and the stepper motor turns moving the glass to the desired position, however at that point it should turn the servo, but it doesn't and for the life of me i can't figure out why.
int led = 13;
int val1 = 2;
int val2 = 3;
int val3 = 4;
#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 771 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 2193 // this is the 'maximum' pulse length count (out of 4096)
void setup() {
Serial.begin(9600);
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
// Print a message to the LCD.
//lcd.setCursor(0, 1);
lcd.print("Welcome to the Bar!");
pinMode(val1, INPUT);
pinMode(val2, INPUT);
pinMode(val3, INPUT);
pinMode(led, OUTPUT);
pinMode(8, OUTPUT); // for stepper
pinMode(9, OUTPUT); // for stepper
digitalWrite(8, LOW);//direction control
digitalWrite(9, LOW);// for pulses of stepper
Serial.println("Set up running...");
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
Serial.println("... Set up completed");
}
void loop() {
int a = digitalRead(val1);
int b = digitalRead(val2);
int c = digitalRead(val3);
if(a==0 && b==0 && c==1)
{
digitalWrite(led,HIGH);
Serial.println("first flavor");
lcd.setCursor(0, 1);
lcd.print("Long Island Iced Tea");
// first stepper will move then servos will move
//stepper code
//for(int z=1 ; z<200 ; z++)
//{
//digitalWrite(9, HIGH);
//delay(1);
// digitalWrite(9, LOW);
// delay(1);
// }
a=0;
b=0;
c=0;
for(int x=0; x<200; x=x+50)
{
// double pulselen_x;
// pulselen_x= map(x,0,255,SERVOMIN,SERVOMAX);
pwm.setPWM(0, 1000, 2000);
//output
Serial.print("Servo 1 running ");
Serial.println(x);
}
}
else if(a==0 && b==1 && c==0)
{
digitalWrite(led,LOW);
Serial.println("second flavor");
// first stepper will move then servos will move
//stepper code
//for(int z=1 ; z<200 ; z++)
// {
// digitalWrite(9, HIGH);
// delay(1);
// digitalWrite(9, LOW);
// delay(1);
// }
lcd.setCursor(0, 1);
lcd.print("Gin & Tonic ");
a=0;
b=0;
c=0;
for(int y=0; y<200; y=y+50)
{
// double pulselen_y;
// pulselen_y= map(y,0,255,SERVOMIN,SERVOMAX);
pwm.setPWM(1, 771, 1798);
//output
Serial.print("servo 2 running");
Serial.println(y);
}
}
}
I realise that most of this code probably isn't the best way of doing what I'm doing, but I am learning and still very new to this, I am hoping to learn better ways of doing this throughout this journey that will hopefully allow me to find these better ways and clean up this code ![]()