
I have cleaned up my code from my previous post and want to make it available for anyone to try out themselves.
This code drives two 5v volt meters much like the developers have in their MkII console.
Remember to set “hardware=true” in your game’s config file or else it wont work!
/* Objects in Space Panel Meters
This example displays telemetry from the game on an LCD and 5v panel meters.
It uses the built-in LiquidCrystal library - see
https://www.arduino.cc/en/Reference/LiquidCrystal for details.
DISPLAYS POWER FLOW AND TOTAL POWER AS A PWM ON THE LCD SCREEN AND
PASSES IT TO PWM PINS 9 AND 10 FOR USE ON 5V PANEL METERS.
The LCD is only used to display data for debuging purposes and can be
omitted without affecting the meters.
CODE BY DAK47922 - http://www.wolfpack.xyz/blog
----------------
*/
#include <LiquidCrystal.h>
#include <ArduinosInSpace.h>
int flow; //Declare our variables as integers
int power;
// Initialise the LiquidCrystal library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Create an ObjectsInSpace object. The first parameter is the
// serial interface to use. The second is the number of values
// we're requesting from the game.
ObjectsInSpace OIS(Serial, 2);
void setup()
{
pinMode(9, OUTPUT); // POWER FLOW PWM TO METER
pinMode(10, OUTPUT); // BATTERY PWM TO METER
// set up the LCD's number of columns and rows:
// specific for a 16x2 LCD screen (change this for different sizes)
lcd.begin(16, 2);
Serial.begin(9600); //must be 9600
OIS.begin(); //begin the handshake/synchronisation with the game
// register the command to get the power flow from the game. (a value between -100 and 100)
OIS.registerInt(POWER_FLOW_PERCENT, powerflowCallback);
// register the command to get the total power from the game. (a value between 0 and 100)
OIS.registerInt(POWER_LEVEL_PERCENT, powerlevelCallback);
OIS.activate(); //stop syncing and ACTIVATE
}
void loop()
{
OIS.update(); //required to keep getting info from the game.
}
// because we registered this in setup(), this gets called every time
// OIS.update() is called. therefore our info is refreshed
void powerflowCallback(int channel, int data)
{
flow = map(data, -100, 100, 0, 255); // Converts our -100 to 100 value to a useable PWM value (0 to 255)
lcd.setCursor(0,0); // Set the cursor to the top left of the screen (0,0)
lcd.print("FLOW PWM: "); // Prints our label on the screen plus 3 empty spaces to insure our value which can be 1, 2, or 3 digits is properly cleared and reprinted
lcd.setCursor(9,0); // Backs up 3 spaces to print our data value on the empty spaces from the previous line
lcd.print(flow); // prints our PWM value
analogWrite(9, flow); // Writes our PWM value to pin 9
}
void powerlevelCallback(int channel, int data)
{
power = map(data, 0, 100, 0, 255); // Converts our 0 to 100 value to a useable PWM value (0 to 255)
lcd.setCursor(0, 1); //set the cursor to the 1st position of the second row
lcd.print("BATT PWM: "); // Prints our label plus 3 empty spaces like above
lcd.setCursor(9,1); // Backs up 3 spaces to print our data value on the empty spaces from the previous line
lcd.print(power); // prints our PWM value
analogWrite(10, power); // Writes our PWM value to pin 10
}

You must log in to post a comment.