1

I dont know what to do?

Sketch uses 3712 bytes (12%) of program storage space. Maximum is 30720 bytes.

Global variables use 210 bytes (10%) of dynamic memory, leaving 1838 bytes for local variables. Maximum is 2048 bytes.

Program:

String texto;
int flag;
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);//luz stop
pinMode(8, OUTPUT);//faros
}
void loop() {
while (Serial.available())//
{
delay (10);
if (Serial.available() > 0) //verifica que este disponible el puerto serie y verifica que haya algun
caracter
{
char c = Serial.read(); //guara cualquier caracter que haya llegado por el puerto serie
texto += c; //concatenamos
}
} ///Sentencias de control///
if (texto == "1") //compara el contenido de la avariable "texto" con el numero 1
{
digitalWrite(7,LOW); //Apaga la luz stop
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
}
if (texto == "3") //compara el contenido de la avariable "texto" con el numero 3
{
digitalWrite(7,LOW); //Apaga la luz stop
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
}
if (texto == "2") //compara el contenido de la avariable "texto" con el numero 2
{
digitalWrite(7,LOW); //Apaga la luz stop
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
}
if (texto == "4") //compara el contenido de la avariable "texto" con el numero 4
{
digitalWrite(7,LOW); //Apaga la luz stop
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
}
if (texto == "5") //Compara el contenido de la avariable "texto" con el numero 5
{
digitalWrite(7,HIGH); //Enciende la luz stop
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
if (texto == "6") //Compara el contenido de la avariable "texto" con el numero 6
{
if (flag==1)
{digitalWrite(8, LOW);
flag=0;
}
else if (flag==0)
{digitalWrite(8, HIGH);
flag=1;
}
}
texto = "";
}
2
  • What‘s the problem, you have lots of space left?
    – Sim Son
    Feb 4, 2020 at 18:06
  • I dont know what to do? ... i think that you are the only one who knows the answer to that question
    – jsotola
    Feb 4, 2020 at 18:37

1 Answer 1

1

Next time, select your code and press ctrl-K... Also align your code (indent it well).

However, you don't need help actually as you have plenty of memory left. As as written:

  • You only use 12% of program storage space. This might seem a lot having such a small sketch, but most of it is because of library usage/framework (such as the digitalWrite call(s).
  • 210 bytes of dynamic memory is also not something to worry. Btw, this is unrelated to the sketch size, but to your use of (global and other) variables.

So unless you know beforehand your sketch will be much bigger, don't change it because of that reason (too less space).

However, there is another reason, to shorten the sketch, and that is because of duplicated code. As you can see, there is in the entire sketch a lot of repetition (or almost repetition), as in:

if (texto == "5") //Compara el contenido de la avariable "texto" con el numero 5
{
  digitalWrite(7,HIGH); //Enciende la luz stop
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
}

What you can do to improve it is:

  • Either use a switch statement instead of the if/else statements
  • Or create a function that incorporates this condition, together with the following item. This solution is in your case the most clear.

What you do is creating a new function:

void setPins(bool condition, int pin7, int pin11, int pin12, int pin10, int pin9)
{
  if (condition)
  {
    digitalWrite(7, pin7);
    digitalWrite(11, pin11);
    digitalWrite(12, pin12);
    digitalWrite(10, pin10);
    digitalWrite(9, pin9);
  }
}

And you call your function with:

setPins(texto == "5", HIGH, LOW, LOW, LOW, LOW);

And the same for all other conditions. I only used 5 as example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.