Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Switch pressed -> reading HIGH
- //Switch not pressed -> reading LOW
- //Motor direction 0 is down, 1 is up
- const int uplimit =32;
- const int downlimit =34;
- const int motorspeed=9;
- const int motordirection=28;
- //const int pumpspeed=5;
- //const int pumpdirection=6;
- const int motorenable=26;
- //const int pumpenable=;
- int Finishedcleaning=0;
- int Reachedup=0;
- int Motorinitialsetup=0;
- int Motorsecondsetup=0;
- int downfiltered=0;
- int upfiltered=0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(motorspeed, OUTPUT);
- pinMode(motordirection, OUTPUT);
- pinMode(motorenable, OUTPUT);
- //pinMode(pumpspeed, OUTPUT);
- //pinMode(pumpenable, OUTPUT);
- pinMode(uplimit, INPUT);
- pinMode(downlimit, INPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- int average1=0;
- int average2=0;
- if (Motorinitialsetup==0 ) // to be run before hitting initial switch
- {digitalWrite(motorenable,HIGH);
- analogWrite(motorspeed,190);
- digitalWrite(motordirection,0);// Bring motor down
- // digitalWrite(pumpenable,HIGH);
- // digitalWrite(pumpdirection,1);
- // analogWrite(pumpspeed,240);
- Motorinitialsetup=1;//so this if statement does not run again
- }
- for (int i=1;i<100;i++)
- {downfiltered=digitalRead(downlimit);
- average1=average1+downfiltered;
- }
- average1=average1/100;
- if ((average1>0.9) && Finishedcleaning==0 ) // Function after hitting down switch
- {
- delay(1000); // wait until motor cross switch
- analogWrite(motorspeed,0);// stop motor
- Finishedcleaning=1; //indicate cleaning is over for next loop
- delay(500); // wait until motor cross switch
- }
- if ( (Finishedcleaning==1) && (Reachedup==0) && (Motorsecondsetup==0))
- {
- digitalWrite(motordirection,1);// reverse motor direction
- analogWrite(motorspeed,190);// start motor again
- Motorsecondsetup=1;
- }
- if( Motorsecondsetup==1) // only checks switch up switch after motors comes back
- {
- for (int i=1;i<100;i++)
- {upfiltered=digitalRead(downlimit);
- average2=average2+downfiltered;
- } }
- average2=average2/100;
- if ((average2>0.9) && (Finishedcleaning==1)) // Stop Motor after returning
- { analogWrite(motorspeed,0);
- delay(1000);
- Reachedup=1;// indicate finished returning after cleaning cycle
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement