Expected primary-expression before '>' token

Hi, I’m waiting for a GPS module to arrive for an autonomous RC car I am trying to make. While I am waiting I’m creating a program to test and get any non-GPS related bugs out early. I’m using a Wii Nunchuck for the controller in the test program (because I didn’t want to have to get timings of turn commands etc right)… anyway. When I try to use > or < a number as a case in a ‘switch’ statement I get the error “Expected primary-expression before ’ ’ token”.

Full error message below (I’m using Windows 10 unlike the message says)

Arduino: 1.6.4 (Windows 8.1), Board: “Arduino Uno”

WiichuckRC.ino: In function ‘void loop()’:
WiichuckRC:59: error: expected primary-expression before ‘>’ token
WiichuckRC:73: error: expected primary-expression before ‘<’ token
WiichuckRC:78: error: expected primary-expression before ‘>’ token
expected primary-expression before ‘>’ token

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

Main code below

/*
 * WiiChuckDemo -- 
 *
 * 2008 Tod E. Kurt, http://thingm.com/
 *
 */
 
 
 // Source of Wii Nunchuck code above
 
 // Controls- accelerometer Y axis = accelerator(85(straight up) = 0), joystick X axis = turning, C button engaged = reverse, Z button = forward

#include <Wire.h>
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte joyx,accy,zbut,cbut;

int spd;
int motorAenable = 6;
int motorBenable = 8;
int motorAP = 4;
int motorAN = 7;
int motorBP = 3;
int motorBN = 2;


void setup()
{
    nunchuck_setpowerpins();
    nunchuck_init(); // send the initilization handshake
}

void loop()
{
    spd = map(accy, 85, 173, 0, 255);
    
    
  
    if( loop_cnt > 100 ) { // every 100 msecs get new data
        loop_cnt = 0;

        nunchuck_get_data();

        joyx  = nunchuck_joyx(); // ranges from approx 70 - 182
        accy  = nunchuck_accely(); // ranges from approx 65 - 173
        zbut = nunchuck_zbutton();
        cbut = nunchuck_cbutton(); 
    }
    loop_cnt++;
    delay(1);
    
    
    if(zbut == 1)
    {
    switch(spd)
    {
      case > 0:
      analogWrite(motorAenable, spd);
      break;
      default:
      analogWrite(motorAenable, 0);
      break;
    }
    }
    else
    {
      analogWrite(motorAenable, 0);
    }
    
    switch (joyx) {
     case < 110:
      analogWrite(motorBenable, 255);
      digitalWrite(motorBN, HIGH);
      digitalWrite(motorBP, LOW);
       break;
     case > 130:
      analogWrite(motorBenable, 255);
      digitalWrite(motorBN, LOW);
      digitalWrite(motorBP, HIGH);
       break;
     default: 
      analogWrite(motorBenable, 0);
      digitalWrite(motorBN, LOW);
      digitalWrite(motorBP, LOW);
     break;
   }
    
    switch (cbut) {
     case 1:
      digitalWrite(motorAP, LOW);
      digitalWrite(motorAN, HIGH);
       break;
     default: 
     digitalWrite(motorAP, HIGH);
     digitalWrite(motorAN, LOW);
     break;
    }
}

nunchuck_funcs.h code below

/*
 * Nunchuck functions  -- Talk to a Wii Nunchuck
 *
 * This library is from the Bionic Arduino course : 
 *                          http://todbot.com/blog/bionicarduino/
 *
 * 2007-11 Tod E. Kurt, http://todbot.com/blog/
 *
 * The Wii Nunchuck reading code originally from Windmeadow Labs
 *   http://www.windmeadow.com/node/42
 */

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
//#define Wire.write(x) Wire.send(x)
//#define Wire.read() Wire.receive()
#endif



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
static void nunchuck_init()
{ 
    Wire.begin();                // join i2c bus as master
    Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
    Wire.write((uint8_t)0x40);// sends memory address
    Wire.write((uint8_t)0x00);// sends sent a zero.  
#else
    Wire.send((uint8_t)0x40);// sends memory address
    Wire.send((uint8_t)0x00);// sends sent a zero.  
#endif
    Wire.endTransmission();// stop transmitting
}

// Send a request for data to the nunchuck
// was "send_zero()"
static void nunchuck_send_request()
{
    Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
    Wire.write((uint8_t)0x00);// sends one byte
#else
    Wire.send((uint8_t)0x00);// sends one byte
#endif
    Wire.endTransmission();// stop transmitting
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
static char nunchuk_decode_byte (char x)
{
    x = (x ^ 0x17) + 0x17;
    return x;
}

// Receive data back from the nunchuck, 
// returns 1 on successful read. returns 0 on failure
static int nunchuck_get_data()
{
    int cnt=0;
    Wire.requestFrom (0x52, 6);// request data from nunchuck
    while (Wire.available ()) {
        // receive byte as an integer
#if (ARDUINO >= 100)
        nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.read() );
#else
        nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.receive() );
#endif
        cnt++;
    }
    nunchuck_send_request();  // send 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
static 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 += 1;
    if ((nunchuck_buf[5] >> 3) & 1)
        accel_x_axis += 2;

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

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

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

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

    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("but:");
    Serial.print(z_button, DEC);
    Serial.print(",");
    Serial.print(c_button, DEC);

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

// returns zbutton state: 1=pressed, 0=notpressed
static int nunchuck_zbutton()
{
    return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1;  // voodoo
}

// returns zbutton state: 1=pressed, 0=notpressed
static int nunchuck_cbutton()
{
    return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1;  // voodoo
}

// returns value of x-axis joystick
static int nunchuck_joyx()
{
    return nunchuck_buf[0]; 
}

// returns value of y-axis joystick
static int nunchuck_joyy()
{
    return nunchuck_buf[1];
}

// returns value of x-axis accelerometer
static int nunchuck_accelx()
{
    return nunchuck_buf[2];   // FIXME: this leaves out 2-bits of the data
}

// returns value of y-axis accelerometer
static int nunchuck_accely()
{
    return nunchuck_buf[3];   // FIXME: this leaves out 2-bits of the data
}

// returns value of z-axis accelerometer
static int nunchuck_accelz()
{
    return nunchuck_buf[4];   // FIXME: this leaves out 2-bits of the data
}

Any help appreciated. Thanks

The value of a case label needs to be a number literal or a constant number; and it needs to be an int as well. What you're trying to do is not supported in C or C++; it is just wishful thinking ...

kind regards,

Jos

Thankyou

If anyone else wants a way around this problem. I made a filter to put before the ‘switch’ statement that fixes the problem.

if(joyx < 100)
        {
          joyx = 1;
        }
        if(joyx > 130)
        {
          joyx = 2;
        }