Won't work without Serial.print

Hello,

I am at the beginning stages of using the wiinunchuk.h library (found here) and already there are a couple of things puzzling me. Below is a stripped down version of my sketch. At the moment I am concentrating on getting the button communications set up.

//bring in the libraries
#include <Wire.h>
#include <wiinunchuck.h>

int LED = 5; //pin for LED

void setup() {
  pinMode(5, OUTPUT);

  Serial.begin(19200);
  nunchuk_setpowerpins();
  nunchuk_init();
  nunchuk_calibrate_joy;
}

void loop() {
  nunchuk_get_data();

if (nunchuk_zbutton() == HIGH){
  digitalWrite(LED, HIGH);
}
else{
  digitalWrite(LED, LOW);
}

  Serial.println("this is nonsense"); // needs this to work!
}

The first curious thing is that the remote will not communicate with the Arduino without having something sent and written by the "Serial.print" command. It is completely unresponsive without this line of code. Does anyone know why this is? Not really a problem as such, but I don't like having things like this if I don't know their true purpose.

Secondly, when the arduino is reset or starts up, the output is set to high for a very short amount of time (a flicker). I really don't like this as I can't have the arduino sending voltages when it's not supposed to as this will be critical later down the road - I am only using an LED to test it. Again, if anyone knows why this is or how to combat it I would be very grateful.

It might be worth noting that without

else{
  digitalWrite(LED, LOW);
}

the LED is high from the start without the button being pressed. I have tried setting the output to low in the setup, but the flicker still occurs.

Thanks

Please post a link to the library that you use.

To prevent the very short pulse on the output, first write a LOW to the pin before changing the mode

digitalWrite(5, LOW);
pinMode(5, OUTPUT);

PS
Ain't you missing something on this line? Like Round brackets?

  nunchuk_calibrate_joy;

The flickering and odd behaviour may be due to the way you have wired your button. Best is usually wiring a button between the arduino pin and ground, and using the in-built pullup resistor on the pin. This means the pin is "LOW" when the button is pressed.

Library is here: http://www.timteatro.net/2012/02/10/a-library-for-using-the-wii-nunchuk-in-arduino-sketches/
and here GitHub - timtro/wiinunchuck-h: A Library for Using the Wii Nunchuk In Arduino Sketches

To prevent the very short pulse on the output, first write a LOW to the pin before changing the mode

thanks, but this does not seem to help.

Ain't you missing something on this line? Like Round brackets?

this is calling the calibration function in the library for the joystick, not really relevant at the moment but thanks for the heads up.

The flickering and odd behaviour may be due to the way you have wired your button. Best is usually wiring a button between the arduino pin and ground, and using the in-built pullup resistor on the pin. This means the pin is "LOW" when the button is pressed.

I am not sure how the button is wired as the remote communicates via the SCL & SDA pins and the library does the rest. What I do know is that the pressed button originally gives a LOW output and the library flips this to a HIGH & vice versa so a pressed button gives a HIGH output.

A pull down resistor in the circuit does not work either.

WestyTea:
this is calling the calibration function in the library for the joystick

I doubt it's really doing that if you don't have the round braces at the end.

sterretje:
I doubt it's really doing that if you don't have the round braces at the end.

yes you're right, but as I said it's not relevant to any part of the sketch - I shouldn't have included it in the stripped down version as it's a distraction to the current problem.

Still, thanks for the input.

WestyTea:
yes you're right, but as I said it's not relevant to any part of the sketch - I shouldn't have included it in the stripped down version as it's a distraction to the current problem.

Still, thanks for the input.

So even without that line (or with it corrected), you still need that Serial.println statement in the code?

yes that's correct

update -

the initial high voltage outputting on pin5 comes from the reading of button state in these lines of code:

if (nunchuk_zbutton() == HIGH){
  digitalWrite(LED, HIGH);
}
else{
  digitalWrite(LED, LOW);
}

However, I do not understand C enough to understand the code in the library.

According to the library file, it uses two Arduino pins for power. I find that strange; why not just 5V and GND?

Anyway, maybe you need to give it some more time to get stabilized. Additional delays between each nunchuck step in setup() and one before leaving the setup().

void setup() {
  pinMode(5, OUTPUT);

  Serial.begin(19200);
  nunchuk_setpowerpins();
  delay(1000);
  nunchuk_init();
  delay(1000);
  nunchuk_calibrate_joy();
  delay(1000);
}

sterretje:
According to the library file, it uses two Arduino pins for power. I find that strange; why not just 5V and GND?

that's because of the adaptor that can be used with the remote (link). They expect you to plug it straight into the arduino's analog pins.

sterretje:
Anyway, maybe you need to give it some more time to get stabilized. Additional delays between each nunchuck step in setup() and one before leaving the setup().

Noted, tried this - it doesn't change anything but will leave it in for good practice.

Okay - half solved!

I should have included the line

  nunchuk_send_request();

in the setup()

This gets rid of my problem of the flickering and prevents the LEDs from being set to high from the start. Unfortunately I couldn't find many example sketches using this particular library so there isn't a whole lot to reference (plenty of the older more complicated libraries).

Anyhoo, the Serial.print thing is still required. Very curious, but as it doesn't actually harm anything I suppose I can live with it.

Cheers