Psx_analog won't compile

Hi everibody, I bought a cheap PS2 compatibile controller in order to use it as an input device for the Arduino. I downloaded the AnalogPSXLibrary(Arduino Playground - AnalogPSXLibrary) from the playground but I couldn't manage to compile it trying lo load an example sketch.

Here are the errors I got:

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/stdlib.h:111: error: expected `)' before 'int'

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/stdlib.h:111: error: expected `)' before 'int'

I tried both arduino 016 and 017 with the same result. I took a look at the .cpp and .h files and seems ok to me.

Thanks in advance!

Hi,

had the same problem and was sure that something odd is going on. After reading the button example I added in the first line

const int ledPin =  13;

The example code psx_analog.pde comprises

#define LEDPin 13

Resulting code which compiles successful is now

const int ledPin =  13; 
#include "Psx_analog.h"                                          // Includes the Psx Library 

#define dataPin 14
#define cmndPin 15
#define attPin 16
#define clockPin 17

#define motorup  5
#define motordown 6
#define motorleft  9
#define motorright 10

#define center  127

#define LEDPin 13

Psx Psx;                                                  // Initializes the library

void setup()
{
  Psx.setupPins(dataPin, cmndPin, attPin, clockPin);  // Defines what each pin is used
                                                      // (Data Pin #, Cmnd Pin #, Att Pin #, Clk Pin #)
                                                       
  Psx.initcontroller(psxAnalog);    
  
  pinMode(LEDPin, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
  
  //setup motoroutputs 
    pinMode(motorup, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
    pinMode(motordown, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
    pinMode(motorleft, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
    pinMode(motorright, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
  
  
  Serial.begin(9600); 
  Serial.println("Raw controller values"); 

  
    // wait for the long string to be sent 
  delay(100); 
}

void loop()
{
  Psx.poll();                                      // Psx.read() initiates the PSX controller and returns
  Serial.print("\n");                                            // the button data
  Serial.print(Psx.Controller_mode, HEX);     // prints value as string in hexadecimal (base 16) 
  Serial.print(Psx.digital_buttons, HEX);     // prints value as string in hexadecimal (base 16)  
  Serial.print(Psx.Right_x, HEX);     // prints value as string in hexadecimal (base 16)    
  Serial.print(Psx.Right_y, HEX);     // prints value as string in hexadecimal (base 16)    
  Serial.print(Psx.Left_x, HEX);     // prints value as string in hexadecimal (base 16)    
  Serial.print(Psx.Left_y, HEX);     // prints value as string in hexadecimal (base 16)      

  if (Psx.digital_buttons & psxR2)                                       // If the data anded with a button's hex value is true,
                                                          // it signifies the button is pressed. Hex values for each
                                                          // button can be found in Psx.h
  {
    digitalWrite(LEDPin, HIGH);                           // If button is pressed, turn on the LED
  }
  else
  {
    digitalWrite(LEDPin, LOW);                            // If the button isn't pressed, turn off the LED
  }
  
  delay(100); 

}

I don't understand the difference since I'm a beginner with ardunio. Could someone help me out with this?

Bye,

Bencao74

C is case sensitive so your definition of LEDPin has to match its use exactly. A #define can be thought of as a "search and replace at compile time". Given the following everywhere foo appears (below the define) it will be replaced with 10 by the preprocessor (which runs just before, and part of, the compile of your code). I noticed that the Arduino docs said to stay away from #defines - why? IMO they are better as they don't use any memory (search/replace).

Your code
#define foo 10
...
int var = foo;

Gets turned into this
int var = 10;

Furthermore you should get rid of "const int ledPin = 13;" as it is not being used and just wasting precious memory.

Greg

Perhaps this is confusing because the addition of a const at the top fixed the compile error, but put that down to the way the Arduino build environment massages source files before they are compiled.

A solution to the compile problem is to replace
#include "WConstants.h"
In Psz_analog.h with this:
#include "WProgram.h"

You can use either the #define or the const int to define a constant in your sketch, but not both (for the same constant). and as stated above, it must match the case used later in your code

Furthermore you should get rid of "const int ledPin = 13;" as it is not being used and just wasting precious memory.

Greg, const int will not use any more memory than a #define, even if it was being used.

Why is that? In my understanding a #define is akin to a preprocessor search and replace (no extra mem used) and a global const int actually takes up however much memory the AVR defines as an int.

Please explain!

And also, any idea why the Arduino docs say not to use #define? This is pretty standard practice in my experience.

Thanks!
Greg

The compiler treats const int as a constant and optimizes it away away so no RAM is used.
Here is the memory dump ( using avr-objdump) from three versions of the blink example sketch. The only difference is in the way the port is defined.

Values are hex:

Data text bss style used
#define 0 378 9 #define pin 13
const int 0 378 9 const int pin=13;
int 2 37e 9 int pin = 13;

As you can see, the int version uses two bytes of RAM and a few extra bytes of code to load the variable. the const int version has an identical footprint to #define. const int is better because the compiler can do type checking.

Arduino examples have used int instead of const int perhaps because it is thought that the former is easier for non-technical people. I do see more examples appearing that use const int and hope this will trend will continue (developing one or two good habits early on is no bad thing)

this is the sketch used, compiled three times with the three variations

//const int ledPin = 13;
//int ledPin =  13;    // LED connected to digital pin 13
#define ledPin 13

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);     
}

void loop()                     
{
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  del

Ahhh, thanks for providing this tip/trick! Good to know!

Greg

@mem :

And thanks from me for this great and very informative posts!!

Bye,

bencao74

I agree, great post Mem, helped me a lot :slight_smile: So what happens if you do a float instead of an int? Same thing?

const float f =3.14 will be exactly the same as #define f 3.14
both of these will use 4 bytes less RAM and 8 bytes less flash then a normal float in a sketch like this:

float f = 3.14;  // this uses 4 bytes more RAM 
//const float f = 3.14; 
//#define f 3.14

void setup() { 
  Serial.begin(9600); 
  Serial.print(f);    
} 

void loop() {
}

Bencao74,
I tried your code and I didn´t get response, nothing happens.
May be I had missed something, do I need to use ACKNOLEDGE in what arduinos pin?
I will be glad for your answer.