code for beermachine

this is code for a beermachine :slight_smile:

#include <Servo.h>
#include <LiquidCrystal.h>

Servo myservo;
Servo secondserv0;// create servo object to control a servo
// a maximum of eight servo objects can be created

int pos =0; // variable to store the servo position
const int buttonPin = 7;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonState = 0;
void setup()
{

secondserv0.attach(1);
myservo.attach(0); // attaches the servo on pin 0 to the servo object
secondserv0.write(180);
myservo.write(90);
pinMode(buttonPin, INPUT);

}

void loop()
{
lcd.begin(16, 2);

lcd.print("Beer Machine");

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
lcd.clear();
lcd.print("Pouring beer...");
for(pos = 90; pos >= 45; pos -= 1) // goes from 90 to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos >= 90; pos -= 1) // goes from 90 to 180 degrees
{ // in steps of 1 degree.
// 90 degrees is stopped.

secondserv0.write(pos); // rotate servo at speed given by 'angle'

delay(100); // waits 20ms between servo commands
}
{
delay(2000);

}

for(pos = 90; pos < 180; pos += 1)// goes from 180 to 90 degrees
{
secondserv0.write(pos); // rotate at a speed given by 'angle'

}

for(pos = 45; pos < 90; pos += 1)// goes from 180 to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(300); // waits 15ms for the servo to reach the position
}
}else {

}

}

Start here

for(pos = 90; pos >= 45; pos -= 1)  // goes from 90 to 180 degrees

Really?

Does the machine make beer?

There are so many places in that code where the comments don't match the code that it is almost as if the comments were from a different program.

    for(pos = 90; pos >= 45; pos -= 1)  // goes from 90 to 180 degrees
    for(pos = 180; pos >= 90; pos -= 1)  // goes from 90 to 180 degrees
      secondserv0.write(pos);        // rotate servo at speed given by 'angle'
    for(pos = 45; pos < 90; pos += 1)// goes from 180 to 90 degrees

Also

  else 
  {
  }

else what ?

    {
      delay(2000);
    }

Why the braces ?

Are these real servos or continuous rotation servos ? The code says one thing, the comments another.

      secondserv0.write(pos);        // rotate servo at speed given by 'angle'

As a matter of interest do you have a pulldown resistor in your button circuit or is the machine likely to dispense beer at random due to picking up noise ?

UKHeliBob:
As a matter of interest do you have a pulldown resistor in your button circuit or is the machine likely to dispense beer at random due to picking up noise ?

Mmmm... random beer service. I can live with that.