-3

I am creating a project with Arduino Uno and some other parts such as:

  • Mg996r servo1 (connected to a elevator system)
  • Mg996r servo2 (connected to a rotating tap)
  • a 4 channel relay that is connected to 4 vacuum pumps
  • Momentary switch x4

The problem I am having is I need the following actions to happen in this order:

button activated(1-4) -> servo1 activated for a certain amount of rotations -> servo2 activated for around 70 degrees -> pump is activated(1-4, depending on the button I chose) -> servo 2 activated back to 0 degrees -> servo1 activated for a certain number of rotations.

Another problem on how to activate different parts of the code depending on the button you pressed (same code except a different pump will fire).

I expected to be able to control the program through buttons (press a button, certain part of program is activated).

halfer
  • 19,824
  • 17
  • 99
  • 186
  • could you post your code? If you don't know how to start I'd recommend taking a tutorial so you can learn the basics of Arduino, like reading buttons, moving servos or turning on LEDs. – Sembei Norimaki Jun 19 '23 at 11:19

1 Answers1

0
#include <Servo.h>    
// number of button pins
const int button1Pin = BUTTON1PIN; 
const int button2Pin = BUTTON2PIN;  
const int button3Pin = BUTTON3PIN;  
const int button4Pin = BUTTON4PIN;  
// create servo object to control a servo
Servo servo1;  
Servo servo2;

int pos1 = 0;    // variable to store the servo1 position
int pos2 = 0;    // variable to store the servo2 position
        
// variables for reading the buttons status
int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
        
void setup() {
  // initialize the button pins as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  // attach pins to the servo objects
  servo1.attach(SERVO1PIN); 
  servo2.attach(SERVO2PIN); 
}
        
void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
       
  // check if any button has been pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
      rotateServos();
      // activate pump 1
      rotateServosBack();
  } else if (buttonState2 == HIGH) {
      rotateServos();
      // activate pump 2
      rotateServosBack();
  } else if (buttonState3 == HIGH) {
      rotateServos();
      // activate pump 3
      rotateServosBack();
  } else if (buttonState4 == HIGH) {
      rotateServos();
      // activate pump 4
      rotateServosBack();
  }
}


void rotateServos() {
  
  for (pos1 = 0; pos1 >= 360; pos1 += 1) {   // goes from 0 degrees to 360 degrees
    servo1.write(pos1); // tell servo to go to position in variable 'pos'
    delay(15);          // waits 15ms for the servo to reach the position
  }

  for (pos2 = 0; pos2 <= 70; pos2 += 1) { 
    servo2.write(pos2);
    delay(15);
  }
}

void rotateServosBack() {
  for (pos1 = 360; pos1 >= 0; pos1 -= 1) {                   
    servo1.write(pos1); 
    delay(15);         
  }

  for (pos2 = 70; pos2 >= 0; pos2 -= 1) {                  
    servo2.write(pos2); 
    delay(15);         
  }
}

Make sure to watch through demo/example code and read through the documentation of the parts you are using. You still have to implement to activate each pump. The first part to learning how to code is struggling and piecing your code together until it finally works.

Michael
  • 31
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 27 '23 at 09:23
  • I will make sure to examine the code to understand how it works so I can edit it to my liking. i think I'm on the right track with adding the pumps because they are connected to a relay so what I learnt is I need to define a relay connected to a pin and then determine that it's an output. Thank you for all the help. – Samuel Olds Jun 28 '23 at 23:46