Pages: [1]   Go Down
Author Topic: expected unqualified id before 'if'  (Read 933 times)
0 Members and 1 Guest are viewing this topic.
France - Bretagne
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//
//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.
« Last Edit: November 28, 2012, 03:52:20 am by Coding Badly » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Read this before posting a programming question
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26331
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: November 28, 2012, 04:19:50 am by AWOL » Logged

"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.

Milano
Offline Offline
Newbie
*
Karma: 0
Posts: 1
I am a tinkerer and a thinker
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
      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
Logged


New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3700
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
            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:
              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.
Logged

France - Bretagne
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: