else without a previous if problem.. -_-

else without a previous if problem.. -_-

oid loop()
{
while (Serial.available())
{
if (val == 'z')
val = Serial.read();
{
for(int i = 49; i >=0 ; i--)
{
LED_DATA = ' ';

  • a = "";*
  • a.trim();*
  • }*
  • }*
  • else*
  • val = Serial.read();*
  • {*
  • (a += val);*
  • a.toCharArray(LED_DATA, 50);*
  • Serial.print(LED_DATA);*
  • Serial.flush();*
  • } *
  • }*
  • Serial.print(LED_DATA);*
  • Display();*
  • shift();*
    }
    problem in else line..
  • }*
  • else //problem is here*
  • val = Serial.read();*
  • {*

Why is it a problem. That is what you wrote. I think you are forgetting that an if statement ends after one statement if you have not included the statements you want in braces { }

oid loop()
{
  while (Serial.available())
  {
    if (val == 'z')
    val = Serial.read();

Your problem is here. There are basically two ways to write an "if" statement:

if (test)
  commandOne();

commandTwo();

In this case, only a single line of code follows the if() statement. No brackets are needed. Importantly, if you write this as:

if (test)
  commandOne();
  commandTwo();

The compiler doesn't care at all how you indented the lines. Only commandOne() is dependent on the if statement. commandTwo will always execute.

If you have multiple lines that are dependent on the if statement, you must enclose them in brackets.

if (test)
{
  commandOne();
  commandTwo();
}

What is confusing you is that you can include whatever code you want in brackets, such as:

if (test)
  commandOne();
  {
    commandTwo();
  }

The fact that commandTwo() is enclosed in brackets has no effect at all, because the brackets do not immediately follow the if() statement.