manhoosbilli1

Motor code switch case problem

Jul 7th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.12 KB | None | 0 0
  1. #include <Bounce2.h>
  2. #include <Wire.h>
  3. #include "RTClib.h"
  4. #include <TimeLib.h>
  5. #include <TimeAlarms.h>
  6.  
  7. AlarmId id;
  8. RTC_DS3231 rtc;
  9.  
  10. #define motorRightPin 4
  11. #define motorLeftPin 3
  12. #define limitSwitchLeftPin 9
  13. #define limitSwitchRightPin  8
  14. int hour_now;
  15. int minute_now;
  16. int second_now;
  17. int month_now;
  18. int year_now;
  19. int day_now;
  20.  
  21.  
  22.  
  23. bool startMotor = false;
  24. Bounce limitSwitchLeft = Bounce();
  25. Bounce limitSwitchRight = Bounce();
  26.  
  27.  
  28. void setup() {
  29.   // syncing the time of arduino with time of RTC
  30.  
  31.   DateTime now = rtc.now();
  32.   hour_now = now.hour();
  33.   minute_now = now.minute();
  34.   second_now = now.second();
  35.   month_now = now.month();
  36.   year_now = now.year();
  37.   day_now = day.now();
  38.  
  39.   setTime(hour_now,minute_now,second_now,month_now,day_now,year_now);  
  40.  
  41.   pinMode(motorRightPin, OUTPUT);
  42.   pinMode(motorLeftPin, OUTPUT);
  43.   digitalWrite(motorRightPin, LOW);
  44.   digitalWrite(motorLeftPin, LOW);
  45.  
  46.   limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  47.   limitSwitchLeft.interval(50);
  48.  
  49.   limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  50.   limitSwitchRight.interval(50);
  51.  
  52. //using alarm library which lets me set alarm and execute a specific function at specifc time
  53.  
  54.   Alarm.alarmRepeat(8,30,0, turnEgg);
  55.   Alarm.alarmRepeat(8,31,0, turnEgg);
  56.   Alarm.alarmRepeat(4,30,0, turnEgg);
  57.  
  58.  
  59.   Serial.begin(9600);
  60.   Serial.println("Incubator starting. motor is now at rest");
  61.  
  62. //RTC FUCNTIONS BELOW. necessary
  63.  
  64.   if(! rtc.begin())
  65.   {
  66.     Serial.println("Couldn't find RTC");
  67.     while(1);
  68.   }
  69.  
  70.   if(rtc.lostPower())
  71.   {
  72.     Serial.println("RTC lost power");
  73.     // following line sets the RTC to the date & time this sketch was compiled
  74.     //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  75.     // This line sets the RTC with an explicit date & time, for example to set
  76.     // January 21, 2014 at 3am you would call:
  77.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  78.   }
  79.  
  80.  
  81. //RTC FUNCTIONS ABOVE
  82.  
  83.   //if motor isn't on one of the switches, move the tray to one of the switches
  84.   while(digitalRead(limitSwitchLeftPin) == HIGH && digitalRead(limitSwitchRightPin) == HIGH )
  85.   {
  86.     digitalWrite(motorRightPin, LOW);
  87.     digitalWrite(motorLeftPin, HIGH);
  88.     Serial.println("Mtr calibrating");
  89.   }
  90.  
  91.   digitalWrite(motorRightPin, LOW);
  92.   digitalWrite(motorLeftPin, LOW);
  93.   Serial.println("Calibration OK");
  94.  
  95.   delay(2000);
  96. }
  97.  
  98. enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };
  99.  
  100. int currentState = STATE_IDLE;          //giving names to cases in switch case
  101.  
  102.  
  103. void loop() {
  104.   // initializing time and updating
  105.   DateTime now = rtc.now();
  106.   hour_now = now.hour();
  107.   minute_now = now.minute();
  108.   second_now = now.second();          
  109.   month_now = now.month();
  110.   year_now = now.year();
  111.   day_now = day.now();
  112.   limitSwitchLeft.update();
  113.   limitSwitchRight.update();      //reading the debounce state of switches
  114.   bool limitSwitchLeftState = limitSwitchLeft.read();   //switch are input pullup. high state is zero
  115.   bool limitSwitchRightState = limitSwitchRight.read();
  116.    
  117.   turnEgg();       //turn the eggs
  118.   Alarm.delay(50);        //works as a normal delay to slow down arduino
  119. }
  120.  
  121. turn(){
  122.   if (startMotor == true){  
  123.    switch (currentState) {
  124.     case STATE_IDLE:
  125.         Serial.println("button to trigger start motor has been presssed");
  126.         if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW){
  127.           Serial.println("Moving right now");
  128.           digitalWrite(motorRightPin, HIGH);
  129.           digitalWrite(motorLeftPin, LOW);
  130.           currentState = STATE_MOVING_RIGHT;
  131.         }
  132.         else if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH){
  133.           Serial.println("Moving left now");
  134.           digitalWrite(motorRightPin, LOW);
  135.           digitalWrite(motorLeftPin, HIGH);
  136.           currentState = STATE_MOVING_LEFT;
  137.         }
  138.       break;
  139.  
  140.     case STATE_MOVING_RIGHT:
  141.       // moving right so only check right limit switch
  142.       if (limitSwitchRightState == HIGH && limitSwitchLeftState == LOW)  {
  143.         Serial.println("Motor reached right. motor stopping");
  144.         digitalWrite(motorRightPin, LOW);
  145.         digitalWrite(motorLeftPin, LOW);
  146.         startMotor = false;
  147.         //im not telling it to change state to currentstate_idle once the motor stops because the arduino is so fast that
  148.         //it will read the switches status and keep the motor running in a loop and just reversing it...
  149.         //but since i can now specify at which second i want this to execute i think i should be fine adding
  150.         //the currentSTate_idle statement.. but i haven't tried and giving you original code
  151.        }
  152.       break;
  153.  
  154.     case STATE_MOVING_LEFT:
  155.       // moving left so only check left limit switch
  156.  
  157.       if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW){
  158.         Serial.println("Motor has reached left. Motor stopping");
  159.         digitalWrite(motorRightPin, LOW);
  160.         digitalWrite(motorLeftPin, LOW);
  161.         startMotor = false;
  162.        
  163.      
  164.       }
  165.       break;
  166.   }
  167.  }
  168. }
  169.  
  170. turnEgg() {
  171.   startMotor = true;
  172.   turn();
  173.   startMotor = false;
  174. }
Add Comment
Please, Sign In to add comment