Advertisement
Patatofour

Untitled

Nov 18th, 2020 (edited)
1,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 16.51 KB | None | 0 0
  1. /*
  2.  
  3. Automatic Auger Audiono pet feeder Copyright Roger Donoghue 28/03/2015 all rights reserved. For personal use only.
  4. Not for commercial use or resale.
  5. Allows you to set 2 feeding times and the quantity as a multiple of the default feed quantity.
  6. Uses a DS1307 real time clock to keep the time, with a rechargable battery built in.
  7. (You can use the arduino RTC example code in the IDE to set the clock , or use the rotary encoder as intended)
  8.  
  9. */
  10. // include the library code:
  11. #include <Time.h>
  12. #include <TimeLib.h>
  13. #include <LiquidCrystal_I2C.h>
  14. #include <Wire.h>      // needed for the RTC libraty
  15. #include <Time.h>
  16. #include <DS1307RTC.h> // Real Time Clock Library
  17. #include <Servo.h>
  18. int feedpin = 7;  //test d'activation de pin
  19. int feedlight = 13;  //test d'activation de pin
  20. int feedpin2 = 8;  //fake pour test n'importe quoi
  21. int feedpin3 = 8; //fake pour test n'importe quoi
  22.  
  23. // initialize the library with the numbers of the interface pins dor the LCD
  24. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  25.  
  26. Servo feedServo;
  27. Servo stirServo;
  28. int pos = 0;
  29. volatile boolean TurnDetected;
  30. volatile boolean up;
  31. const int PinCLK=2;                   // Used for generating interrupts using CLK signal
  32. const int PinDT=3;                    // Used for reading DT signal
  33. const int PinSW=4;                    // Used for the push button switch of the Rotary Encoder
  34. const int buttonPin = A3;             // the number of the pushbutton pin for manual feed 13
  35. int buttonState = 0;// variable for reading the manual feed pushbutton status
  36. int feed1hour = 22;                   // variables for feeding times and quantity
  37. int feed1minute = 04;
  38. int feed2hour = 22;
  39. int feed2minute = 06;
  40. int feed3hour = 22;
  41. int feed3minute = 08;
  42. int feedQty = 4;
  43. int feedRate = 800;   //a pwm rate the triggers forward on the servo 75
  44. int feedReversal = 80; //a pwm rate that triggers reverse on the servo
  45.                        // play with these numbers for your servo. Mine is a Futaba digital servo
  46.                        // that I removed the pot from and the plastic lug, to make it continuous.
  47.  
  48. void isr ()  {                    // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
  49.  if (digitalRead(PinCLK))         // this keeps an eye out for the rotary encoder being turned regardless of where the program is
  50.    up = digitalRead(PinDT);       // currently exectuting - in other words, during the main loop this ISR will always be active
  51.  else
  52.    up = !digitalRead(PinDT);
  53.  TurnDetected = true;
  54. }
  55.  
  56.  
  57. void setup ()  {
  58. //serial time read//
  59.   Serial.begin(9600);
  60.   while (!Serial) ; // wait for serial
  61.   delay(200);
  62.   Serial.println("DS1307RTC Read Test");
  63.   Serial.println("-------------------");
  64.    // set up the LCD's number of columns and rows:
  65.   lcd.begin(16, 2);
  66.   pinMode(feedpin, OUTPUT); //test d'activation de pin pour le moteur
  67.   pinMode(feedlight, OUTPUT);
  68.   // setup the Rotary encoder
  69.  pinMode(PinCLK,INPUT);
  70.  pinMode(PinDT,INPUT);  
  71.  pinMode(PinSW,INPUT);
  72.  pinMode(buttonPin, INPUT);
  73.  
  74.  attachInterrupt (0,isr,FALLING);   // interrupt 0 is always connected to pin 2 on Arduino UNO
  75.     lcd.setCursor(17,0);          
  76.     lcd.print("JF Larouche");  // A bit of fun :-)
  77.     /*
  78.     lcd.setCursor(17,1);
  79.     lcd.print("  deer feeder");
  80.      for (int positionCounter = 0; positionCounter < 17; positionCounter++) {
  81.      // scroll one position left:
  82.      lcd.scrollDisplayLeft();
  83.      // wait a bit:
  84.      delay(150);
  85.           }
  86.       delay(3000);
  87.       for (int positionCounter = 0; positionCounter < 17; positionCounter++) {
  88.      // scroll one position left:
  89.      lcd.scrollDisplayRight();
  90.      // wait a bit:
  91.       */
  92.      delay(500);
  93.      
  94.        //      }  // end of fun
  95.    
  96.      lcd.setCursor(17,0);
  97.      lcd.print("                ");
  98.      lcd.setCursor(17,1);
  99.      lcd.print("                ");  
  100.         }
  101.  
  102. void loop ()  {  //Main program loop - most things in here!
  103.    
  104.    //timeread
  105.    tmElements_t tm;
  106.  
  107.   if (RTC.read(tm)) {
  108.     Serial.print("Ok, Time = ");
  109.     print2digits(tm.Hour);
  110.     Serial.write(':');
  111.     print2digits(tm.Minute);
  112.     Serial.write(':');
  113.     print2digits(tm.Second);
  114.     Serial.print(", Date (D/M/Y) = ");
  115.     Serial.print(tm.Day);
  116.     Serial.write('/');
  117.     Serial.print(tm.Month);
  118.     Serial.write('/');
  119.     Serial.print(tmYearToCalendar(tm.Year));
  120.     Serial.println();
  121.   } else {
  122.     if (RTC.chipPresent()) {
  123.       Serial.println("The DS1307 is stopped.  Please run the SetTime");
  124.       Serial.println("example to initialize the time and begin running.");
  125.       Serial.println();
  126.     } else {
  127.       Serial.println("DS1307 read error!  Please check the circuitry.");
  128.       Serial.println();
  129.     }
  130.     delay(9000);
  131.   }
  132.   delay(1000);
  133.  
  134.  
  135. }
  136. void print2digits(int number) {
  137.   if (number >= 0 && number < 10) {
  138.     Serial.write('0');
  139.   }
  140.   Serial.print(number);
  141.    
  142.     static long virtualPosition=0;    // without STATIC it does not count correctly!!!
  143.     tmElements_t tm;    // This sectionm reads the time from the RTC, sets it in tmElements tm (nice to work with), then displays it.
  144.     RTC.read(tm);
  145.     lcd.setCursor(0, 0);
  146.     printDigits(tm.Wday); //call to print digit function that adds leading zeros that may be missing
  147.     lcd.print(":");
  148.     printDigits(tm.Hour);
  149.     lcd.print(":");
  150.     printDigits(tm.Minute);
  151.     lcd.print(":");
  152.     printDigits(tm.Second);
  153.     lcd.print("  ");
  154.     lcd.print("Qty ");
  155.     lcd.print(feedQty);
  156.     lcd.print(" ");
  157.     lcd.setCursor(0,1);
  158.     lcd.print("1)");
  159.     printDigits(feed1hour);
  160.     lcd.print(":");
  161.     printDigits(feed1minute);
  162.     lcd.print(" 2)");
  163.     printDigits(feed2hour);
  164.     lcd.print(":");
  165.     printDigits(feed2minute);
  166.     lcd.print(" 3)");
  167.     printDigits(feed3hour);
  168.     lcd.print(":");
  169.     printDigits(feed3minute);
  170.    
  171.    
  172.    
  173. // MAIN BREAKOUT "IF" SECION BELOW THAT MONITORS THE PUSH BUTTON AND ENTERS PROGRAMMING IF IT'S PUSHED
  174.  
  175.  if (!(digitalRead(PinSW))) {      // check if pushbutton is pressed
  176.                                    // if YES then enter the programming subroutine
  177.     lcd.blink();   // Turn on the blinking cursor:
  178.     lcd.setCursor(5,0);
  179.     lcd.print(" SET");
  180.     virtualPosition = tm.Hour;  //needed or the hour will be zero each time you change the clock.
  181.        do  {
  182.             lcd.setCursor(0,0);   // put cursor at Time Hour  
  183.             delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  184.             if (TurnDetected)  {        // do this only if rotation was detected
  185.             if (up)
  186.               virtualPosition--;
  187.             else
  188.             virtualPosition++;
  189.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  190.                                }
  191.             // Here I change the hour of time -
  192.             if (virtualPosition >= 24) {virtualPosition = 0;}
  193.             if (virtualPosition <= 0) {virtualPosition = 0;}
  194.             tm.Hour = virtualPosition;
  195.             RTC.write(tm);
  196.             lcd.setCursor(0, 0);
  197.             printDigits(tm.Hour);  // then re-print the hour on the LCD
  198.          } while ((digitalRead(PinSW)));  // do this "do" loop while the PinSW button is NOT pressed
  199.          lcd.noBlink();
  200.          delay(1000);
  201.        
  202.        //   SET THE MINS
  203.        
  204.         lcd.blink();   // Turn on the blinking cursor:
  205.        virtualPosition = tm.Minute;  //needed or the minute will be zero each time you change the clock.
  206.        do  {
  207.        lcd.setCursor(3,0);   // put cursor at Time Mins
  208.        delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  209.            if (TurnDetected)  {       // do this only if rotation was detected
  210.             if (up)
  211.               virtualPosition--;
  212.             else
  213.             virtualPosition++;
  214.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  215.                                }
  216.             // Here I change the min of time -
  217.             if (virtualPosition >= 60) {virtualPosition = 0;}
  218.             if (virtualPosition <= 0) {virtualPosition = 0;}
  219.             tm.Minute = virtualPosition;
  220.             RTC.write(tm);
  221.             lcd.setCursor(3, 0);
  222.             printDigits(tm.Minute);  // then re-print the min on the LCD
  223.          } while ((digitalRead(PinSW)));
  224.          lcd.noBlink();
  225.          delay(1000);
  226.    
  227.        //   SET THE QTY - Feed quantity
  228.        
  229.        lcd.blink();   // Turn on the blinking cursor:
  230.        virtualPosition = feedQty;  //needed or the qty will be zero.
  231.        do  {
  232.        lcd.setCursor(14,0);   // put cursor at QTY
  233.        delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  234.            if (TurnDetected)  {       // do this only if rotation was detected
  235.             if (up)
  236.               virtualPosition--;
  237.             else
  238.             virtualPosition++;
  239.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  240.                                }
  241.             // Here I change the feed qty
  242.             feedQty = virtualPosition;
  243.             lcd.setCursor(14, 0);
  244.             lcd.print(feedQty);
  245.            } while ((digitalRead(PinSW)));
  246.          lcd.noBlink();
  247.          delay(1000);
  248.          
  249.      //   SET THE Feed1 Hour
  250.        
  251.        lcd.blink();   // Turn on the blinking cursor:
  252.        virtualPosition = feed1hour;  //needed or will be zero to start with.
  253.        do  {
  254.        lcd.setCursor(2,1);   // put cursor at feed1hour
  255.        delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  256.            if (TurnDetected)  {       // do this only if rotation was detected
  257.             if (up)
  258.               virtualPosition--;
  259.             else
  260.             virtualPosition++;
  261.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  262.                                }
  263.             // Here I change the feed1 hour
  264.             if (virtualPosition >= 24) {virtualPosition = 0;}
  265.             if (virtualPosition <= 0) {virtualPosition = 0;}
  266.             feed1hour = virtualPosition;
  267.             lcd.setCursor(2,1);
  268.             printDigits(feed1hour);
  269.            } while ((digitalRead(PinSW)));
  270.          lcd.noBlink();
  271.          delay(1000);  
  272.  
  273.       //   SET THE Feed1 Mins
  274.        
  275.        lcd.blink();   // Turn on the blinking cursor:
  276.        virtualPosition = feed1minute;  //needed or will be zero to start with.
  277.        do  {
  278.        lcd.setCursor(5,1);   // put cursor at feed1minute
  279.        delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  280.            if (TurnDetected)  {       // do this only if rotation was detected
  281.             if (up)
  282.               virtualPosition--;
  283.             else
  284.             virtualPosition++;
  285.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  286.                                }
  287.             // Here I change the feed1 minute
  288.             if (virtualPosition >= 60) {virtualPosition = 0;}
  289.             if (virtualPosition <= 0) {virtualPosition = 0;}
  290.             feed1minute = virtualPosition;
  291.             lcd.setCursor(5,1);
  292.             printDigits(feed1minute);
  293.            } while ((digitalRead(PinSW)));
  294.          lcd.noBlink();
  295.          delay(1000);  
  296.  
  297.    //   SET THE Feed2 Hour
  298.        
  299.        lcd.blink();   // Turn on the blinking cursor:
  300.        virtualPosition = feed2hour;  //needed or will be zero to start with.
  301.        do  {
  302.        lcd.setCursor(10,1);   // put cursor at feed1hour
  303.        delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  304.            if (TurnDetected)  {       // do this only if rotation was detected
  305.             if (up)
  306.               virtualPosition--;
  307.             else
  308.             virtualPosition++;
  309.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  310.                                }
  311.             // Here I change the feed1 hour
  312.             if (virtualPosition >= 24) {virtualPosition = 0;}
  313.             if (virtualPosition <= 0) {virtualPosition = 0;}
  314.             feed2hour = virtualPosition;
  315.             lcd.setCursor(10,1);
  316.             printDigits(feed2hour);
  317.            } while ((digitalRead(PinSW)));
  318.          lcd.noBlink();
  319.          delay(1000);  
  320.  
  321.       //   SET THE Feed2 Mins
  322.        
  323.        lcd.blink();   // Turn on the blinking cursor:
  324.        virtualPosition = feed2minute;  //needed or will be zero to start with.
  325.        do  {
  326.        lcd.setCursor(13,1);   // put cursor at feed1minute
  327.        delay(500);   // Delay needed or same button press will exit do-while as while is checking for another button push!
  328.            if (TurnDetected)  {       // do this only if rotation was detected
  329.             if (up)
  330.               virtualPosition--;
  331.             else
  332.             virtualPosition++;
  333.             TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
  334.                                }
  335.             // Here I change the feed1 minute
  336.             if (virtualPosition >= 60) {virtualPosition = 0;}
  337.             if (virtualPosition <= 0) {virtualPosition = 0;}
  338.             feed2minute = virtualPosition;
  339.             lcd.setCursor(13,1);
  340.             printDigits(feed2minute);
  341.            } while ((digitalRead(PinSW)));
  342.          lcd.noBlink();
  343.          delay(1000);                        
  344.      
  345.   }  // end of main IF rotary encoder push button checker
  346.  
  347.    // CHECK FOR MANUAL FEED BUTTON
  348.   buttonState = digitalRead(buttonPin);
  349.   if (buttonState == HIGH) {
  350.     feed();
  351.   }
  352.   // CHECK FEEDING TIME AND FEED IF MATCHED
  353.  
  354.   if (tm.Hour == feed1hour && tm.Minute == feed1minute && tm.Second == 0)  {  // if I dont' check seconds are zero
  355.     feed();                                                                   // then it'll feed continuously for 1 minute!
  356.       }
  357.  
  358.   if (tm.Hour ==  feed2hour && tm.Minute == feed2minute && tm.Second == 0)  {
  359.     feed();
  360.       }  
  361. if (tm.Hour ==  feed3hour && tm.Minute == feed3minute && tm.Second == 0)  {  // if I dont' check seconds are zero
  362.     feed();                                                                   // then it'll feed continuously for 1 minute!
  363.       }
  364. }   // End of main Loop
  365.  
  366. void printDigits(int digits){   // utility function for digital clock display: prints leading 0
  367.    if(digits < 10)
  368.     lcd.print('0');
  369.    lcd.print(digits);
  370.  }
  371.  
  372.  void feed() {
  373.    digitalWrite(13,HIGH);
  374.    lcd.setCursor(17,0);
  375.    lcd.print("   Miam Miam!!");
  376.     for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
  377.      // scroll one position left:
  378.      lcd.scrollDisplayLeft();
  379.      // wait a bit:
  380.      delay(150);
  381.          }
  382.  
  383.    // Stir servo section     If you don't need a stir servo simply comment out all fo this until the Auger rotate section  
  384.    stirServo.attach(feedpin);      // I don't know if I need one either but I'm adding it now as it's easiest before I build it!
  385.     for(pos = 0; pos <= 110; pos += 1) {                                  
  386.     stirServo.write(pos);              
  387.     delay(5);   }
  388.     delay(40000);
  389.    for(pos = 110; pos>=0; pos-=1)  {                                
  390.     stirServo.write(pos);          
  391.     delay(5);   }
  392.    
  393.    stirServo.detach();  
  394.    
  395.    // rotate the Auger
  396.    /*
  397.    
  398.    digitalWrite(feedpin2, HIGH);  //test d'activation de pin
  399.    digitalWrite(feedlight, HIGH);
  400.    delay(feedQty *1000);
  401.    digitalWrite(feedpin2, LOW);  //test d'activation de pin
  402.    digitalWrite(feedlight, LOW);
  403.  /*  
  404.    feedServo.attach(feedpin3);
  405.     for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
  406.     // in steps of 1 degree
  407.     {
  408.       feedServo.write(pos);              // tell servo to go to position in variable 'pos'
  409.     delay(15);                       // waits 15ms for the servo to reach the position
  410.   for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
  411.     feedServo.write(pos);              // tell servo to go to position in variable 'pos'
  412.     delay(15);                       // waits 15ms for the servo to reach the position
  413.   }
  414.      feedServo.detach();
  415.      */
  416.      for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
  417.      // scroll one position left:
  418.      lcd.scrollDisplayRight();
  419.      // wait a bit:
  420.      delay(150);
  421.          }
  422.          digitalWrite(13,LOW);
  423.          
  424.              }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement