Using a button to override sensor switch Help

Hi all,

I am currently in the process of fine tuning my garduino, self monitoring watering system. I have the code and kit and is all working well. However i want to include a switch in the code that will enable me turn the pump on manually (ie if i feel the plants need an additional burst of watering i could hit the space bar on my laptop and that would trigger the pump to pump). I dont want this to affect the day to day self operating functionality of the system. Should i be using another if function for the switch and how would this piece of code would fit in the rest of it. My code so far is as follows;

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;

//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
int waterPump = 7;

//define variables to store moisture, light, and temperature values
int moisture_val;



//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
  //open serial port
  Serial.begin(9600);
  //set the water, light, and temperature pins as outputs that are turned off
  pinMode (waterPump, OUTPUT);
  digitalWrite (waterPump, LOW);
}


void loop() {
  // read the value from the moisture-sensing probes, print it to screen, and wait a second
  moisture_val = analogRead(moistureSensor);
  Serial.print("moisture sensor reads ");
  Serial.println( moisture_val );
  delay(1000);


  //turn water on when soil is dry, and delay until soil is wet
  if (moisture_val < 850)
  {
    digitalWrite(waterPump, HIGH);
  }


  {
    delay(10000);
  }

  digitalWrite(waterPump, LOW);
}

Thanks for the help

Yes, you can do it like the 'button' example in your IDE. Make sure the delays don't mess up the timing system, delay will freeze the entire program. If it does you can look at the 'blink without delay' digital example in your IDE.

i could hit the space bar on my laptop and that would trigger the pump to pump

How far is your garduino from the laptop? You might need to include a long cable, wireless or ethernet connection to make it work.

And if you hit the space-bar (or another switch) for how long the pump should work?

And if your cat was dancing on the spacebar how would you prevent drowning your plants?

Just thinking in scenarios... Rob

lol! the guardinho is very close to my laptop, ideally the space bar switch would pump the water for the same amount of time as the current set up ie 10 seconds and lastly i dont have a cat!!!!

Its actually for demonstration purposes as this is part of a university project. Once the presentation is done i expect i will just go back to using the self monitoring system.

Any advice? Thanks

As it is a demonstration you could use the serial connection between the laptop and the garduino.

Try to understand this - http://www.arduino.cc/en/Serial/Read - tutorial, and then merge it into your code.

Note: you must press the send button in the serial monitor to actually send the typed in chars.

Again thanks for the help. I have read the tutorial but am not sure how it would work.

Would i have to write a piece of code that states that if a certain character on the keyboard (lets say g) was sent via the serial monitor and read using the “read” function then the gardinho should turn the pump on. would it matter where this new code is placed within the existing?

I have read the tutorial but am not sure how it would work.

Then you should run it on the Arduino and experiment with it. Reading is the first step of learning, exercising the second.

Would i have to write a piece of code that states that if a certain character on the keyboard (lets say g) was sent via the serial monitor and read using the "read" function then the gardinho should turn the pump on.

Yes! see you can do it !

For now it doesn't matter which character is pressed but if you choose one use the w for water :) You could extend it later with pressing the L for Light and M for Manure etc

would it matter where this new code is placed within the existing?

Yes, but you try first.

Note: don't forget to stop the pump ..

Right, i am trying to walk before i can run here and am trying to write a piece of code that will result in an led being turned on using the keyboard as a switch. I then plan to beef this up and use the same code to turn on my water pump. However i am having a few issues with the read.serial function. The code so far is

void setup() {
      Serial.begin(9600);
        pinMode(13, OUTPUT);
}

void loop()  {
  
if (Serial.read = w)
{
  digitalWrite(13, HIGH);     
  delay(1000);                
  digitalWrite(13, LOW);  
}
}

I dont know how to define the w key on my key board as the input to turn the led on. Any help would be appreciated

if (Serial.read = w)
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
}

There are several problems with this code. Serial.read() is a function, so the () are necessary. The = operator is an assignment operator. The == is the equality operator. While we think of them as the same thing, they are very different. The assignment operator should be thought of as "Let this variable equal this value." The equality operator should be thought of as "Is this variable equal this value?"

Finally, the w should be in quotes.

So, you should have:

if (Serial.read() == 'w')
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
}

Missed this part:

I dont know how to define the w key on my key board as the input to turn the led on.

You need some application running on the PC that will register key events, and send the key to the serial port (or some other value that is a function of the key pressed).

Processing is a good choice for this.

The code works!! and my LED is now lights up when the W key is pressed. Amazing. I am now going to insert it into the rest of my garduino code. I will insert it in the same void.loop as the code for the moisture sensor and also the water pump. I dont have the rest of the kit with me (its at uni) so do you pro’s think this will work, the pump turns on when the plants are too dry and then if the w key is pressed and sent via the serial monitor it will get an additional burst from the pump.

Thanks so much for the help

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;

//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
int waterPump = 7;

//define variables to store moisture, light, and temperature values
int moisture_val;



//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
  //open serial port
  Serial.begin(9600);
  //set the water, light, and temperature pins as outputs that are turned off
  pinMode (waterPump, OUTPUT);
  digitalWrite (waterPump, LOW);
}


void loop() {
  // read the value from the moisture-sensing probes, print it to screen, and wait a second
  moisture_val = analogRead(moistureSensor);
  Serial.print("moisture sensor reads ");
  Serial.println( moisture_val );
  delay(1000);


  //turn water on when soil is dry, and delay until soil is wet
  if (moisture_val < 850)
  {
    digitalWrite(waterPump, HIGH);
  }


  {
    delay(10000);
  }

  digitalWrite(waterPump, LOW);



  if (Serial.read() == 'w')
  {
    digitalWrite(waterPump, HIGH);
    delay(10000);
    digitalWrite(waterPump, LOW);
  } 
}
  {
    delay(10000);
  }

The curly braces are unnecessary. Other than that, the code looks good. One improvement would be to test whether there is serial data available to read, before reading. But, if there is no serial data to read, Serial.read() returns a -1, which is clearly not a 'w', so it isn't essential to check availability first. It's just a good habit to get into.

I would add something like

  if (Serial.read() == 'w' && moisture_val < 850) 
  {
    digitalWrite(waterPump, HIGH);
    delay(10000);
    digitalWrite(waterPump, LOW);
  }

to prevent drowning the plant.

Thanks for the all the help guys. Looking forward to getting it all set up now and watering the hell out of all my plants (only if they need it though of course)