Servo motor with Serial Monitor for automatic gate

Hello everyone!
i'm doing a code for an automatic door...so I want to use the monitor serial, because i will put this code in an app.
so i code like this:

#include <Servo.h>
byte acao = 0;
Servo PortaoGaragem;
int pos = 0; // variavel de armanezamento da posição do servo //

void setup() {

// Portão Garagem //
// 1 => Abrir //
// 2 => Fechar //
PortaoGaragem.attach(9); // attach (anexar) referente ao pino que suporta o servo //
Serial.begin(9600);
PortaoGaragem.write(0);
}

void loop() {

//Portão Garagem //
acao = 0;
if (Serial.available() > 0)
{
acao = Serial.read();
if (acao == 49 && pos < 10) //1 = abrir
{
Serial.println("abrir");
for (pos = 0; pos <= 80; pos += 1)
{
PortaoGaragem.write(pos);
delay(50);
}
acao = 1;
}
}
acao = 1;
if (Serial.available() > 1)
{
acao = Serial.read();
if (acao == 1 && pos < 80) //2 = fechar
{
Serial.println("fechar");
for (pos = 80; pos <= 0; pos +=1)
{
PortaoGaragem.write(pos);
delay(50);
}
acao = 1;
}
}
}

but the monitor serial have been reading just "abrir" (1) and not "fechar" (2).

what i can do?

byte acao = 0;
. . .
acao = Serial.read();

You might want to change this to char .


acao = Serial.read();
if (acao == 1 && pos < 80) //2 = fechar

How are you sending 1 (acao == 1) on you serial monitor ?

why cant i use "byte acao = 0; "?

in my serial monitor i'm seendig 1 to open de door, look:

void loop() {

//Portão Garagem //
acao = 0;
if (Serial.available() > 0)
{
acao = Serial.read();
if (acao == 49 && pos < 10) //1 = abrir
{
Serial.println("abrir");
for (pos = 0; pos <= 80; pos += 1)
{
PortaoGaragem.write(pos);
delay(50);

i'm not seendig acao == 1 or acao == 2, just "1"...and i'd like to use "2" to close the door.

Google ASCII



char acao = ‘0’;
. . .
void loop() {

//Portão Garagem //
acao = ‘0’;
if (Serial.available() > 0)
{
acao = Serial.read();
if (acao == ‘1’ && pos < 10) //1 = abrir
{
Serial.println("abrir");
for (pos = 0; pos <= 80; pos += 1)
{
PortaoGaragem.write(pos);
delay(50);

well, the problem is here yet.

the problem is, when i send "1" to my serial monitor, the servo goes from 0º to 80º twice. and when i send 2 the servo goes from 80º to 0º.

the servo isn't respecting may command, that would be "1" to open//abrir or "2" to close//fechar.

#include <Servo.h>

Servo PortaoGaragem;

char acao;

int pos; // variavel de armanezamento da posição do servo //


//**********************************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(9, OUTPUT);

  // Portão Garagem //
  // 1 => Abrir //
  // 2 => Fechar //
  PortaoGaragem.write(0);
  PortaoGaragem.attach(9);
  pos = 0;

  delay(500);
  
} //END of   setup()


//**********************************************************************************
void loop()
{
  //******************************
  while (Serial.available() > 0)
  {
    acao = Serial.read();

    if (acao == '1' || acao == '2')
    {
      break;
    }
  }

  //******************************
  if (acao == '1' && pos == 0)
  {
    acao = '0';

    Serial.println("abrir");

    for (pos = 0; pos < 80; pos = pos + 1)
    {
      PortaoGaragem.write(pos);

      delay(50);
    }
  }

  //******************************
  if (acao == '2' && pos == 80)
  {
    acao = '0';

    Serial.println("fechar");

    for (pos = 80; pos > 0; pos = pos - 1)
    {
      PortaoGaragem.write(pos);

      delay(50);
    }
  }

} //END of   loop()

wow it worked!!! thanks xD

i'll study more about this "while", well i think that this whille allowed the if w/ break.

this topic is cloosed XD