SHARE
TWEET

Untitled

a guest Feb 11th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Switch pressed ->  reading HIGH
  2. //Switch not pressed -> reading LOW
  3. //Motor direction 0 is down, 1 is up
  4.  
  5. const int uplimit = 32;
  6. const int downlimit = 34;
  7. const int motorspeed = 9;
  8. const int motordirection = 28;
  9. //const int pumpspeed=5;
  10. //const int pumpdirection=6;
  11. const int motorenable = 26;
  12. //const int pumpenable=;
  13. int Finishedcleaning = 0;
  14. int Reachedup = 0;
  15. int Motorinitialsetup = 0;
  16. int Motorsecondsetup = 0;
  17. int downfiltered = 0;
  18. int upfiltered = 0;
  19. void setup() {
  20.   // put your setup code here, to run once:
  21.  
  22.   pinMode(motorspeed, OUTPUT);
  23.   pinMode(motordirection, OUTPUT);
  24.   pinMode(motorenable, OUTPUT);
  25.   //pinMode(pumpspeed, OUTPUT);
  26.   //pinMode(pumpenable, OUTPUT);
  27.   pinMode(uplimit, INPUT);
  28.   pinMode(downlimit, INPUT);
  29.  
  30. }
  31.  
  32. void loop() {
  33.   // put your main code here, to run repeatedly:
  34.   int average1 = 0;
  35.   int average2 = 0;
  36.   if (Motorinitialsetup == 0 ) // to be run before hitting initial switch
  37.   { digitalWrite(motorenable, HIGH);
  38.     analogWrite(motorspeed, 190);
  39.     digitalWrite(motordirection, 0); // Bring motor down
  40.     //   digitalWrite(pumpenable,HIGH);
  41.     //  digitalWrite(pumpdirection,1);
  42.     // analogWrite(pumpspeed,240);
  43.     Motorinitialsetup = 1; //so this if statement does not run again
  44.  
  45.   }
  46.  
  47.   for (int i = 1; i < 100; i++)
  48.   { downfiltered = digitalRead(downlimit);
  49.     average1 = average1 + downfiltered;
  50.   }
  51.   average1 = average1 / 100;
  52.  
  53.   if ((average1 > 0.9) && Finishedcleaning == 0 ) // Function after hitting down switch
  54.   {
  55.     delay(1000); // wait until motor cross switch
  56.     analogWrite(motorspeed, 0); // stop motor
  57.     Finishedcleaning = 1; //indicate cleaning is over for next loop
  58.     delay(500); // wait until motor cross switch
  59.   }
  60.  
  61.   if ( (Finishedcleaning == 1) &&   (Reachedup == 0)  && (Motorsecondsetup == 0))
  62.   {
  63.     digitalWrite(motordirection, 1); // reverse motor direction
  64.     analogWrite(motorspeed, 190); // start motor again
  65.     Motorsecondsetup = 1;
  66.   }
  67.  
  68.  
  69.   if ( Motorsecondsetup == 1) // only checks switch up switch after motors comes back
  70.   {
  71.  
  72.     for (int i = 1; i < 300; i++)
  73.     { upfiltered = digitalRead(downlimit);
  74.       average2 = average2 + downfiltered;
  75.  
  76.     }
  77.     average2 = average2 / 300;
  78.     if ((average2 > 0.9) && (Finishedcleaning == 1)) // Stop Motor after returning
  79.     { analogWrite(motorspeed, 0);
  80.       delay(1000);
  81.       Reachedup = 1; // indicate finished returning after cleaning cycle
  82.     }
  83.   }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top