Would like to fill a bucket of water at the push of a button

Okay. I'd like a valve to upon over a relay to fill a bucket with water at the push of a button. The valve should close when the bucket is full and the system should then wait for another button push.
Button, ultra sonic sensor (sensing full bucket), relay and valve are all within my grasps and hense control.

What I need is to figure out the code that makes it all happen.

#include <Button.h>        
#define BUTTON_PIN 2      // forbindes til gnd
#define PULLUP true        
#define INVERT true        
#define DEBOUNCE_MS 20     
#define echoPin 7 // Echo Pin //ny
#define trigPin 8 // Trigger Pin //ny
#define RelayPin 9 // Relæ Pin //ny         

Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);    
boolean ledState;
int maximumRange = 10; // afstand i cm til vandspejl   
int minimumRange = 0;      
long duration, distance;           

void setup(void)
{
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);    
    pinMode(echoPin, INPUT);     
    pinMode(RelayPin, OUTPUT); 
    pinMode(13, OUTPUT);       
    digitalWrite(13,LOW); 
}

void loop(void)
{
    myBtn.read();                   

    if (myBtn.wasPressed()) {       
        digitalWrite(trigPin, LOW); 
        delayMicroseconds(2); 
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10); 
 
        digitalWrite(trigPin, LOW);
        duration = pulseIn(echoPin, HIGH);
        distance = duration/58.2;
 
        if (distance >= maximumRange || distance <= minimumRange){
            digitalWrite(RelayPin, LOW); 
             }
        else {
             digitalWrite(RelayPin, HIGH); 
 }
 delay(30);
    }
}

is what I've got.
I've tested button push in a seperate code and it's working.
I've also tested ultra sonic and relay in another seperate code and they're also working.
The combination I still need to figure out.
Any suggestions? I'd like the system to reset when the bucket is full and then wait to be re-activated with another bucket and button push.

Any suggestions?

Start with the state change detection example. When the switch becomes pressed, instead of incrementing a counter, call a function, fillBucket().

Initially, fillBucket() should just turn an LED on for 10 seconds, and then turn it off.

When that works, replace the LED with the relay, and turn the relay on for 10 seconds, and then turn it off.

When that works, replace the code to turn the relay off after 10 seconds with code to turn the relay off when the ping sensor says the bucket is full.

Incremental development is far more rewarding (Hey, that worked!) than is trying to write all the code at once.

Sounds great, thanks. But it's not exactly working out for me. I'm not used to stating and calling functions.

#include <Button.h>        
#define BUTTON_PIN 2      // forbindes til gnd
#define PULLUP true        
#define INVERT true        
#define DEBOUNCE_MS 20     
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define RelayPin 9 // Relæ Pin        

Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);    
boolean ledState;
int maximumRange = 10; // afstand i cm til vandspejl   
int minimumRange = 0;      
long duration, distance;           

void setup(void)
{
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);    
    pinMode(echoPin, INPUT);     
    pinMode(RelayPin, OUTPUT); 
    pinMode(13, OUTPUT);       
    digitalWrite(13,LOW); 
}

void loop(void)
{
    myBtn.read();                   
    if (myBtn.wasPressed()) {       
       void FyldKande();
}



void FyldKande() 
 {
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 digitalWrite(RelayPin, LOW); 
 }
 else {
 digitalWrite(RelayPin, HIGH); 
}
return 
}

This is a function prototype:

       void FyldKande();

This is a function call:

       FyldKande();

cveng:
Sounds great, thanks. But it's not exactly working out for me.

As there was only about 20 minutes between @PaulS posting Reply #1 and your response in Reply #2 I am not surprised. How about spending another 3 or 4 hours (or even days) trying to figure it out.

In 20 minutes you could not possibly have worked through the series of exercises suggested in Reply #1 - never mind doing some additional research when you got stuck.

Have a look at how functions are used in Planning and Implementing a Program

...R

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define RelayPin 9 // Relæ Pin

What extra information do those comments provide?

If I read your code correctly it looks like you need to wait for the bucket to fill before returning from the function FyldKande().

void loop(void)
{
  myBtn.read();
  if (myBtn.wasPressed()) {
    FyldKande();
  }

}



void FyldKande()
{
  boolean bucketFull = false;

  while ( !bucketFull )
  {

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);

    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration / 58.2;

    if (distance >= maximumRange || distance <= minimumRange) {
      digitalWrite(RelayPin, LOW);
    }
    else {
      digitalWrite(RelayPin, HIGH);
      bucketFull = true;
    }

  }
}

Well I've now put my code into a few functions and the logic seems okay (most likely isn't though).
What happens is as follows.

  • I upload the code to the board and relay opens instantly - can't prevent it.
  • The press of the button runs a beepK followed by a beepL
  • Ultra sonic seems to have no effect
    Seperatly it all works fine
#include <Button.h>        
#define BUTTON_PIN 2      // forbindes til gnd
#define TrigPin 7 // trigger pin ultralyd
#define EchoPin 8 // echo pin ultralyd
const int BeepPin = 9; // beeper pin
#define RelayPin 10 // relæ pin
#define PULLUP true        
#define INVERT true        
#define DEBOUNCE_MS 20     
 
int maxRange = 10; // afstand i cm til vandspejl
int minRange = 0; 
long duration, distance;      

void beepK() {
  digitalWrite(BeepPin,1);  
  delay(100);
  digitalWrite(BeepPin,0); 
}

void beepL() {
  digitalWrite(BeepPin,1);  
  delay(1000);
  digitalWrite(BeepPin,0); 
}

void fyldKande() {
 digitalWrite(TrigPin, LOW); 
 delay(2); 
 digitalWrite(TrigPin, HIGH);
 delay(10); 
 
 digitalWrite(TrigPin, LOW);
 duration = pulseIn(EchoPin, HIGH);
 distance = duration/58.2;
 
 if (distance >= maxRange || distance <= minRange){
 digitalWrite(RelayPin, LOW); 
 }
 else {
 digitalWrite(RelayPin, HIGH); 
 }
 delay(40);
}

Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);      
  

void setup(void)
{
    pinMode(BeepPin,OUTPUT); 
    Serial.begin (9600);
    pinMode(TrigPin, OUTPUT);
    pinMode(EchoPin, INPUT);
    pinMode(RelayPin, OUTPUT);
    pinMode(13, OUTPUT); // sluk onboard LED 
    digitalWrite(13,LOW); // sluk onboard LED
//    digitalWrite(RelayPin, LOW); 
}

void loop(void)
{
    myBtn.read();                   
    if (myBtn.wasPressed()) {       
        beepK();
        fyldKande();
        beepL();
    }
}
  • Ultra sonic seems to have no effect

What do your Serial.print() statements tell you is happening?

Why not?

PaulS:
What do your Serial.print() statements tell you is happening?

Why not?

What and why do you think?

cveng:
What and why do you think?

The first question was what do your serial prints tell you. The answer to that is "I don't have any". The second question was why not. If you want us to debug your code for you, but a whole bunch of Arduinos and ping sensors and switches, and get them wired up. We'll all send you snail-mail addresses, where you can send each of us an Arduino with attached sensors. We'll debug your code for you, and then send the Arduinos back.

If that is too much trouble, or expense, then YOU need to debug your code.

PaulS:
The first question was what do your serial prints tell you. The answer to that is "I don't have any". The second question was why not. If you want us to debug your code for you, but a whole bunch of Arduinos and ping sensors and switches, and get them wired up. We'll all send you snail-mail addresses, where you can send each of us an Arduino with attached sensors. We'll debug your code for you, and then send the Arduinos back.

If that is too much trouble, or expense, then YOU need to debug your code.

You read code and you know I didn't serialprint. Want it, ask for it instead of playing stupid! I'm new at this and if you don't want to help in a kind and constructive manner, the Internet is full of kind people who will. Wanna patronise, find another thread!

I don't see anything "stupid" or "patronizing" in Reply #9

The first was a simple question that you did not reply to. I can see how @PaulS's second question could be confusing if you don't answer the first question.

...R

Why did you start another thread for this ?

UKHeliBob:
Why did you start another thread for this ?

There is no other thread 8)

@OP - DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.