Pages: [1]   Go Down
Author Topic: error: expected initializer before 'void'  (Read 3070 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
Arduino Mega rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im having some trouble, im getting this error. "error: expected initializer before 'void'" and if i move stuff around it changes where the error occurs, never the same line, currently its #define lazor 14. please help.
Code:
#include <Wire.h>
#include <string.h>
#include <utility\twi.h>
#undef int
#include <stdio.h>
#include <Servo.h>


// adapt to your hardware config
#define POWER_VIA_PORT_C2_C3 1    // use port pins port C2 and C3 as power supply of the Nunchuck (direct plug using wiichuck adapter)

#define USE_NEW_WAY_INIT 1        // use "The New Way" of initialization <http://wiibrew.org/wiki/Wiimote#The_New_Way>
#define WII_IDENT_LEN ((byte)6)
#define WII_TELEGRAM_LEN ((byte)6)
#define WII_NUNCHUCK_TWI_ADR ((byte)0x52)

#define HIGH true
#define LOW false

#define ledPin 12
#define debugled 13
#define button 11
#define lazor 14
#define drive_motor 9
#define steer_servo 10
#define X_servo 7
#define Y_servo 8

uint8_t outbuf[WII_TELEGRAM_LEN];            // array to store arduino output

long previousMillis = 0;
long previousWIIupdate = 0;
long previousDEBUG = 0;

int cnt = 0;
int x = 0, y = 0;
int joy_x_axis = 0;
int joy_y_axis = 0;
int accel_x_axis = 0;
int accel_y_axis = 0;
int accel_z_axis = 0;
int z_button = 0;
int c_button = 0;

bool no_com = false;
bool debug = false;
bool locked = false;
bool accel_state = false;

Servo x_servo;
Servo y_servo;
Servo steer;
Servo drive

void setup ()
{
  pinMode(button, INPUT);
  pinMode(debugled,OUTPUT);
  pinMode(lazor,OUTPUT);
  pinMode(ledPin, OUTPUT);

  delay(100);

  if (digitalRead(button) == HIGH)
  {
    // turn green LED on:    
    digitalWrite(debugled, HIGH);  

    debug = true;

    Serial.begin (57600);
  }

#ifdef POWER_VIA_PORT_C2_C3    // power supply of the Nunchuck via port C2 and C3
  PORTC &=~ _BV(PORTC2);
  PORTC |=  _BV(PORTC3);
  DDRC |= _BV(PORTC2) | _BV(PORTC3);  // make outputs
  delay(100);  // wait for things to stabilize
#endif

  Wire.begin(); // initialize i2c
  // we need to switch the TWI speed, because the nunchuck uses Fast-TWI
  // normally set in hardware\libraries\Wire\utility\twi.c twi_init()
  // this is the way of doing it without modifying the original files
#define TWI_FREQ_NUNCHUCK 400000L
  TWBR = ((CPU_FREQ / TWI_FREQ_NUNCHUCK) - 16) / 2;

  nunchuck_init(0); // send the initialization handshake

  // display the identification bytes, must be "00 00 A4 20 00 00" for the Nunchuck
  byte i;
  if(readControllerIdent(outbuf) == 0)
  {
    if (debug)
      Serial.print("Ident=");
    for (i = 0; i < WII_TELEGRAM_LEN; i++)
    {
      if (debug)
      {
        Serial.print(outbuf[i], HEX);
        Serial.print(' ');
      }
    }
    if (debug)
      Serial.println();
  }

  steer.attach(steer_servo);
  drive.attach(drive_motor);
  x_servo.attach(X_servo);
  y_servo.attach(Y_servo);

  if (debug)
    Serial.println("Finished setup");
}

// params:
//   timeout: abort when timeout (in ms) expires, 0 for unlimited timeout
//   return:  0 == ok, 1 == timeout
byte nunchuck_init (unsigned short timeout)
{
  byte rc = 1;

#ifndef USE_NEW_WAY_INIT
  // look at <http://wiibrew.org/wiki/Wiimote#The_Old_Way> at "The Old Way"
  Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);      // transmit to device 0x52
  Wire.send (0x40);            // sends memory address
  Wire.send (0x00);            // sends sent a zero.  
  Wire.endTransmission ();      // stop transmitting
#else
  // disable encryption
  // look at <http://wiibrew.org/wiki/Wiimote#The_New_Way> at "The New Way"

  unsigned long time = millis();
  do
  {
    Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);      // transmit to device 0x52
    Wire.send (0xF0);            // sends memory address
    Wire.send (0x55);            // sends data.  
    if(Wire.endTransmission() == 0) // stop transmitting
    {
      Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);      // transmit to device 0x52
      Wire.send (0xFB);            // sends memory address
      Wire.send (0x00);            // sends sent a zero.  
      if(Wire.endTransmission () == 0)      // stop transmitting
      {
        rc = 0;
      }
    }
  }
  while (rc != 0 && (!timeout || ((millis() - time) < timeout)));
#endif

  return rc;
}


// params:
//   ident [out]: pointer to buffer where 6 bytes of identification is stored. Buffer must be at least 6 bytes long.
//                A list of possible identifications can be found here: <http://wiibrew.org/wiki/Wiimote#The_New_Way>
//   return:  0 == ok, 1 == error
byte readControllerIdent(byte* pIdent)
{
  byte rc = 1;

  // read identification
  Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);      // transmit to device 0x52
  Wire.send (0xFA);            // sends memory address of ident in controller
  if(Wire.endTransmission () == 0)      // stop transmitting
  {
    byte i;
    Wire.requestFrom (WII_NUNCHUCK_TWI_ADR, WII_TELEGRAM_LEN);      // request data from nunchuck
    for (i = 0; (i < WII_TELEGRAM_LEN) && Wire.available (); i++)
    {
      pIdent[i] = Wire.receive();      // receive byte as an integer
    }
    if(i == WII_TELEGRAM_LEN)
    {
      rc = 0;
    }
  }
  return rc;
}

void clearTwiInputBuffer(void)
{
  // clear the receive buffer from any partial data
  while( Wire.available ())
    Wire.receive ();
}


void send_zero ()
{
  // I don't know why, but it only works correct when doing this exactly 3 times
  // otherwise only each 3rd call reads data from the controller (cnt will be 0 the other times)
  for(byte i = 0; i < 3; i++)
  {
    Wire.beginTransmission (WII_NUNCHUCK_TWI_ADR);      // transmit to device 0x52
    Wire.send (0x00);            // sends one byte
    Wire.endTransmission ();      // stop transmitting
    delay(1);
  }
}

void loop ()
{
  if (digitalRead(button) && ((millis() - previousMillis) > 1000))
  {
    accel_state = !accel_state;
    previousMillis = millis();
  }
  if (no_com)
  {
    if ((millis() - previousDEBUG) > 500)
    {
      previousDEBUG = millis();

      debug = !debug;
      digitalWrite (debugled, debug);
    }
  }

  //if ((millis() - previousWIIupdate) > 90)
  //{previousWIIupdate = millis();
  delay (90);
  send_zero (); // send the request for next bytes
  Wire.requestFrom (WII_NUNCHUCK_TWI_ADR, WII_TELEGRAM_LEN);      // request data from nunchuck

  for (cnt = 0; (cnt < WII_TELEGRAM_LEN) && Wire.available (); cnt++)
  {
    outbuf[cnt] = nunchuk_decode_byte (Wire.receive ());      // receive byte as an integer
  }



  clearTwiInputBuffer();

  // If we recieved the 6 bytes, then go print them
  if (cnt >= WII_TELEGRAM_LEN)
  {
    print ();
  }
  //}

  /*if ((outbuf[5] >> 0) & 1)
   digitalWrite(lazor,LOW);
   else
   {
   digitalWrite(lazor,HIGH);
  
   if ((outbuf[5] >> 1) & 1){
   }
  
   else if (no_com)
   locked = true;
   else
   locked = !locked;
   }*/
  /*if (accel_state)
   {
   if (accel_y_axis < 475)
   {
   LF_motor.run(BACKWARD);
   RT_motor.run(BACKWARD);
  
   LF_motor.setSpeed(map(accel_y_axis,475,313,85,255));
   RT_motor.setSpeed(map(accel_y_axis,475,313,85,255));
   }
   else if (accel_y_axis > 525)
   {
   LF_motor.run(FORWARD);
   RT_motor.run(FORWARD);
  
   LF_motor.setSpeed(map(accel_y_axis,525,720,85,255));
   RT_motor.setSpeed(map(accel_y_axis,525,720,85,255));
   }
   else
   {
   LF_motor.setSpeed(0);
   RT_motor.setSpeed(0);
   }
   }
   else
   {*/
  //if (c_button)
  //{
  digitalWrite (ledPin, !c_button);

  drive.write(map(joy_y_axis,6,247,0,180);

  //analogWrite(drive_motor,joy_y_axis);

  steer.write(map(joy_x_axis,8,247,15,115));
  //}
  /*else
   {
   x_servo.write(map(joy_x_axis,0,255,0,180));
   y_servo.write(map(joy_y_axis,0,255,30,180));
  
   analogWrite (drive_motor, 127);
  
   //Light is good if on
   digitalWrite (ledPin, HIGH);
   }*/
  //}
}

// 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 print ()
{
  joy_x_axis = outbuf[0];
  joy_y_axis = outbuf[1];
  accel_x_axis = outbuf[2] * 2 * 2;
  accel_y_axis = outbuf[3] * 2 * 2;
  accel_z_axis = outbuf[4] * 2 * 2;

  z_button = 0;
  c_button = 0;

  if (joy_y_axis == 127)
    joy_y_axis += 1;


  // byte outbuf[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 ((outbuf[5] >> 0) & 1)
  {
    z_button = 1;
  }
  if ((outbuf[5] >> 1) & 1)
  {
    c_button = 1;
  }

  if ((outbuf[5] >> 2) & 1)
  {
    accel_x_axis += 2;
  }
  if ((outbuf[5] >> 3) & 1)
  {
    accel_x_axis += 1;
  }

  if ((outbuf[5] >> 4) & 1)
  {
    accel_y_axis += 2;
  }
  if ((outbuf[5] >> 5) & 1)
  {
    accel_y_axis += 1;
  }

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

  if (debug)
  {
    Serial.print (joy_x_axis, DEC);
    Serial.print ("\t");

    Serial.print (joy_y_axis, DEC);
    Serial.print ("\t");

    Serial.print (accel_x_axis, DEC);
    Serial.print ("\t");

    Serial.print (accel_y_axis, DEC);
    Serial.print ("\t");

    Serial.print (accel_z_axis, DEC);
    Serial.print ("\t");

    Serial.print (z_button, DEC);
    Serial.print ("\t");

    Serial.print (c_button, DEC);
    Serial.print ("\t");

    Serial.print ("\r\n");
  }

  if (((joy_x_axis == 128) && (joy_y_axis == 128) && (accel_x_axis == 509) && (accel_y_axis == 511) && (accel_z_axis == 661) && (z_button == 1) && (c_button == 1)) || ((joy_x_axis == 255) && (joy_y_axis == 255) && (accel_x_axis >= 1023) && (accel_y_axis >= 1023) && (accel_z_axis >= 1023) && (z_button == 1) && (c_button == 1)))
    no_com = true;
  else
    no_com  = false;
}
« Last Edit: April 26, 2010, 06:54:07 am by Raveious » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
Arduino Mega rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(add this to the end, its to long to fit in the first post)

using 0018

Code:
// Decode data format that original Nunchuck uses with old init sequence. This never worked with
// other controllers (e.g. wireless Nunchuck from other vendors)
char nunchuk_decode_byte (char x)
{
#ifndef USE_NEW_WAY_INIT
  x = (x ^ 0x17) + 0x17;
#endif
  return x;
}
« Last Edit: April 25, 2010, 09:16:21 pm by Raveious » Logged

Wuppertal/Germany
Offline Offline
God Member
*****
Karma: 1
Posts: 895
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

there is no need to include this header:
Code:
#include <utility\twi.h>
its meant for internal use only by the Wire-lib

What is that good for?
Code:
#undef int

Eberhard


Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 325
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Servo x_servo;
Servo y_servo;
Servo steer;
Servo drive //<----- put a semicolon after Servo drive

void setup ()
{
  pinMode(button, INPUT);
  pinMode(debugled,OUTPUT);
  pinMode(lazor,OUTPUT);
  pinMode(ledPin, OUTPUT);
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
Arduino Mega rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lol, i cant believe i missed that smiley-lol
thanks
Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 325
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That "expected initializer before" something almost always is caused by a missing semicolon usually immediately above the 'something'.

Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
Arduino Mega rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, thanks
Logged

Pages: [1]   Go Up
Jump to: