Go Down

Topic: expected unqualified id before 'if' (Read 1 time) previous topic - next topic

Marc22190

Nov 28, 2012, 09:49 am Last Edit: Nov 28, 2012, 09:52 am by Coding Badly Reason: 1
Thank you for your help,
I block on my program and I turn in circle for the solution.
The part piloted by the PC functions well. The part piloted by the buttons sends me a message of mistake: expected unqualified id before 'yew'
Here is my program:

Code: [Select]
//
//Controle la vitesse et du sens de rotation du moteur Accufocus Orion brancgé en M1
//sur le sheild Adafruit
//Lecture du port com
//quand a il augmente la vitesse de 1
//quand d il diminue la vitesse de 1
//o avance dans le sens anti horaire le focus rentre
//i avance dans le sens horaire le focus sort
//Utilisation par PC via le port USB et en manuel par boutons
//Cela conserve la raquette Orion dans son état normale.
//
#include <Button.h>
Button buttonin = Button(14, BUTTON_PULLUP);
Button buttonout = Button(15, BUTTON_PULLUP);
Button buttond = Button(16, BUTTON_PULLUP);
Button buttona = Button(17, BUTTON_PULLUP);

#include <AFMotor.h>
   AF_DCMotor motor1(1, MOTOR12_64KHZ);

   char val;
   int vitesse = 125;
   int i = 0;
   int x = 0;
 
   void setup()
   {
     Serial.begin(9600);
     motor1.setSpeed(vitesse);
   }
   void loop()
   {
       if(Serial.available())
       {
           if(Serial.available()>2)
           {            
             x = Serial.available();
             char vitread[x-2];
             //Serial.println(x);
             for (i = 0; i <= x-2; i++)
             {
               vitread[i] = Serial.read();
             }
             vitesse = atoi(vitread);
             Serial.println(vitesse);
             motor1.setSpeed(vitesse);
             Serial.flush();
           }
           else
           {
             val = Serial.read();
             //Serial.print(val);
           }
       }
      switch (val){
        case 'a':
          if (vitesse < 255)
          {
            vitesse++;
            Serial.println(vitesse);
            motor1.setSpeed(vitesse);
          }
          break;
        case 'd':
          if (vitesse > 0)
          {
            vitesse--;
            Serial.println(vitesse);
            motor1.setSpeed(vitesse);
          }
          break;
        case 'o':
          motor1.run(FORWARD);
        break;
        case 'i':
           motor1.run(BACKWARD);
          break;
      default:
      motor1.run(RELEASE);
   }
   delay(100);
   }
//erreure ici
if (buttonin.isPressed()){
motor1.run(BACKWARD);
          break;
if (buttonout.isPressed()){
motor1.run(FORWARD);
          break;
}
if (buttond.isPressed()){
if (vitesse > 0)
          {
            vitesse--;
            Serial.println(vitesse);
            motor1.setSpeed(vitesse);
          }
          break;
  if (buttona.isPressed()){
if (vitesse < 255)
          {
            vitesse++;
            Serial.println(vitesse);
            motor1.setSpeed(vitesse);
          }
          break;
}



Moderator edit: [code] [/code] tags added.

Nick Gammon

Can you copy and paste the error message please? I don't see "yew" in there.

Read this before posting a programming question
http://www.gammon.com.au/electronics

AWOL

#2
Nov 28, 2012, 10:11 am Last Edit: Nov 28, 2012, 10:19 am by AWOL Reason: 1
Code: [Select]
for (i = 0; i <= x-2; i++)
             {
               vitread[i] = Serial.read();
             }
             vitesse = atoi(vitread);

atoi takes a null-terminated string; is your string null-terminated?

The IDE has a handy-dandy feature called auto-format which will help your indentation.
Better still, develop the habit of when you find yourself writing a "{", make sure you type its matching "}" at the same llevel of indentation, and keep your style consistent.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ebmrjfm

Hi Marc,
Are you sure about the block separation in this code? In particular, I can see a problem here:

Code: [Select]
      else
            {
              val = Serial.read();
              //Serial.print(val);
            }
        }
       switch (val){


The "val" variable is not initialized (it is just declared) outside of that "else" condition.
However, the program reaches that "switch" instruction even when the "else" code is not executed.
At that moment, "val" holds a random value and should not be used for a "switch" statement.
Hope this helps!
Riccardo
http://aliencoding.com

wildbill

Quote
The IDE has a handy-dandy feature called auto-format which will help your indentation.


In case it's not obvious, this is not just useful information - it's a strong hint to help find what is wrong.

PaulS

Code: [Select]
            if(Serial.available()>2)
            {           
              x = Serial.available();
              char vitread[x-2];
              //Serial.println(x);
              for (i = 0; i <= x-2; i++)
              {
                vitread[i] = Serial.read();
              }

So, if there are two bytes, and the first one is 33, read 31 of the remaining bytes. How's that going to work. Here's a hint. Not well at all.

Code: [Select]
              Serial.flush();
Why is this here? If you are using 0023 or earlier, then you want to throw away random amounts of unread data. There won't be any because you've just tried to read more than were there, most likely. Even if there was unread data, why would you want to throw it away?

If you are using 1.0 or later, flush() simply blocks until all unsent data is sent. I can't imagine why that would be important here.

Unless you've got a really good explanation for using this function, quit doing it. If you do, I'd really like to hear it.

Marc22190

Thank you for the attention that you carry to my problem,
I corrected the two lines with / / that I had put at the time of attempts and that I had forgotten to remove before mailing.
My program turns so much that I don't use the section that manages the buttons from the line 84 -  if (buttonin.isPressed ()) {  -.
The goal of this program is to use a motor to make the clarification on a téléscope.
That it is in manual by the buttons or in automatic managed since the PC.
Of advance, excuse me for my English. I use Google for the translation since the French.

Go Up