Braces and if statement

I have been working on this for months and am really close. It is a fish feeder. There will be a gnome fishing and using a mp3 player he calls the fish. I am very proud of myself. I have solved a lot of problems not just coding but 3d printing and using hardware too. My family is visiting next week and I am hoping to finish it. Could I get some clue as to how to fix my latest problem I am pretty sure it is the braces. The fish feeder goes off at the first feed but continues to go through feeds 2, 3 and 4. Just a clue please to start me off?

/// Richard's fish pond sketch
/// Using the existing automatic fish feeder manual button
/// I will program an arduino to keep real time and play a
/// sound before the programmed fish feeding time. It will
/// then operate a servo to press the override button on the
/// fish feeder to release fish food. There will be an
/// over-ride button on the exterior of the box to operate it
/// at any-time.It will play a voice recording on a MP3 player.
///
/// Featuring rtc1307, MP3, thermometer, LCD, button
/// still to add humidity and temp DHT11
/// MP3
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(0, 1); // RX, TX10,11
DFRobotDFPlayerMini myDFPlayer;
////////rtc1307
#include "RTCDS1307.h"
RTCDS1307 rtc(0x68);
///LCD
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
////Thermometer
#include <DallasTemperature.h>
#include <OneWire.h>
int pDigitalTemp = 7;
int deviceCount = 0;
float tempC;
OneWire oneWire(pDigitalTemp);
DallasTemperature sensor ( &oneWire);
////RTClock
uint8_t year, month, weekday, day, hour, minute, second;
bool period = 0;
String m[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String w[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
/////
///Button
const int buttonPin1 = 2;     // the number of the pushbutton pin
int buttonState1 = 0;         // variable for reading the pushbutton status

////Servo
#include <Servo.h>
int pos = 30;
Servo feed;

void setup()
{
  ////servo
  feed.attach(A3);///was 4
  ////button
  pinMode(buttonPin1, INPUT);
  mySoftwareSerial.begin(9600);///////
  if (!myDFPlayer.begin(mySoftwareSerial));
  Wire.begin();///temp?
  ///Serial.begin(9600); Again, this prevents MP3 player from working?

  rtc.begin();
  ///rtc.setDate(24, 1, 23);
 /// rtc.setTime(9, 54, 01); //// hour, minute, second
  ///Initiate the LCD
  lcd.init();
  lcd.backlight();
  lcd.begin(16, 2);  // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.backlight(); // LCD backlight on
  lcd.clear();
  delay(100);
}

void loop()
{
  rtc.getDate(year, month, day, weekday);
  rtc.getTime(hour, minute, second, period);
  if (!(second % 3)) rtc.setMode(1 - rtc.getMode());
  rtc.getTime(hour, minute, second, period);
  lcd.clear();
  lcd.setCursor(0, 0);
  if (hour == 11) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("operate servo");
  }
  delay(200);
  lcd.clear();
  lcd.setCursor(6, 0);
  lcd.print("Time ");
  lcd.setCursor(5, 1);
  lcd.print(hour, DEC);
  lcd.print(':');
  if ( minute < 10 ) {
    lcd.print('0');
  }
  lcd.print(minute, DEC);
  lcd.print(rtc.getMode() ? (period ? " PM" : " AM") : "");
  delay(2000); // delay 1 seconds
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Pond temperature");
  {
    sensor.requestTemperatures();
        lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Pond Temperature");
    lcd.setCursor(1, 1);
    lcd.print(sensor.getTempCByIndex(0));
     lcd.print((char)223);
    lcd.print("Celcius");
    delay(3000);
  }
  /// calculate time until next feed?
  ///operate OLEDs?
  /// First feed
  if (hour == 10) {
    if (minute == 14) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Breakfast!");
      delay(100);///1800000
      myDFPlayer.volume(30);///set volume to 30
      myDFPlayer.play(001);
      delay(10000);
      myDFPlayer.play(001);
      delay(8000);

      ///now operate servo
      {
        for (pos = 30; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        delay(1200);
        for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
      }
      }
  }
  ///2nd feed
  if (hour == 13) {
    if (minute == 00) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Lunch!");
      delay(180);
      myDFPlayer.volume(20);///set volume to 30
      myDFPlayer.play(001);
      delay(10000);
      myDFPlayer.play(001);
      delay(8000);

      ///now operate servo
      {
        for (pos = 30; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        delay(1200);
        for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
      }
    }
  }
  /// 3rd feed
  if (hour == 16) {
    if (minute == 00) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Afternoon snack");
      delay(180);
      myDFPlayer.volume(20);///set volume to 30
      myDFPlayer.play(001);
      delay(10000);
      myDFPlayer.play(001);
      delay(8000);

      ///now operate servo
      {
        for (pos = 30; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        delay(1200);
        for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
      }
    }
  }
  /// 4th feed
  if (hour == 18) {
    if (minute == 00) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Dinner time!");
      delay(180);
      myDFPlayer.volume(20);///set volume to 30
      myDFPlayer.play(001);
      delay(10000);
      myDFPlayer.play(001);
      delay(8000);

      ///now operate servo
      {
        for (pos = 30; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        delay(1200);
        for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          feed.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
      }
    }
  }
  ///}

  buttonState1 = digitalRead(buttonPin1);
  ///  check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    delay(100);///1800000
    myDFPlayer.volume(20);///set volume to 30
    myDFPlayer.play(001);
    delay(10000);
    myDFPlayer.play(001);
    delay(8000);

    ///now operate servo
    {
      for (pos = 30; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
        feed.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15 ms for the servo to reach the position
      }
      delay(1200);
      for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        feed.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15 ms for the servo to reach the position
      }
      }
  }
}

"Braces"

What indications (audio, LCD, servo) do you get when the feedings do not stop?

1 Like
  • Placing delay(. . .) in your sketch should be avoid.

  • When this evaluates, how many times will the code in the { } braces executed ?

  if (hour == 11) 
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("operate servo");
  }

At a glance, besides a few unnecessary pairs of { braces } and one or two not lined up for humans, this does not look like the problem you think it is.

I see

SoftwareSerial mySoftwareSerial(0, 1); // RX, TX10,11

and advise you to move the software serial off the hardware serial pins.

Does the time and date such as you publish to the LCD seem plausible?

a7

1 Like

Many prefer to use INPUT_PULLUP on the buttons and buttonState == LOW.

Easy enough to check curley braces "pairs" to see if they are in the correct places and if enough/too many etc.
Click beside each and the corresponding one will be shown.

1 Like

Alternatively, since Serial isn't in use in the program, delete the SoftwareSerial instance and change this line

to

1 Like

Yet. But soon enough Imma probably say that serial printing may be a better verify and tracing means than using the LCD.

a7

1 Like

Is this why Serial.begin(9600); prevents MP3 player from working?

Braces are curly brackets. Curly braces would be curly curly brackets!

1 Like

Yes, because it is also trying to use pins 0 and 1.

1 Like

Thanks Larry, I am aware of this and have studied using milliseconds instead but cannot get my head around it. I appreciate your comments.

Thanks Bluejets. I had noticed the "pairs" tip on the sketch but had not really understood it. Thanks to your comment I have a better understanding and am using it a lot to assist me. I appreciate your comments.

Thanks Alto77. It was not the braces, thanks. I am beginning to get an understanding
of the pins. I will not change them as I have soldered them on a shield.
I appreciate your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.