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

So this is my code:
Code:
#include <Wire.h>

void setup()
{
  Serial.begin(19200);
  nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
  nunchuck_init(); // write the initilization handshake
  Serial.print ("Finished setup\n");
}

void loop()
{
  nunchuck_get_data();
  nunchuck_print_data();
  delay(100);
}


//
// Nunchuck functions
//

static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize        
}

// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init()
{
  Wire.begin();                // join i2c bus as master
  Wire.beginTransmission(0x52); // transmit to device 0x52
  Wire.write(0x40); // writes memory address
  Wire.write(0x00); // writes sent a zero.  
  Wire.endTransmission(); // stop transmitting
}

// write a request for data to the nunchuck
// was "write_zero()"
void nunchuck_write_request()
{
  Wire.beginTransmission(0x52); // transmit to device 0x52
  Wire.write(0x00); // writes one byte
  Wire.endTransmission(); // stop transmitting
}

// Receive data back from the nunchuck,
int nunchuck_get_data()
{
    int cnt=0;
    Wire.requestFrom (0x52, 6); // request data from nunchuck
    while (Wire.available ()) {
      // receive byte as an integer
      nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
      cnt++;
    }
    nunchuck_write_request();  // write request for next data payload
    // If we recieved the 6 bytes, then go print them
    if (cnt >= 5) {
     return 1;   // success
    }
    return 0; //failure
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
void nunchuck_print_data()
{
  static int i=0;
  int joy_x_axis = nunchuck_buf[0];
  int joy_y_axis = nunchuck_buf[1];
  int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
  int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
  int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;

  int z_button = 0;
  int c_button = 0;

  // byte nunchuck_buf[5] contains bits for z and c buttons
  // it also contains the least significant bits for the accelerometer data
  // so we have to check each bit of byte outbuf[5]
  if ((nunchuck_buf[5] >> 0) & 1)
    z_button = 1;
  if ((nunchuck_buf[5] >> 1) & 1)
    c_button = 1;

  if ((nunchuck_buf[5] >> 2) & 1)
    accel_x_axis += 2;
  if ((nunchuck_buf[5] >> 3) & 1)
    accel_x_axis += 1;

  if ((nunchuck_buf[5] >> 4) & 1)
    accel_y_axis += 2;
  if ((nunchuck_buf[5] >> 5) & 1)
    accel_y_axis += 1;

  if ((nunchuck_buf[5] >> 6) & 1)
    accel_z_axis += 2;
  if ((nunchuck_buf[5] >> 7) & 1)
    accel_z_axis += 1;


  Serial.print("acc:");
  Serial.print(accel_x_axis, DEC);
  Serial.print(",");
  Serial.print(accel_y_axis, DEC);
  Serial.print(",");
  Serial.print(accel_z_axis, DEC);
  Serial.print("\t");
  Serial.print(sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)));

  Serial.print("\r\n");  // newline
  i++;
}

if(endValue != 0) {
    if (endValue != (sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2));){
    //--- licht aan
    }
  }

endValue = sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2));
    
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}

And I'm getting the following error on line 136=> nunchuck:136: error: expected unqualified-id before 'if'

Can someone help me, because I've looked on the forum already and the solutions offered aren't working for me.
« Last Edit: July 02, 2012, 05:25:36 pm by Leewn » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

a) Please put your code inside code tags (the # button when editing).
b) What line are we talking about?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 514
Posts: 31539
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not enough { or } in that last function. The if is outside of any function.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And I'm getting the following error=> nunchuck:136: error: expected unqualified-id before 'if'
Can someone help me, because I've looked on the forum already and the solutions offered aren't working for me.
This bit of code is outside any function:
Code:
if(endValue != 0) {
    if (endValue != (sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)){
    //--- licht aan
    }
  }

endValue = sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2));
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(endValue != 0) {
    if (endValue != (sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)))) {
    //--- licht aan
    }
  }

This is not in a function.

endValue is not declared anywhere.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, you do this at least three times

Code:
sqrt(pow(accel_x_axis,2)+pow(accel_y_axis,2)+pow(accel_z_axis,2)

Do it once, save the value in a variable and use that from there on.

Code:
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);

You've got the right idea but PORTC3 is only used in this one place, no need for a #define and in fact it makes the code less clear.

______
Rob
« Last Edit: July 02, 2012, 05:38:15 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: