Go Down

Topic: Help with code for cat feeder with servo and lcd counter (Read 165 times) previous topic - next topic

malimaks

Hello to all.
I found sketch (on internet) for cat feeder with servo, and other sketch for counting button press with output on lcd 16x2.
I try to combine this two sketch in one and I have working (without error) code, but I have problem because sometimes servo start but I dont have count on LCD.
If some could please check where is a mistake?

Thanks in advance.
Robert

Code: [Select]
#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a =0;
int e = 8;

Servo myservo;

const int servoPin = 9;
const int buttonPin = 7;
const int ledPin = 13;

void setup() {
  myservo.attach(servoPin);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  myservo.write(180);
  delay(1000);
  myservo.detach();
  ///////
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("How much food");
  lcd.setCursor(0, 1);
  lcd.print("today");
  pinMode(e,INPUT);
  ////////
}
///////////////////
void loop() {
  int buttonVal = digitalRead(buttonPin);
  if(buttonVal == LOW)
  {
    myservo.attach(servoPin);
    myservo.write(30);
    delay(575);
    myservo.write(180);
    delay(1500);
    myservo.detach();
    delay(5000);
  }
  delay(13);
  ///////
    int button = digitalRead(e);
   lcd.setCursor(10, 1);
    lcd.print(a);
 
  if (button == LOW) {
    a ++;
    lcd.setCursor(10, 1);
    lcd.print(a);
     delay(50);
    }
}

PaulS

Why are you connecting the switch to two different pins? Why are you enabling the internal pullup resistor on only one of them?
The art of getting good answers lies in asking good questions.

Go Up