Go Down

Topic: one large step for me..... and my cat (Read 77 times) previous topic - next topic

LandonW

im not the best at coding and recently got the concept of creating a function to be call.... some may find this to be small fries, but to me its large taters.  I didn't get any error codes nor have I built the circuit yet to run the program (im much stronger in hardware than software)
if anybody has any suggestions for the following code either improvement or additions please let me know.
like i said...most might call this small fries.    dont judge lol

Code: [Select]

/* Code to opperate pump in a cats watering bowl (because the spoiled @$$ wont drink water that doesnt move)
 *  this is meant for a vertical style water bowl. This is also meant to turn the water resivour into a night light
 *  when the light level is low. Lastly there is a low battery function that shows how many times the cat has been at
 *  her bowl when the battery is getting low. this will signify to charge the battery and to see how many pump cycles
 *  a single charge can handle
 */

const byte IR = 2;  // Infrared sensor
const byte Pump = 5;  // pin to base of transistor to turn on pump
const byte LED_W = 6; // white led
const byte LED_R = 9; // red led
const byte batt = A0; // read battery voltage through 1k resistor
const byte lightSensePin = A2;  // read photo resistor
int lightValue; //
int Pval;   // assign pwm value for pump transistor
int battVal;  //
int count;  // keep track of IR activation

// run once to set initials values
void setup()
{
  pinMode(IR, INPUT);
  pinMode(lightSensePin, INPUT);
  pinMode(Pump, OUTPUT);
  pinMode(LED_W, OUTPUT);
  pinMode(LED_R, OUTPUT);
  Pval = 122; // set pwm at 50% duty cycle
  count = 0 ; //
}

// run continuously
void loop()
{
 int lightValue = analogRead(lightSensePin); // read photo resistor and assign value
 int battVal = analogRead(batt); // read battery voltage and assign value

// night light function to pulse a white LED
  if (lightValue > 660) {
    for (int F = 70; F <= 255; F++) { // dim LED up and down
      analogWrite(LED_W, F);
      delay(750); // hold at max brightness for 3/4 seconds
    }
  }

// opperate pump if its day time and cat is close to water bowl
  else {
    if ((IR == HIGH) && (lightValue < 660)) {
      count++; // count activations of pump
      analogWrite(Pump, Pval);
      delay(10000); // keep pump on for 10 seconds
      digitalWrite(Pump, LOW); // shut pump off
    }

// when battery is low then flash a red LED for the amount of times that the pump was activated
    if (battVal < 660) {
      for (int i = 0; i <= count; i++) { // opperate pulse per count value
        countDisplay(); // pulse LED
      }
      delay(2000);
    }
  }
}

// pulse LED when called
void countDisplay() {
  digitalWrite(LED_R, HIGH);
  delay(300);
  digitalWrite(LED_R, LOW);
  delay(300);
}




DKWatson

#1
Today at 04:21 am Last Edit: Today at 04:22 am by DKWatson
if ((IR == HIGH) && (lightValue < 660))

IR = 2 as you declared it a constant. Perhaps what you meant was if (digitalRead(IR) == HIGH......

give my regards to Puss.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

6v6gt

Maybe:

1. Show how you intend to measure the battery voltage. Usual is to use the internal microprocessor voltage reference.

2. Replace the delay() statements so things can happen (pseudo) simultaneously.

Go Up