Pages: [1]   Go Down
Author Topic: My gardunio edit and my issues with the delay function  (Read 349 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I decided I'd remake the gardunio because I saw some issues in how it was set up but liked the concept. But I'm having some issues working the timing of the watering and lighting. I know the problem is the delay function but I'm not sure how to work an alternative.  Rather than just rewriting my code if you can please explain how to fix it instead. Examples are great.

Code:
int photocellPin = 0; // the cell and pull down are connected to a0 to check for light levels
int WaterSensor = 1; // Water level sensor
int photocellReading; // the analog reading from the analog resistor divider
int lightarray1 = 13; // lights for the plants
int waterpump = 7; // Its the pump for the water
int water_val; // stores the value for water levels
void setup(void) {
// send debugging information via the Serial monitor
Serial.begin(9600);
  pinMode(lightarray1, OUTPUT);
  pinMode(waterpump, OUTPUT);
  digitalWrite (waterpump, LOW);
}
void loop(void) {
water_val-analogRead(WaterSensor);
Serial.print("Water levels at ");
Serial.println( water_val );
delay(1000);
//turn water on when soil appears to be dry stop when the plant is watered
if (water_val < 850)
{
  digitalWrite(waterpump, HIGH);
}
while (water_val < 850)
{
  delay(10000);
}

digitalWrite(waterpump, LOW);
//reads the light levels
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (photocellReading < 30) {
digitalWrite(lightarray1, HIGH);
}
else {
digitalWrite(lightarray1, LOW);
}
delay(1000);
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some empty lines and right indentation can do miracles smiley

If you have the code in the IDE you should press CTRL-T to re-indent the code to make it more readable.

rewritten the code without delays so it is far more responsive without flooding the serial output - find the differences smiley-wink - and give it a try
Code:
//
//    FILE: garduino.pde
//  AUTHOR:
//    DATE: 2012-NOV-01
//
// PUPROSE: watering plants
//

#define WATERTHRESHOLD  850
#define LIGHTTHRESHOLD 30
#define OUTPUTINTERVAL 1000

int photocellPin  = A0;     // the cell and pull down are connected to a0 to check for light levels
int WaterSensor = A1;    // Water level sensor

int photocellReading =0;    // the analog reading from the analog resistor divider

int lightarray1 = 13;        // lights for the plants
int waterpump = 7;        // Its the pump for the water
int water_val = 0;               // stores the value for water levels

unsigned long lastOutput = 0;

void setup(void)
{
  Serial.begin(9600);
  Serial.println("Start garduino...");

  pinMode(lightarray1, OUTPUT);
  pinMode(waterpump, OUTPUT);
  digitalWrite (waterpump, LOW);
}

void loop(void)
{
  //turn water on when soil appears to be dry stop when the plant is watered
  water_val = analogRead(WaterSensor);
  if (water_val < WATERTHRESHOLD  )
  {
    digitalWrite(waterpump, HIGH);
  }
  else
  {
    digitalWrite(waterpump, LOW);
  }

  //turn light on when it appears to be dark
  photocellReading = analogRead(photocellPin);
  if (photocellReading < LIGHTTHRESHOLD )
  {
    digitalWrite(lightarray1, HIGH);
  }
  else
  {
    digitalWrite(lightarray1, LOW);
  }

  // DO SERIAL OUTPUT ?
  if (millis() - lastOutput >= OUTPUTINTERVAL)
  {
    lastOutput = millis();
    Serial.print("Water levels at: ");
    Serial.println( water_val );
    Serial.print("Light at : ");
    Serial.print( photocellReading ); // the raw analog reading
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: