Lego Powerfunctions

Hi,

nun möchte ich endlich mal mit den Lego Power Functions anfangen.
Dazu habe ich mir die PFTransmitter.h Library installiert und das Beispielsketch geladen:

#include <Arduino.h>

#include <PFTransmitter.h>

namespace
{
  // Digital
  const int pinIrLed                    = 8;
  const int pinButtonNorth              = 4;
  const int pinButtonSouth              = 5;
  const int pinButtonEast               = 3;
  const int pinButtonWest               = 6;
  const int pinButtonJoystick           = 2;
  const int pinInverseJoystickX         = 12;
  const int pinInverseJoystickY         = 11;
  const int pinInverseButtonsEastWest   = 10;
  const int pinInverseButtonsNorthSouth = 9;

  // Analog
  const int pinJoystickX = 0;
  const int pinJoystickY = 1;

  const int xLeft  = 0;
  const int xRight = 1023;
  const int yDown  = 1023;
  const int yUp    = 0;

  int xMid = 0;
  int yMid = 0;

  PF_n::transmitter_c transmitter( pinIrLed );

  /*
    Get joystick values and send message
  */
  void readJoystickComboPWM( PF_n::transmitter_c::channel_t channel, int pinX, int pinY, int xLeft, int xMid, int xRight,
                             int yUp, int yMid, int yDown )
  {
    const int valX = analogRead( pinX );
    const int valY = analogRead( pinY );
    PF_n::transmitter_c::pwmOutput_t outputA = PF_n::transmitter_c::PWM_OUTPUT_BRAKE_FLOAT;
    PF_n::transmitter_c::pwmOutput_t outputB = PF_n::transmitter_c::PWM_OUTPUT_BRAKE_FLOAT;

    if ( valX < xMid )
    {
      // Links
      outputA = PF_n::transmitter_c::pwmOutput_t( map( valX, xMid, xLeft,
                                                       PF_n::transmitter_c::PWM_OUTPUT_FLOAT, PF_n::transmitter_c::PWM_OUTPUT_FORWARD_7 ) );
    }
    else
    {
      // Rechts
      outputA = PF_n::transmitter_c::pwmOutput_t( map( valX, xMid, xRight,
                                                       PF_n::transmitter_c::PWM_OUTPUT_BACKWARD_1 + 1,
                                                       PF_n::transmitter_c::PWM_OUTPUT_BACKWARD_7 ) );
    }
    if ( outputA <= PF_n::transmitter_c::PWM_OUTPUT_FLOAT || outputA > PF_n::transmitter_c::PWM_OUTPUT_BACKWARD_1 )
    {
      outputA = PF_n::transmitter_c::PWM_OUTPUT_BRAKE_FLOAT;
    }

    if ( valY < yMid )
    {
      // Vorwärts
      outputB = PF_n::transmitter_c::pwmOutput_t( map( valY, yMid, yUp,
                                                       PF_n::transmitter_c::PWM_OUTPUT_FLOAT, PF_n::transmitter_c::PWM_OUTPUT_FORWARD_7 ) );
    }
    else
    {
      // Rückwärts
      outputB = PF_n::transmitter_c::pwmOutput_t( map( valY, yMid, yDown,
                                                       PF_n::transmitter_c::PWM_OUTPUT_BACKWARD_1 + 1,
                                                       PF_n::transmitter_c::PWM_OUTPUT_BACKWARD_7 ) );
    }
    if ( outputB <=  PF_n::transmitter_c::PWM_OUTPUT_FLOAT || outputB > PF_n::transmitter_c::PWM_OUTPUT_BACKWARD_1 )
    {
      outputB = PF_n::transmitter_c::PWM_OUTPUT_BRAKE_FLOAT;
    }

    const bool inverseJoystickX = digitalRead( pinInverseJoystickX ) == HIGH;
    const bool inverseJoystickY = digitalRead( pinInverseJoystickY ) == HIGH;

    transmitter.setMessageComboPWM( channel, outputA, inverseJoystickX, outputB, inverseJoystickY );
  }

  /*
    Get button values and send message
  */
  void readButtons( PF_n::transmitter_c::channel_t channel, int pinNorth, int pinSouth, int pinEast, int pinWest )
  {
    const int buttonStateNorth = digitalRead( pinNorth );
    const int buttonStateSouth = digitalRead( pinSouth );
    const int buttonStateEast  = digitalRead( pinEast );
    const int buttonStateWest  = digitalRead( pinWest );
    PF_n::transmitter_c::comboDirectOutput_t outputA = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FLOAT;
    PF_n::transmitter_c::comboDirectOutput_t outputB = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FLOAT;

    if ( buttonStateNorth == HIGH && buttonStateSouth == LOW )
    {
      outputA = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FORWARD;
    }
    else if ( buttonStateNorth == LOW && buttonStateSouth == HIGH )
    {
      outputA = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_BACKWARD;
    }

    if ( buttonStateEast == HIGH && buttonStateWest == LOW )
    {
      outputB = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FORWARD;
    }
    else if ( buttonStateEast == LOW && buttonStateWest == HIGH )
    {
      outputB = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_BACKWARD;
    }

    const bool inverseButtonsNorthSouth = digitalRead( pinInverseButtonsNorthSouth ) == HIGH;
    const bool inverseButtonsEastWest   = digitalRead( pinInverseButtonsEastWest )   == HIGH;

    transmitter.setMessageComboDirect( channel, outputA, inverseButtonsNorthSouth, outputB, inverseButtonsEastWest );
  }

  /*
    Get value of joystick-button and send message
  */
  void readJoystickButton( PF_n::transmitter_c::channel_t channel, int pin )
  {
    const int buttonState = digitalRead( pin );
    PF_n::transmitter_c::comboDirectOutput_t outputA = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FLOAT;
    PF_n::transmitter_c::comboDirectOutput_t outputB = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FLOAT;

    if ( buttonState == LOW )
    {
      outputA = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FORWARD;
      outputB = PF_n::transmitter_c::COMBO_DIRECT_OUTPUT_FORWARD;
    }

    transmitter.setMessageComboDirect( channel, outputA, false, outputB, false );
  }
}

void setup()
{
  pinMode( pinButtonJoystick, INPUT );
  digitalWrite( pinButtonJoystick, HIGH );
  
  pinMode( pinButtonEast, INPUT );
  digitalWrite( pinButtonEast, HIGH );

  pinMode( pinButtonNorth, INPUT );
  digitalWrite( pinButtonNorth, HIGH );
  
  pinMode( pinButtonSouth, INPUT );
  digitalWrite( pinButtonSouth, HIGH );

  pinMode( pinButtonWest, INPUT );
  digitalWrite( pinButtonWest, HIGH );

  pinMode( pinInverseJoystickX, INPUT );
  digitalWrite( pinInverseJoystickX, HIGH );

  pinMode( pinInverseJoystickY, INPUT );
  digitalWrite( pinInverseJoystickY, HIGH );

  pinMode( pinInverseButtonsNorthSouth, INPUT );
  digitalWrite( pinInverseButtonsNorthSouth, HIGH );

  pinMode( pinInverseButtonsEastWest, INPUT );
  digitalWrite( pinInverseButtonsEastWest, HIGH );

  pinMode( pinIrLed, OUTPUT );

  xMid = analogRead( pinJoystickX );
  yMid = analogRead( pinJoystickY );
  
  Serial.begin( 9600 );
}

void loop()
{
/*
  Serial.print( analogRead( pinJoystickX ) );       //Read the position of the joysticks X axis and print it on the serial port.
  Serial.print( "," );
  Serial.print( analogRead( pinJoystickY ) );       //Read the position of the joysticks Y axis and print it on the serial port.
  Serial.print( "," );
  Serial.print( digitalRead( pinButtonJoystick ) ); //Read the value of the select button and print it on the serial port.
  Serial.print( digitalRead( pinButtonNorth ) );    //Read the value of the button 1 and print it on the serial port.
  Serial.print( digitalRead( pinButtonSouth ) ) ;   //Read the value of the button 2 and print it on the serial port.
  Serial.print( digitalRead( pinButtonEast ) );     //Read the value of the button 0 and print it on the serial port.
  Serial.println( digitalRead( pinButtonWest ) );   //Read the value of the button 3 and print it on the serial port.
  
  //Wait for 100 ms, then go back to the beginning of 'loop' and repeat.
  delay( 100 );
*/

  readJoystickComboPWM( PF_n::transmitter_c::CHANNEL_1, pinJoystickX, pinJoystickY, xLeft, xMid, xRight, yUp, yMid, yDown );
  readButtons( PF_n::transmitter_c::CHANNEL_2, pinButtonNorth, pinButtonSouth, pinButtonEast, pinButtonWest );
  readJoystickButton( PF_n::transmitter_c::CHANNEL_3, pinButtonJoystick );

  transmitter.sendMessages();
}

Leider kompiliert das Sketch nicht für einen Arduino nano V3.

…(mehr als 9000 Zeichen)…

Hier die ausführliche Fehlermeldung:

Arduino: 1.8.2 (Windows 7), Board: "Arduino Nano, ATmega328"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Chris\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Chris\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Chris\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328 -ide-version=10802 -build-path C:\Users\Chris\AppData\Local\Temp\arduino_build_644552 -warnings=default -build-cache C:\Users\Chris\AppData\Local\Temp\arduino_cache_167813 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose D:\nodeMCU\Sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Chris\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Chris\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Chris\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328 -ide-version=10802 -build-path C:\Users\Chris\AppData\Local\Temp\arduino_build_644552 -warnings=default -build-cache C:\Users\Chris\AppData\Local\Temp\arduino_cache_167813 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose D:\nodeMCU\Sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino
Using board 'nano' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Build-Optionen wurden verändert, alles wird neu kompiliert
Detecting libraries used...
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp" -o "nul"
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp" -o "nul"
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master\PFTransmitter.cpp" -o "nul"
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master\example.cpp" -o "nul"
Generating function prototypes...
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp" -o "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Program Files (x86)\Arduino\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\preproc\ctags_target_for_gcc_minus_e.cpp"
Sketch wird kompiliert...
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os  -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp" -o "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp.o"
Compiling libraries...
Compiling library "PFTransmitter-master"
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os  -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master\PFTransmitter.cpp" -o "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\libraries\PFTransmitter-master\PFTransmitter.cpp.o"
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os  -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10802 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR   "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs" "-IC:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master" "C:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master\example.cpp" -o "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\libraries\PFTransmitter-master\example.cpp.o"
Compiling core...
Using precompiled core
Linking everything together...
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc"  -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p  -o "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552/Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.elf" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp.o" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\libraries\PFTransmitter-master\PFTransmitter.cpp.o" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\libraries\PFTransmitter-master\example.cpp.o" "C:\Users\Chris\AppData\Local\Temp\arduino_build_644552/..\arduino_cache_167813\core\core_arduino_avr_nano_cpu_atmega328_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\Users\Chris\AppData\Local\Temp\arduino_build_644552" -lm
C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\libraries\PFTransmitter-master\example.cpp.o (symbol from plugin): In function `setup':

(.text+0x0): multiple definition of `setup'

C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\libraries\PFTransmitter-master\example.cpp.o (symbol from plugin): In function `setup':

(.text+0x0): multiple definition of `loop'

C:\Users\Chris\AppData\Local\Temp\arduino_build_644552\sketch\Gamecontroller_Chris_LegoPowerFunctions_004originalExample.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Bibliothek PFTransmitter-master im Ordner: C:\Users\Chris\Documents\Arduino\libraries\PFTransmitter-master (legacy) wird verwendet
exit status 1
Fehler beim Kompilieren für das Board Arduino Nano.

Weiss jemand warum?

Danke und liebe Grüße,
Chris

Steht doch dort: Du hast mehrere setup() und mehrere loop()

Gruß Tommy

Das hab ich auch gelesen.
Aber in dem Sketch ist nur ein setup() und eine loop()
wo sollen denn die anderen sein?

Lieben Gruß,
Chris

.....is ja lustig.
Wenn die setup() und eine loop() aus dem Sketch auskommentiere, dann kompiliert das Sketch.

Das heisst irgendwo anders ist noch ein setup() und eine loop() versteckt.
Kann ja nur in der PFTransmitter.h sein. Ich such mal....

themanfrommoon:
wo sollen denn die anderen sein?

Auch das steht in der Fehlermeldung: im Library-Verzeichnis gibt es eine Datei example.cpp, die da nicht hingehört, denn das ist ein kompletter Sketch, der die Lib nutzt ( Die Lib ist ja auch nicht mehr so ganz taufrisch, vielleicht hat das früher so funktioniert und die example.cpp wurde nicht eingebunden ).

TREFFER und VERSENKT!
Das war das Problem, ich hab das Beispiel "example.cpp" jetzt in einen Unterordner "examples" verschoben (so wie das bei den an deren Libraries ist).
...und schon kompiliert es.

Wunderbar, vielen Dank!

Jetzt kann ich mich an die Arbeit machen.

Beste Grüße,
Chris