Arduino Noob Code

Cant figure out why isn't this working. Please help!
Push Button doesn't activate. TinkerCad Link attached

//librerias
#include <Servo.h>
Servo AEXT; //representa A+
Servo DOOR; //portones
String entradas = ""; //inputs seriales

void loop() {
//INICIA COMUNICACION SERIAL
Serial.begin(9600);

//DIGITAL INPUTS
pinMode (3, INPUT); //HS00
pinMode (4, INPUT); //HS01
pinMode (5, INPUT); //HS02
pinMode (6, INPUT); //LLAVE
pinMode (7, INPUT); //FCS
pinMode (8, INPUT); //FCI

//DIGITAL OUTPUTS
AEXT.attach(9); //A+ Y A-

pinMode (11, OUTPUT); //CPA
pinMode (12, OUTPUT); //CPB
pinMode (13, OUTPUT); // YL00
pinMode (14, OUTPUT); //YL01
pinMode (15, OUTPUT); //YL02
pinMode (16, OUTPUT); //YL03
pinMode (17, OUTPUT); //YL04
pinMode (18, OUTPUT); //YL05
pinMode (19, OUTPUT); //YL06

//MENSAJE DE OPERACIONAL
Serial.println("LISTO PARA INGRESAR");

}

void setup() {
while(Serial.available()){
entradas = Serial.read();

//Cuando se presiona HS00:
//Activar CPA
//Activar A+
//Activar YL00
if (digitalRead(3)==HIGH)
{
digitalWrite(11,HIGH);
AEXT.write(180);
digitalWrite (13,HIGH);

  }

Login | Tinkercad)

Hello
What are your question and your expectations ?

I need to press the pushbutton in order to activate the servo and led, but this doesn´t seem to work out

You have setup code in loop and vice versa - switch them around.

1 Like

Post your sketch, well formated, with comments and in so called code tags "</>" to see how we can help.

Define names for your pin numbers and use the names. The code is hard to read if you have to memorize what each of the numbers means.

//librerias
#include <Servo.h>
Servo AEXT; //representa A+
Servo DOOR; //portones

// Inputs
const byte HS00_Pin = 3;
const byte HS01_Pin = 4;
const byte HS02_Pin = 5;
const byte LLAVE_Pin = 6;
const byte FCS_Pin = 7;
const byte FCI_Pin = 8;

// Outputs
const byte AEXT_Pin = 9; //A+ Y A-

const byte CPA_Pin = 11;
const byte CPB_Pin = 12;
const byte YL00_Pin = 13;
const byte YL01_Pin = A0;
const byte YL02_Pin = A1;
const byte YL03_Pin = A2;
const byte YL04_Pin = A3;
const byte YL05_Pin = A4;
const byte YL06_Pin = A5;

String entradas = ""; //inputs seriales

void setup()
{
  //INICIA COMUNICACION SERIAL
  Serial.begin(9600);

  AEXT.attach(AEXT_Pin); //A+ Y A-

  //DIGITAL INPUTS
  pinMode (HS00_Pin, INPUT);
  pinMode (HS01_Pin, INPUT);
  pinMode (HS02_Pin, INPUT);
  pinMode (LLAVE_Pin, INPUT);
  pinMode (FCS_Pin, INPUT);
  pinMode (FCI_Pin, INPUT);

  //DIGITAL OUTPUTS
  pinMode (CPA_Pin, OUTPUT);
  pinMode (CPB_Pin, OUTPUT);
  pinMode (YL00_Pin, OUTPUT);
  pinMode (YL01_Pin, OUTPUT);
  pinMode (YL02_Pin, OUTPUT);
  pinMode (YL03_Pin, OUTPUT);
  pinMode (YL04_Pin, OUTPUT);
  pinMode (YL05_Pin, OUTPUT);
  pinMode (YL06_Pin, OUTPUT);

  //MENSAJE DE OPERACIONAL
  Serial.println("LISTO PARA INGRESAR");
}

void loop()
{
  while (Serial.available())
  {
    entradas = Serial.read();
  }

  //Cuando se presiona HS00: Activar CPA, A+, YL00
  if (digitalRead(HS00_Pin) == HIGH)
  {
    digitalWrite(CPA_Pin, HIGH);
    AEXT.write(180);
    digitalWrite (YL00_Pin, HIGH);
  }
}

//librerias
#include <Servo.h>
Servo AEXT; //representa A+
Servo DOOR; //portones
String entradas = ""; //inputs seriales

void loop() {
//INICIA COMUNICACION SERIAL
Serial.begin(9600);

//DIGITAL INPUTS
pinMode (3, INPUT); //HS00
pinMode (4, INPUT); //HS01
pinMode (5, INPUT); //HS02
pinMode (6, INPUT); //LLAVE
pinMode (7, INPUT); //FCS
pinMode (8, INPUT); //FCI

//DIGITAL OUTPUTS
AEXT.attach(9); //A+ Y A-

pinMode (11, OUTPUT); //CPA
pinMode (12, OUTPUT); //CPB
pinMode (13, OUTPUT); // YL00
pinMode (14, OUTPUT); //YL01
pinMode (15, OUTPUT); //YL02
pinMode (16, OUTPUT); //YL03
pinMode (17, OUTPUT); //YL04
pinMode (18, OUTPUT); //YL05
pinMode (19, OUTPUT); //YL06

//MENSAJE DE OPERACIONAL
Serial.println("LISTO PARA INGRESAR");

}

void setup() {
while(Serial.available()){
entradas = Serial.read();

//Cuando se presiona HS00:
//Activar CPA
//Activar A+
//Activar YL00
if (digitalRead(3)==HIGH)
{
digitalWrite(11,HIGH);
AEXT.write(180);
digitalWrite (13,HIGH);

      //Esperar a FCS       
            if(digitalRead(7)==HIGH)
            {
              //encender YL01
                digitalWrite(14,HIGH);
                //while reciba info del serial
                  while(1){
                      //vacio
                  }

                  // probando con void loop
                  while(entradas!="HE2"){
                      //esperar datos                            
                          if(Serial.available()>0){
                                  //asignar datos a una variable
                                    entradas = Serial.read();  
                        }
                        }
                                           
                //
                if(entradas=="HE2")
                {
                  //Activar A- Y ACTIVAR YL00/APAGAR YL01
                    digitalWrite (14,LOW);
                    digitalWrite (13, HIGH);
                          AEXT.write(0);
                                  //Esperar FCI, aunque se enciende HE3 Y HE4
                                    if(digitalRead(8)==HIGH)
                                                                          
                                             {
                                                //Para poder luego activar:
                                                    //CPB y YL05 Y YL01/APAGAR YL0
                                                       digitalWrite(12,HIGH);  //CPB ON
                                                          digitalWrite (13,LOW);
                                                            digitalWrite (14, HIGH);
                                                              digitalWrite(18,HIGH); // YL05 ON
                                   
                                      
                                      }
                                           // probando con void loop para dejar de sensar HE3
                                            while(entradas!="HE3") {
                                                //esperar datos
                                                  
                                                     if(Serial.available()>0)
                                                        {

                                                 //asignar datos a una variable
                                                      entradas = Serial.read();  
                                                           } 
                                                           }
                                                            

                                       if(entradas=="HE3 OFF")
                                                          {
                                                          //esperar que se dejen de sensar HE3 para desactivar CPB
                                                            digitalWrite(12,LOW);  //CPB OFF
                                                             }

                                                                       // probando con void loop para HE5
                                                                                  while(entradas!="HE5"){
                                                                                          if(Serial.available()>0){
                                                                                                  //asignar datos a una variable
                                                                                                      entradas = Serial.read();    
                                                                                                      }
                                                                                  }
                                                                                  if(entradas=="HE5")
                                                                                    {
                                                                                    //Despues esperar HE5 para poder llevar a la etapa inicial en la que estan CPA y 
                                                                                    //CPB van en contrario
                                                                                     
                                                                                      digitalWrite(12,LOW);  //CPB  INVERTIDO
                                                                                      digitalWrite(11,LOW);  //CPA  INVERTIDO

                                                                                    }
                                                          }
                                                         
                  
                  }
          
            
            }

  
  
  }

}

You have to put Serial.begin(baudRate); befor Serial.read().

entradas = Serial.read(); only reads one character. Your 'entradas' String will NEVER be equal to "HE2" or "HE3" or "HE3 OFF". Use:
entradas = Serial.readString();
That should work if your Serial Monitor is set to "No line ending". To work with other line endings, use:
if (entradas.startsWith("HE2"))
WARNING:
if (entradas.startsWith("HE3"))
will match both "HE3" and "HE3 OFF" so check for "HE3 OFF" first or check for "HE3 OFF" inside the if (entradas.startsWith("HE3")):

  if (entradas.startsWith("HE3 OFF"))
  {
     // Do "HE3 OFF"
  }
  else if (entradas.startsWith("HE3"))
  {
     // Do "HE3"
  }

or

  if (entradas.startsWith("HE3"))
  {
   if (entradas.startsWith("HE3 OFF"))
    {
       // Do "HE3 OFF"
    }
    else
    {
      // Do "HE3"
    }
  }

@hetch69
1. Connect an external pull-down resistor (about 2.2k) with Dpin-3.
2. Uplaod your sketch shown below with slight revision.

//librerias
#include <Servo.h>
Servo AEXT; //representa A+
Servo DOOR; //portones
String entradas = ""; //inputs seriales

void setup()
{
  //INICIA COMUNICACION SERIAL
  Serial.begin(9600);

  //DIGITAL INPUTS
  pinMode (3, INPUT); //HS00
  pinMode (4, INPUT); //HS01
  pinMode (5, INPUT); //HS02
  pinMode (6, INPUT); //LLAVE
  pinMode (7, INPUT); //FCS
  pinMode (8, INPUT); //FCI

  //DIGITAL OUTPUTS
  AEXT.attach(9); //A+ Y A-

  pinMode (11, OUTPUT); //CPA
  pinMode (12, OUTPUT); //CPB
  pinMode (13, OUTPUT); // YL00
  pinMode (14, OUTPUT); //YL01
  pinMode (15, OUTPUT); //YL02
  pinMode (16, OUTPUT); //YL03
  pinMode (17, OUTPUT); //YL04
  pinMode (18, OUTPUT); //YL05
  pinMode (19, OUTPUT); //YL06

  //MENSAJE DE OPERACIONAL
  Serial.println("LISTO PARA INGRESAR");

}

void loop()
{
  //while (Serial.available())
  //{
  //entradas = Serial.read();

  //Cuando se presiona HS00:
  //Activar CPA
  //Activar A+
  //Activar YL00
  if (digitalRead(3) == HIGH)
  {
    digitalWrite(11, HIGH);
    AEXT.write (97);//(180);
    digitalWrite (13, HIGH);
  }
}

Please read the instructions.

This workout for me, now I'm having issues whenever I get to this part.

while(entradas!="HE2"){
//esperar datos
if(Serial.available()>0){
//asignar datos a una variable
entradas = Serial.read();
}
}

                //
                if(entradas=="HE2")
                {
                  //Activar A- Y ACTIVAR YL00/APAGAR YL01
                    digitalWrite (14,LOW);
                    digitalWrite (13, HIGH);
                          AEXT.write(0);
                }

Which I need the Arduino after getting FCS signal, to wait for me to write back to it.
This makes the previous stage to fail. Any thoughts?

Please read the instructions.

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