Arduino Nano program bug problems?

Hi, first of all thans for the help that you do with this forum.

Now, I’m new in this life, making code to arduino hardware our other types of hardware, and I’m making one project in arduino and the result is no the expected…

I’m using Nano (clone) (I have 3) and not all the tests are ok. Some of them do the suposed but the most of the tests fails… Or the relay from h bridge doesn’t work, or the code jumps some steps…

The project consists on 6 buttons (3 analog input which one with 2 buttons with resistors making voltage drop), 2 outputs making rotate one motor (h bridge) to 3 positions (3 magnetic switches), 4 switches for control and 2 outputs making rotate other motor (h bridge)…

I puted anexed the project sketch.

The big wheel is always located on line 1.

Code (anexed also) (I put only 1 line of 3 because the project is to big):

// Pinos dos sensores
#define sensorEstacao1 A0 //line 1 end switch
#define sensorEstacao2 A1 //line 2 end switch
#define sensorEstacao3 A2 //line 3 end switch
#define OticoAgulha 2 //end switch

// Botoes chamada Estacoes
#define ChamadaEstacao1 A4 //select lines on station 1
#define ChamadaEstacao2 A5 //select lines on station 2
#define ChamadaEstacao3 A6 //select lines on station 3

// Pinos leds
int Green = 3; //led green
int Red = 4;   //led red

// Pinos motor agulha
int M_esq = 5; //platform left
int M_drt = 6; //platform right

// Pinos aspirante soprante
int aspirante = 7; //train front
int soprante = 8;  //train back

// Pinos posicionadores
int posicaoAgulha1 = 11;  //magnetic switch line 1
int posicaoAgulha2 = 10;  //magnetic switch line 2
int posicaoAgulha3 = 9;   //magnetic switch line 3

//
int valor_posicaoAgulha1;
int valor_posicaoAgulha2;
int valor_posicaoAgulha3;

int valor_sensorEstacao1;
int valor_sensorEstacao2;
int valor_sensorEstacao3;
int valor_OticoAgulha;

int Tempo;

int valorE1;
int valorE2;
int valorE3;

void setup() {

  Serial.begin(9600);

  pinMode(ChamadaEstacao1, INPUT_PULLUP);
  pinMode(ChamadaEstacao2, INPUT_PULLUP);
  pinMode(ChamadaEstacao3, INPUT_PULLUP);

  pinMode(sensorEstacao1, INPUT_PULLUP);
  pinMode(sensorEstacao2, INPUT_PULLUP);
  pinMode(sensorEstacao3, INPUT_PULLUP);

  pinMode(posicaoAgulha1, INPUT_PULLUP);
  pinMode(posicaoAgulha2, INPUT_PULLUP);
  pinMode(posicaoAgulha3, INPUT_PULLUP);

  pinMode(soprante, OUTPUT);
  pinMode(aspirante, OUTPUT);

  pinMode(OticoAgulha, INPUT_PULLUP);

  pinMode(Green, OUTPUT);
  pinMode(Red, OUTPUT);

  pinMode(M_esq, OUTPUT);
  pinMode(M_drt, OUTPUT);

  digitalWrite(M_drt, HIGH);
  digitalWrite(M_esq, HIGH);
  digitalWrite(aspirante, HIGH);
  digitalWrite(soprante, HIGH);
}

void loop() {

  Tempo = 3000;
  valorE1 = analogRead(ChamadaEstacao1);
  valorE2 = analogRead(ChamadaEstacao2);
  valorE3 = analogRead(ChamadaEstacao3);

  // INICIO DE CODIO QUE OBRIGA A AGULHA A FICAR NA ESTACAO 1

  if (valor_posicaoAgulha1 == LOW); {
    do {
      digitalWrite(M_drt, LOW);
      valor_posicaoAgulha1 = digitalRead(posicaoAgulha1);
    } while (valor_posicaoAgulha1 == HIGH);
    digitalWrite(M_drt, HIGH);
  }
  if (valor_posicaoAgulha2 == LOW); {
    do {
      digitalWrite(M_drt, LOW);
      valor_posicaoAgulha1 = digitalRead(posicaoAgulha1);
    } while (valor_posicaoAgulha1 == HIGH);
    digitalWrite(M_drt, HIGH);
  }
  if (valor_posicaoAgulha3 == LOW); {
    do {
      digitalWrite(M_drt, LOW);
      valor_posicaoAgulha1 = digitalRead(posicaoAgulha1);
    } while (valor_posicaoAgulha1 == HIGH);
    digitalWrite(M_drt, HIGH);
  }

  digitalWrite(Green, LOW);
  digitalWrite(Red, HIGH);

  // ESTACAO 1

  // ENVIAR DA ESTACAO 1 PARA A ESTACAO 2

  if (valorE1 >= 900 && valorE1 <= 950) {                     // Botao da estacao 2 situado na estacao 1
    digitalWrite(Green, LOW);                                 // Desliga Led verde e liga vermelho
    digitalWrite(Red, HIGH);
    do {
      digitalWrite(aspirante, LOW);                        // Liga motor aspirante
      valor_OticoAgulha = digitalRead(OticoAgulha);           // Fica à espera que o turpedo chegue à agulha
    } while (valor_OticoAgulha == HIGH);
    digitalWrite(aspirante, HIGH);                           // Assim que chega desliga o aspirante
    Serial.println(valor_OticoAgulha);
    //  delay(Tempo);                                             // Espera 3s para o motor parar
    do {
      digitalWrite(M_esq, LOW);                            // Muda a agulha para a estacao 2
      valor_posicaoAgulha2 = digitalRead(posicaoAgulha2);
    } while (valor_posicaoAgulha2 == HIGH);
    digitalWrite(M_esq, HIGH);                               // Liga motor soprante
    Serial.println(valor_posicaoAgulha2);
    do {
      digitalWrite(soprante, LOW);                         // Fica à espera que o turpedo chegue à estacao 2
      valor_sensorEstacao2 = digitalRead(sensorEstacao2);
    } while (valor_sensorEstacao2 == HIGH);                  // Assim que chega desliga o soprante
    digitalWrite(soprante, HIGH);
    Serial.println(valor_sensorEstacao2);
    do {
      digitalWrite(M_drt, LOW);                            // Envia a agulha de novo para a estacao 1
      valor_posicaoAgulha1 = digitalRead(posicaoAgulha1);
    } while (valor_posicaoAgulha1 == HIGH);
    digitalWrite(M_drt, HIGH);
    digitalWrite(Red, LOW);                                   // Desliga o led vermelho e liga o verde
    digitalWrite(Green, HIGH);
  }

  // ENVIAR DA ESTACAO 1 PARA A ESTACAO 3

  if (valorE1 >= 700 && valorE1 <= 750) {                     // Botao da estacao 3 situado na estacao 1
    digitalWrite(Green, LOW);                                 // Desliga Led verde e liga vermelho
    digitalWrite(Red, HIGH);
    do {
      digitalWrite(aspirante, LOW);                        // Liga motor aspirante
      valor_OticoAgulha = digitalRead(OticoAgulha);           // Fica à espera que o turpedo chegue à agulha
    } while (valor_OticoAgulha == HIGH);
    digitalWrite(aspirante, HIGH);                           // Assim que chega desliga o aspirante
    //  delay(Tempo);                                             // Espera 3s para o motor parar
    do {
      digitalWrite(M_esq, LOW);                            // Muda a agulha para a estacao 3
      valor_posicaoAgulha3 = digitalRead(posicaoAgulha3);
    } while (valor_posicaoAgulha3 == HIGH);
    digitalWrite(M_esq, HIGH);                               // Liga motor soprante
    do {
      digitalWrite(soprante, LOW);                         // Fica à espera que o turpedo chegue à estacao
      valor_sensorEstacao3 = digitalRead(sensorEstacao3);
    } while (valor_sensorEstacao3 == HIGH);                  // Assim que chega desliga o soprante
    digitalWrite(soprante, HIGH);
    do {
      digitalWrite(M_drt, LOW);                            // Envia a agulha de novo para a estacao 1
      valor_posicaoAgulha1 = digitalRead(posicaoAgulha1);
    } while (valor_posicaoAgulha1 == HIGH);
    digitalWrite(M_drt, HIGH);
    digitalWrite(Red, LOW);                                   // Desliga o led vermelho e liga o verde
    digitalWrite(Green, HIGH);
  }

}

If you can help, I’d be grateful.
And sorry for the bad english, I’m portuguese.

Moderator edit: code tags

projecto.ino (13.2 KB)

In your code you do this:

#define ChamadaEstacao3 A6 //select lines on station 3
...
  pinMode(ChamadaEstacao3, INPUT_PULLUP);
...
  valorE3 = analogRead(ChamadaEstacao3);
...

on an Arduino Nano, the A6 and A7 are Analog only

--> those 2 pins are inputs just to the ADC mux, without any digital capabilities attached to the pins. Since the pull-up resistors/circuitry is part of the digital latch, those two pins dont have pull ups and so you can't use A6 (nor A7) if you need INPUT_PULLUP on a Nano. Use a regular digital pin for example.

Thanks for the reply,

and the A0 to A5 doesn't have problem to be INPUT_PULLUP??

Because I have the A3 that is not used and I can pass to that pin..

And is this the problem that causes so many problems?

Thanks.

A0 to A5 are fine indeed

That could be a cause for your trouble - I've not read everything, try by fixing this first

Ok, already done the changes

// Botoes chamada Estacoes
#define ChamadaEstacao1 A3 //select lines on station 1
#define ChamadaEstacao2 A4 //select lines on station 2
#define ChamadaEstacao3 A5 //select lines on station 3

pinMode(ChamadaEstacao1, INPUT);
pinMode(ChamadaEstacao2, INPUT);
pinMode(ChamadaEstacao3, INPUT);

valorE1 = analogRead(ChamadaEstacao1);
valorE2 = analogRead(ChamadaEstacao2);
valorE3 = analogRead(ChamadaEstacao3);

but the arduino keep making the same things..

Often do not operate the relays, skips the programming..

I'm a beginner and most likely to be something in programming.. I just do not know what I'm doing wrong :frowning:

The nano have some type of special programming configuration/parameters on the Arduino Software (IDE)?

I think the problem that I have is something in the code or compilation..

  if (valor_posicaoAgulha1 == LOW); {

You have two things wrong there. First you have a semicolon (:wink: after the if(). That will end the if() statement right there. The {} code bock following the if(); will always run.

Second, you are using the value of a variable before you assigned anything to it. (This one I copied is the first one at the top of the loop().) If you are depending on the value of valor_posicaoAgulha1 starting at any specific value, then you should make that explicit where you declare the variable or in setup().