How can I turn on 4 leds and activate 2 servos with 2 push buttons?

can i help me please

Did you try to write the code yourself? Show your efforts

yes because it does not work

#include <Servo.h>

Servo myservo;

const int buttonPin = 2;
int val;
int buttonState = 0;

void setup()
{
myservo.attach(9);
pinMode(buttonPin, INPUT);
}

void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
val = 90;
}
else {
val = 0;
}
myservo.write(val);
delay(15);
}

and this
int verde = 2;
int amarillo = 3;
int rojo = 4;
int azul = 5;
int bt_am = 6;
int bt_r = 7;
int bt_az = 8;
int bt_mq = 9;

int boton_amarillo;
int boton_rojo;
int boton_azul;
int boton_maquina;

void setup()
{
pinMode (amarillo, OUTPUT);
pinMode (rojo, OUTPUT);
pinMode (azul, OUTPUT);
pinMode (verde, OUTPUT);
pinMode (bt_am, INPUT);
pinMode (bt_r, INPUT);
pinMode (bt_az, INPUT);
pinMode (bt_mq, INPUT);

}

what I don't know is how to do all this together, that is, turn on 4 leds, move 2 servos and activate it with the push button

First, insert your code with code tags <\>

And next - i don't see any leds in the code

That's why I tell you that I don't know how to do it

<int verde = 2;
int amarillo = 3;
int rojo = 4;
int azul = 5;
int bt_am = 6;
int bt_r = 7;
int bt_az = 8;
int bt_mq = 9;

int boton_amarillo;
int boton_rojo;
int boton_azul;
int boton_maquina;

void setup()
{
pinMode (amarillo, OUTPUT);
pinMode (rojo, OUTPUT);
pinMode (azul, OUTPUT);
pinMode (verde, OUTPUT);
pinMode (bt_am, INPUT);
pinMode (bt_r, INPUT);
pinMode (bt_az, INPUT);
pinMode (bt_mq, INPUT);

}

this is other part of code

Please follow the rules in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

How to do multi things in the time - code example:

How to merge two arduino codes in one:
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

1st step is to understand how do you want the buttons to control the LEDs and servos?

should one button sequentially turn each button on and off and the other button to sequentially move each servo?

should the each button simply control one of the servos, possibly moving it between 2 positions and a pair of LEDs indicating the positions?


to post you code, select the "</>" icon and paste your code in the highlighted area.

in the code below, looks like you simply move one servo to either 0 or 90 deg depending on if the button is being pressed or not.

is this what you were trying to do at this point? or did you borrow the code from somewhere.

you will probably want code that recognized a button press, not simply that the button is being pressed, and sequence thru a set of states

void loop()
{
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
        val = 90;
    }
    else {
        val = 0;
    }
    myservo.write(val);
    delay(15);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.