Go Down

Topic: Litter Box  (Read 485 times) previous topic - next topic

hugh59

Hey All,

I'm trying to recycle our old littermaid litter box, the original circuit board finally crapped out after many years and many repairs. the rest of the litter box and components are fine(ir sensor and receiver 2 limit switches and motor )


Here's how it should work:

- Cat walks in his litter, IR goes LOW while cat is in box then waits till cat leaves say 10 minutes

- IR goes HI and rake cycle starts, rake heads to end stop and returns(on timer)
- IF it doesn't complete its trip in say 10 seconds to end stop it returns to home and tries again up to 5 
   times then quits or maybe go back to home if it can.


I found some code(included) for the same box but doesn't work, it's missing some parentheses I think, 
on the 2 lines where the timing occurs.
I've tried for 3 days read everything but my old brain doesn't want to hold it all!

I'm using an arduino and a generic L298n circuit board.

The code does work but all it does is kick the motor back and forth 5 times, its like its not getting the info to at least run the 10 seconds before giving up.
Note: the timing for the motor is to keep it from running continuously if it jams up.

So if you could help recycle this i'd appreciate the help.
Thanks, Hugh

Code: [Select]



// set up sensor & switch pin locations
int sensorPin = A0;   // select the input pin for the cat presence sensor
int endPin = 3;       // select the input pin for the endstop
int homePin = 4;      // select the input pin for the home endstop

// set up motor pin locations, set by the motor shield used
int pinI1=8;               //define I1 interface
int pinI2=11;              //define I2 interface
int speedpinA=9;           //enable motor A
int spead =255;            //define the spead of motor

// set up various delays
int sensorloopdelay=500; //delay in ms between loop repetitions when waiting for cat
unsigned long delayaftercat=600000; //delay in ms before cycling catbox after cat is detected, 10 minutes


// set up default sensor values
int sensorValue = 100;  // variable to store the value coming from the sensor. starting at 100 so no cat present
int endValue=1; // endstop status
int homeValue=1; // home stop status
unsigned long MaxTravelTime=30000; //maximum time for rake to travel each direction, assumes jammed if didn't finish in alloted time

void setup() {
  // initialize the various inputs and outputs
  pinMode(endPin, INPUT);
  pinMode(homePin, INPUT);
  pinMode(sensorPin, INPUT);
  pinMode(pinI1, OUTPUT);
  pinMode(pinI2, OUTPUT);
  pinMode(speedpinA, OUTPUT);
 
  //set up serial communications with computer
  Serial.begin(9600);
}



void loop() {
// run catbox cycle
int success=0; // cycle success (1) or failure (0)
int i=0; // loop counter

       Serial.println("cat box cycle start");
       
   while (i < 5)
   { // run cycle maximum of 5 times, stop when cycle is successful
     
     success = cycle();
     Serial.println(success);
           
     if (success != 0) // break out of 5 rep loop if cycle did not fail
     {
       Serial.println("successfull cycle");
       i=5; // break out of loop
     }
     else
     {
       Serial.println("unsuccessful cycle");
       i=i+1; //increment i
     }

   }


// reset sensor value
sensorValue = 100;

// wait until cat is detected
  Serial.println("waiting for cat");


while (sensorValue > 10)
{
   // read the value from the sensor
   // if > 10 no cat
   // if < 10 cat is present - loop until cat is present
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue); //print sensor value for troubleshooting
   
  delay(sensorloopdelay);
}

  Serial.println("cat detected");
 
// wait until cat leaves
while (sensorValue < 10)
{
   // read the value from the sensor
   // if > 10 no cat
   // if < 10 cat is present - loop until cat is present
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue); //print sensor value for troubleshooting
 
  delay(sensorloopdelay);
}

  Serial.println("cat left, waiting for specified period of time");
 
// wait for specified period of time before running catbox cycle

delay(delayaftercat);
                 
}

int cycle() //returns successful cycle or failure
{
  unsigned long startMove_time=0;
  unsigned long time_elapsed=0;
  int success=0;
 
  Serial.println("start motor out");
  Serial.println("wait until endstop");
  // wait until endstop is reached
 
  startMove_time = millis(); // find time immediately before move start
 
      backward(); // start motor towards endstop
    Serial.println("moving towards endstop");
 
  while (endValue != 0 & time_elapsed
  {
    time_elapsed= millis()-startMove_time; // calculate time elapsed
    endValue=digitalRead(endPin);

  }
 
  stop_motor();
 
  if (endValue==0)
  {
    Serial.println("endstop reached");
    success=1;
  }
 
  if (time_elapsed >= MaxTravelTime)
  {
    Serial.println("outbound travel timed out");
    success=0;
  }
 
  endValue=1; //reset endstop value

 
  Serial.println("stop motor");
  Serial.println("reverse motor");
  Serial.println("wait until home stop");
 
    startMove_time = millis(); // find time immediately before move start
    time_elapsed=0; //reset time elapsed
   
  forward(); // start motor back towards home
          Serial.println("moving towards home");
 
    while (homeValue != 0 & time_elapsed
  {
        time_elapsed= millis()-startMove_time; // calculate time elapsed
        homeValue=digitalRead(homePin);

  }
 
  stop_motor(); // stop motor

 
    if (homeValue==0)
  {
    Serial.println("home stop reached");
    success=1;
  }
 
  if (time_elapsed >= MaxTravelTime)
  {
    Serial.println("inbound travel timed out");
    success=0;
  }
 
  homeValue=1; //reset home stop value
 
 
  Serial.println("stop motor");
 
  return success; //return value back to main loop
 
 
}

void forward()
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);
}

void backward()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);
}

void stop_motor()//
{
     digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
     delay(1000);
}




tbillion

its been a few days you ever figure this out or you still need help?

Go Up