Merge servo and relay sketches

Hey everybody.

I’m trying to merge these two sketches. One is the IDE example push button sketch and the other i have sourced from a website (at top of sketch). Ive tried using various different statements such as break, while, do…while to no avail. They both work individually. Is the sketch i currently have here correct.

Previously the pump relay code would only detect change or be implemented when the servo was operating. Is this solved by the way I have placed the void pumprelay function?

thanks for the help. sorry for the noobish post :slight_smile:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

//long FEEDINGDELAY = 43200000; // 12 hours
//  long FEEDINGDELAY = 57600000; // 16 hours
long FEEDINGDELAY = 300;

long endtime; 
long now;

const int floatPin = 2;     // the number of the floatswitch pin
const int relayPin =  13;      // the number of the relay pin

int floatState = 0;         // variable for reading the floatswitch status
#define pumprelay
void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.println("FISH FEEDER 1.1");
  myservo.write(80);
  delay(15);
  
  
  // FEED THE FISH..
  Serial.print("FEEDING FISH NOW ");

  for(pos = 90; pos < 95; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 95; pos>=90; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
   // initialize the relay pin as an output:
  pinMode(relayPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(floatPin, INPUT);     

}
 
 
void loop() 
{ 
  
  pumprelay ();
  
  // process times
  now = millis();
  endtime = now + FEEDINGDELAY;
  
  while(now < endtime) {
   myservo.write(80);
   Serial.print("soon... ");
   Serial.println(endtime-now);
   delay(20000);
   now = millis();   
  }
  
  // FEED THE FISH..
  Serial.print("FEEDING FISH at ");
  Serial.println(now);

  for(pos = 90; pos < 95; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 95; pos>=90; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position 
    break;
  } 
   
   /*-----*/
  
   void pumprelay (); 
   {
   // read the state of the floatswitch value:
  floatState = digitalRead(floatPin);

  // check if the floatswitch is low level.
  // if it is, the floatState is HIGH:
  if (floatState == HIGH) {     
    // turn relay on:    
    digitalWrite(relayPin, HIGH);  
  } 
  else {
    // turn relay off:
    digitalWrite(relayPin, LOW); 
  }}
}

How to use this forum - please read.

#7

// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }}

So the entire purpose of this function is to just set the LED to the state of the button? This could be done much easier by wiring it up that way, but if you insist on using software, then the entire function can be boiled down to a single line.

digitalWrite(ledPin, digitalRead(buttonPin));

I would also not recommend naming a function “pumprelay” if all it is doing it controlling an LED.

 // process times
  now = millis();
  endtime = now + FEEDINGDELAY;
  
  while(now < endtime) {
   myservo.write(80);
   Serial.print("soon... ");
   Serial.println(endtime-now);
   delay(20000);
   now = millis();   
  }

What exactly is the point of endtime here? You set it to three hundred milliseconds after the current time, then wait 20 seconds. May as well just remove the while structure, because it’s not doing anything for you.

cebenestelli:
Previously the pump relay code would only detect change or be implemented when the servo was operating. Is this solved by the way I have placed the void pumprelay function?

Your pumprelay function has zero coupling with the servo. In other words, they don’t affect each other (with the exception being the delays cause the pumprelay function to be called infrequently). Is that what you’re going for?

You have referred to two sketches that you are trying to combine but you haven't said what your project is intended to do. If the intention is to use a push-button switch to operate a relay for pump then I suggest you divide your code into two parts - one to read the button and save its value and the other to operate the pump depending on the saved value. Something like this

boolean buttonPressed = false; // assumes button push causes a LOW

void setup() {
   // usual stuff
}

void loop() {
   readAndSaveButton();
   operateRelay();
}

void readAndSaveButton() {
  if(digitalRead(buttonPin) == LOW) {
     buttonPressed = true;
  }
}

void operateRelay() {
  if (buttonPressed) {
     digitalRead(relayPin, HIGH);
  }
}

You will quickly see that this code has no means to change the buttonPressed back to false to turn off the relay - because I don't know when you want that to happen.

And if you also need to operate a servo you could add another function perhaps called moveServo().

Each of the functions will roughly correspond to the different sketches you are trying to incorporate.

...R

Sorry I have amended the names of the components. I’m an electrician but I have very little programming skill. I know how to hard wire this but I already have an arduino uno and a panel wired in such a way as to run off another sketch that is currently not functioning as intended.

Its for an aquaponic setup. The anticipated function of this sketch is to have the pump shut off, via the relay, if the tank reaches low level and also trigger a 240v rotating beacon near the office. The servo circuit is for an automatic fish feeder which I will later control in unison with a real time clock when it arrives.

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

//long FEEDINGDELAY = 43200000; // 12 hours
//  long FEEDINGDELAY = 57600000; // 16 hours
long FEEDINGDELAY = 300;

long endtime; 
long now;

const int floatPin = 2;     // the number of the floatswitch pin
const int relayPin =  13;      // the number of the relay pin

int floatState = 0;         // variable for reading the floatswitch status
#define pumprelay
void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.println("FISH FEEDER 1.1");
  myservo.write(80);
  delay(15);
  
  
  // FEED THE FISH..
  Serial.print("FEEDING FISH NOW ");

  for(pos = 90; pos < 95; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 95; pos>=90; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
   // initialize the relay pin as an output:
  pinMode(relayPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(floatPin, INPUT);     

}
 
 
void loop() 
{ 
  
  pumprelay ();
  
  // process times
  now = millis();
  endtime = now + FEEDINGDELAY;
  
  while(now < endtime) {
   myservo.write(80);
   Serial.print("soon... ");
   Serial.println(endtime-now);
   delay(20000);
   now = millis();   
  }
  
  // FEED THE FISH..
  Serial.print("FEEDING FISH at ");
  Serial.println(now);

  for(pos = 90; pos < 95; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 95; pos>=90; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position 
    break;
  } 
   
   /*-----*/
  
   void pumprelay (); 
   {
   // read the state of the floatswitch value:
  floatState = digitalRead(floatPin);

  // check if the floatswitch is low level.
  // if it is, the floatState is HIGH:
  if (floatState == HIGH) {     
    // turn relay on:    
    digitalWrite(relayPin, HIGH);  
  } 
  else {
    // turn relay off:
    digitalWrite(relayPin, LOW); 
  }}
}

[/quote]

Arrch: How to use this forum - please read.

Your pumprelay function has zero coupling with the servo. In other words, they don't affect each other (with the exception being the delays cause the pumprelay function to be called infrequently). Is that what you're going for?

I see. No thats not what I'm going for. I want the float switch (as I have renamed it) to be read every loop, not only when the servo operates.

Cheers

Arrch: What exactly is the point of endtime here? You set it to three hundred milliseconds after the current time, then wait 20 seconds. May as well just remove the while structure, because it's not doing anything for you.

It is only set to 300 for testing purposes. When finalised it be set to 16hrs. In milliseconds of course. The accuracy of the timing is of little concern. As king as the fish are fed.