Newbe's Questions

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Post Reply
Message
Author
jedeen
Posts: 527
Joined: Fri Jan 13, 2017 5:41 pm

Newbe's Questions

#1 Post by jedeen »

Hi,
I am a complete newbe when it comes to arduino and AM3.3 :D
I have tried to get the example from Ralph (on board led on pin 13) with no success
When I paste the code into my script and run it I get this:
ERROR - [string "logic.lua"]:27: Argument 'hw_id(1)' in function 'hw_led_add' is nil, which is not allowed
ERROR - [string "logic.lua"]:31: Argument 'id(1)' in function 'hw_led_set' is nil, which is not allowed
This is the code:

Code: Select all

my_led = hw_led_add(ARDUINO_MEGA2560_A_D13, 1.0)

function new_data(brightness)
	hw_led_set(my_led, brightness)
end
xpl_dataref_subscribe("sim/cockpit/radios/transponder_brightness", "FLOAT", new_data)	--brightness
When I try to connect a button to the arduino and Am3.3 I get this:
ERROR - [string "logic.lua"]:47: Argument 'hw_id(1)' in function 'hw_button_add' is nil, which is not allowed
This is the code:

Code: Select all

function button_pressed()
  print("button pressed")
end

--function button_released()
  --print("button released")
--end

hw_button_add(ARDUINO_MEGA2560_A_D9, button_pressed)
The arduino is hooked up to AM as shown in the picture below
ArduinoConnected.jpg
And now a not newbe question.
Is it not necessary to tell the arduino what is on pin 9 and that pin 9 in for INPUT and that I want to use the on board pull-up :?: :?: :?:

Have fun
Jedeen

User avatar
Corjan
Posts: 2941
Joined: Thu Nov 19, 2015 9:04 am

Re: Newbe's Questions

#2 Post by Corjan »

Hey,


Make sure to use quotes ("") for the hw_id.

So try this instead:

Code: Select all

my_led = hw_led_add("ARDUINO_MEGA2560_A_D13", 1.0)
Corjan

User avatar
Corjan
Posts: 2941
Joined: Thu Nov 19, 2015 9:04 am

Re: Newbe's Questions

#3 Post by Corjan »

Where did you get the example from? It is wrong there as well.

Corjan

jedeen
Posts: 527
Joined: Fri Jan 13, 2017 5:41 pm

Re: Newbe's Questions

#4 Post by jedeen »

Sorry Corjan,
I just wanted to delete the post, because I found the mistake. It was completely mine :oops:
I found the example here:
https://siminnovations.com/wiki/index.p ... spberry_Pi

But over there is all well to.......AND.......I have learned something.
One has to do nothing in the Arduino environment. ;)

Have fun
Jedeen

jedeen
Posts: 527
Joined: Fri Jan 13, 2017 5:41 pm

Re: Newbe's Questions

#5 Post by jedeen »

An other question from newbe...
I have setup a simple test rig .....a pushbutton and a led on my mega.
I send a message to AM when the button is pushed down....works fine.
Message in AM is also received.......but why so many times :?: :?: :?: :?: :?:
Button is only pushed once.....I have nothing done to prevent bouncing.
Is this bouncing :?: :?: :?: :?: :?: :?:
See attached screenshot

Have fun
Jedeen
TekstViaMessagePort.jpg

User avatar
Corjan
Posts: 2941
Joined: Thu Nov 19, 2015 9:04 am

Re: Newbe's Questions

#6 Post by Corjan »

Could you share your Arduino Sketch?

Corjan

jedeen
Posts: 527
Joined: Fri Jan 13, 2017 5:41 pm

Re: Newbe's Questions

#7 Post by jedeen »

Sure, but it is very very basic and I know there is also an other way to achieve the same, but I just want to test MessagePort.

This is the arduino part:

Code: Select all

#include <si_message_port.hpp>
#include <si_message_port.h>

// constants
const int buttonPin = 2;     // the number for the pushbutton pin
const int ledPin =  3;      // the number for the LED pin

// variables
int knopStatus = 0;         // variable for reading the pushbutton status

SiMessagePort* messagePort;

static void new_message_callback(uint16_t message_id, struct SiMessagePortPayload* payload) 
{
  // Do something with a message from Air Manager or Air Player

  // The arguments are only valid within this function!
  // Make a clone if you want to store it

  if (payload == NULL) 
  {
    messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received without payload");
  }
  else 
  {
    switch(payload->type) 
    {
      case SI_MESSAGE_PORT_DATA_TYPE_BYTE:
        messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " bytes: " + payload->data_byte[0]);
        break;
      case SI_MESSAGE_PORT_DATA_TYPE_STRING:
        messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received string: " + payload->data_string);
        break;
      case SI_MESSAGE_PORT_DATA_TYPE_INTEGER:
        messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " integers" + payload->data_int[0]);
        break;
      case SI_MESSAGE_PORT_DATA_TYPE_FLOAT:
        messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " floats" + payload->data_float[0]);
        break;
    }
  }
}

void setup() 
{
  // Init library on channel A and Arduino type MEGA 2560
  messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_MEGA_2560, SI_MESSAGE_PORT_CHANNEL_A, new_message_callback);

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input with internal pullup:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Make sure this function is called regularly
  messagePort->Tick();

  // read the state of the pushbutton value:
  knopStatus = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  if (knopStatus == HIGH) 
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  } else 
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    messagePort->SendMessage(777, "hello");
  }
}
And this is the Lua part:

Code: Select all

-- This function will be called when a message is received from the Arduino.
function new_message(id, payload)
  -- Do something with the message from the Arduino
print("received new message with id " .. id .. " and payload saying " .. payload)
end

id = hw_message_port_add("ARDUINO_MEGA2560_A", new_message)
Have fun
Jedeen

User avatar
Corjan
Posts: 2941
Joined: Thu Nov 19, 2015 9:04 am

Re: Newbe's Questions

#8 Post by Corjan »

Hi,


Note that the 'loop' function loops :) So it is continuously being called.

When the pin is LOW, the SendMessage function will also keep being called.
That is why you receive many messages.


I think you should only send a message when the pin state changes,

Corjan

jedeen
Posts: 527
Joined: Fri Jan 13, 2017 5:41 pm

Re: Newbe's Questions

#9 Post by jedeen »

Good idea....will do that :D
Thnkx Corjan

Have fun
Jedeen

Post Reply