Servo issue

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 :confused:

  int a = digitalRead(val1);
  int b = digitalRead(val2);
  int c = digitalRead(val3);

The pin names are meaningless. The values that the states are stored in are meaningless. We have NO idea what you are doing here.

  // 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);
 // }

This code obviously has NOTHING to do with your problem. Why did you clutter up your post by leaving this crap in?

Have you tried to write some code that does NOTHING but move the servo?

Why do you need a 16 channel servo controller to move ONE servo?

Thanks for your help.

I left that crap in as it is part of my code as is, I have commented it out for the moment whilst I try and solve the servo issue, but I thought it prudent to show you exactly what I am looking at and working with. If you read the original post you will know that I am not trying to write code that moves one servo, but moving one servo is where my issue currently lies, hence why I need a 16 channel servo controller, but what is the point in trying to drive 16 servos until I can figure out how to drive one using the controller.

I did state I was new to this and have so far managed to get an ESP8266 NodeMCU talking to an UNO, I have managed to have it connect to my router and serve a webpage, I have mangled to get it to display the required messages on an LCD and managed to make a stepper motor move in the way I want it to - so apologies if asking for a little guidance on why the servo won't move proves to be too much to ask.

Have a good day.

Anybody with a slightly better attitude care to give me a pointer?

Sorry, I don't have a better attitude...

cloudmad:
have so far managed to get an ESP8266 NodeMCU talking to an UNO, I have managed to have it connect to my router and serve a webpage, I have mangled to get it to display the required messages on an LCD and managed to make a stepper motor move in the way I want it to

That's like the complete opposite way of learning... It's like learning to drive a car but first going full speed before you learn how to take corners...

Start with the basics. Read some basic toturials and yeah, they might be not what you want to do. But get the basics first. So please go back and remove al the clutter in the code and start using propper names/comments. A good starting point is the Traps and tips guide of mister Gammon.

Do you see the "Servo moving" messages ?
Can you make the servos move using the library and a simpler program that does only that ?
I am not familiar with the servo library that you are using. Does it offer anything more than the standard Servo library ?

Hi UkHeliBob, yes Serial shows servo is moving in increments of 50, from 0 - 200, but servo doesn't move. It twitches when I first power up the controller but after that nothing. I'm using that library as my understanding is that the standard library won't work with the 16 channel servo controller, even with the example included in the adafruit library the servo won't move, I'm pretty sure it's not the servo as I have tested several and also managed to get the servo to move using the standard servo sweep example, but that doesn't use the 16 channel controller.

I know I am learning to run before I can walk but I did play with each of the elements i intend to use before coming up with this. I started with blink, then with servo, then stepper then ESP8266 as a web server making LED blink etc, and every other element of my project is working except the servo. I just hoped someone else may have experience with the same controller that may be able to point me in the right direction.

I also realise that some of the pin labels won't make sense but they correspond to pin labels in the script running on the ESP.

I am beginning to think it maybe power supply related although the controller is being powered by 5v from arduino Uno and the servo power comes from a separate 5v 1.6amp power supply which should be enough for 1 servo.

even with the example included in the adafruit library the servo won't move

That sounds like a hardware problem. Continuing to try to solve the software problem does not sound like good use of your time, to me.

cloudmad:
I also realise that some of the pin labels won't make sense but they correspond to pin labels in the script running on the ESP.

Don't care, the names are bad. Change them there as well :wink:

cloudmad:
I am beginning to think it maybe power supply related although the controller is being powered by 5v from arduino Uno and the servo power comes from a separate 5v 1.6amp power supply which should be enough for 1 servo.

So what about a schematic? (NOT a crappy fritzing breadboard view) And photo's? Most preferably not taken with a potato...

Don't care, the names are bad. Change them there as well :wink:

What should I change them to in order to be good?

Will take a picture later as I only have a potato on me at the moment.

Thanks PaulS, I will investigate the hardware side before doing anything else to the code. I guess if I can get it working as expected with the library included in the example it would be a start and at least eliminate any possible hardware issues.

What do the pins actually do? What's connected to them? Are they buttons? Then call them buttonPin. While you're at it, place them in an array (and give it a plural name) and make you life easy.

const byte ButtonPins[] = {2, 3 4};

And while we are at it, give every pin a name. Why are the stepper motor pins nameless?

const byte StepperDirectionPin = 8;
const byte StepperPulsePin = 9;

Thanks!! That will make things easier. The reason I hadn't given them specific names was because on the ESP I didn't want to use one pin for every button on the web page otherwise I would quickly run out of pins, so instead I have used three pins, if button one is pressed on the webpage it will result in pin 1 staying low pin 2 staying low and pin 3 going high, this way I can use the combination of three pins for more than just 3 buttons. So it didn't make sense to me to give them a button specific name as the pins are used over and over again for different buttons.

Thanks for your tip on the array though!