# [Hardware, Plugin] Arduino based physical display + serial port io+ tutorial (10-06-17)

## Recommended Posts

It's again me, I reread the code one more time but I don't understand where put le wires, there are numbers many times, there is three 0 so one is for digital and one is for analog but the third ?!

##### Share on other sites
Posted (edited)

No. Lets go a bit into the theory behind this, understanding it will help you later.

Analog ports read a voltage, and convert 0-5V to values 0-1024. So in order to get our values, we need to supply a voltage we can control. The easy way to do this is with a variable resistor. It has three wires: is in either end of the resistor, and the last is connected so it divides up the resistance between one end and the other. For example, if you have a 10 000 Ohm  (10K) resistor, and you put the slider 75% of the way towards one end, the resistance between the third wire and that end and the middle wire is 2500 Ohm, while the resistance between the other end and the middle wire is 7500 Ohm.

So how does this help? Well, if you apply a fixed voltage between the end points of the resistor, the voltage will divide up in the same proportion as the resistor is divided. To continue the example above, you connect one end of the resistor to your ground port, and the other end of the resistor to +5V. Then the voltage read by the middle (analog) port will read the same ratio (25%) of 5 volt as the ratio of resistance.

So to sum up, you connect one wire to +5V, the middle to an analog port, and the last to ground. In most literature on the Arduino, the analog ports are named A0-A15, and ground is GND. On the board, you will find several ground ports. The exact layout is shown on this schematic, download it and keep it handy.

I hope the above helps.

Edited by Freshmeat
long story
• 2

##### Share on other sites

Thanks for explain, I already knew that, but the deepening is great (I have also all pinout of my arduino in pdf), but I was talking about button like the action groups, RCS, SAS : what pin is who

After this I will try the connect my 20*04 display

##### Share on other sites
Posted (edited)

Just as a tip, what I found helped was doing a handful of the Tutorials on the Arduino website. Get comfortable with the code, and how physically connecting things works. Then when you jump into someone elses code, you can decipher it much easier!

11 hours ago, Tobit said:

Thanks for explain, I already knew that, but the deepening is great (I have also all pinout of my arduino in pdf), but I was talking about button like the action groups, RCS, SAS : what pin is who

After this I will try the connect my 20*04 display

At the top of "KSPIODemo16' you have:

```//pins for LEDs
#define GLED 5
#define YLED 6
#define RLED 7
#define SASLED 11
#define RCSLED 12
#define CG1LED 13

//pins for input
#define SASPIN 8
#define RCSPIN 9
#define CG1PIN 10
#define THROTTLEPIN 0```

This is the nicest and easiest way to 'store' the pin numbers as a text variable, so you can reference them more easily in the code.

By default, the switch to turn on SAS plugs into pin 8 [#define SASPIN 8]; The switch to turn on RCS plugs into pin 9 [#define RCSPIN 9]

Then jump over to the 'output' tab, and you will see

```if (digitalRead(SASPIN)) { //--------- This is how you do main controls
MainControls(SAS, HIGH);
setSASMode(SMSAS); //setting SAS mode
//setNavballMode(NAVBallSURFACE); //setting navball mode
}
else {
//setNavballMode(NAVBallTARGET);
MainControls(SAS, LOW);
}

MainControls(RCS, HIGH);
else
MainControls(RCS, LOW);```

I will do a tutorial video in the next little while (this is something I wanted to do for a long time) which will explain the code... but honestly, most people here have fought with the code and figured it out!  )

But basically, the first part looks at the switch connected to pin 8 (SASPIN) and works out if it is HIGH - that is, is there current going to it. If so, it will send a signal to the game to enable SAS.

Same for the RCS. If this is clear as mud, you should go check out those arduino tutorials

The 'utilities' tab controls the LEDs that are connected. Once again, you will see a relationship between the code, and the 'defining' of the variables to pins on the board on that first 'KSPIODemo16' tab.

Edited by Sputnix

##### Share on other sites
Posted (edited)

I did it with this, and it work with with the stage I will see with the customs actioon groups now

Hi,

you just need to do this:

```if (digitalRead(somepin))
MainControls(STAGE, HIGH);
else
MainControls(STAGE, LOW);```

You can also control RCS, SAS, LIGHTS, and other stuff this way.

Edited by Tobit

##### Share on other sites
Posted (edited)

(As i am a C-coder / Arduino noob ->  correct me anyone if totaly wrong in the helpings).

1. Win 10... you have a USB problem.

and don´t use pin 0 / 1  and keep A4/A5 free for SerialComms / I2C (LCD)

2. the number of the Data or action groups is a simple structure or byte array expession for object definitions as i understood for myself.
not to be confused with the pinouts of an arduino or an #define substitute.
you have to understand that the RCSpin, RCS, and the RCSLed (example)-defines are totally different things.

3. as a little help:

at the top:

#define CG1PIN 10 // the digital number of the pinout
#define YLED 13 //  YLED is called "13".

and the other needed stuff.

in setup:

pinMode(CG1PIN, INPUT_PULLUP);  // for example this switch - define  that as input pullup with no resistors - important!
pinMode(YLED ,OUTPUT);   // pin physically on Pin 13 out.
// but the slider don´t need an input def ? (why?) it´s analog?

in subroutine // put in input/output section

if (digitalRead(CG1PIN)){   //--------- This is how you do control groups  // liest CG1PIN aus  reads that pin
MainControls(STAGE, HIGH);
digitalWrite(YLED,HIGH);   // you see below YLED is 13 and high / on

}   // wenn Status high dann führe staging aus - switch kann high gelassen werden; besser wäre ein Pushbutton  /if high than ...
else {
MainControls(STAGE, LOW);
digitalWrite(YLED, LOW);

}   // sonst low    else low

only an example. because the showing of a StageLED is not usefull. could do this with RCS SAS and the ActionGroups 0-10. as long as you have pins. You can also display the stuff in an LCD / 7seg or LEDs  or whatever you want. Only Problem is to cope with the ComputersKeys; if you have the switch on and type the SAS off, the switch is on and the LED off ;-)

firstly take the minimal config with the leds. then decide which you don´t need any more. for myself i don´t need any hardware pins for leds, because i use a digital LEDstripe (FAstLED). its only software. i realized at a certain point that i had a switch on pin10 and an LED on 10-  that didn´t work (properly). i spared 3-4 pins.

4. the rest for the 20x4 is sent you in a pmessage.
5. the slider / pot  you have strictly to define with YOUR min-max values and ranges. see my posts below. and put a resistor to the ground (debounce, because the values are go random crazy). with the super contrain/map function i had driven sucessfully a little 1 Volts analog display.
6. and don´t put any delay() function in code!  in loop main is only input() and output(). nothing else for timing issues. ;-)

PS: i forgot: the Throttle starting Value could be set to 0 or whatever in the settingsMenus

Edited by sec3
• 2

##### Share on other sites

Hello,

I plan to build a control panel. First I want to thank #zitronen for his great work and contribution to spread fun and increasing involvement in KSP, coding and making!

Now to my Problem. Everything look promissing but I'm stuck with the CPacked.NavballSASMode (set/get).

I'm more or less familiar with arduino coding but I never got in touch with byte editing and stuff like that. I apologize in advance for my incompetence and bad programming.

In the first tests I want to switch between SAS Modes 0-10 by pressing two buttons for up and down. Later every mode will get it own butten.

I tryed to understand the functions on page 70 for the NAV ball stuff, but I don't get how I get a values from this example or set a mode.

So I did something like this in the KSPIODemo16 output.ino in void controls(){...

```
if ((digitalRead(SASMODEUPPIN) == HIGH) && /*(CPacket.NavballSASMode <= 1010) &&*/ (SASPinUpCheck == 0)){     //Reads Pin 3; was to stay under 10 but seems not to be necessary, to sent one button press insteat of spamming
CPacket.NavballSASMode ++ ;
SASPinUpCheck = 1;
}

if (digitalRead(SASMODEUPPIN) == LOW){                                                                        //After releasing the butten can presst again ones
SASPinUpCheck = 0;
}

if ((digitalRead(SASMODEDOWNPIN) == HIGH) && /*(CPacket.NavballSASMode >= 0) &&*/ (SASPinDownCheck == 0)){    //Reads Pin 4; was to stay over 0 but seems not to be necessary, to sent one button press insteat of spamming
CPacket.NavballSASMode --;
SASPinDownCheck = 1;
}

SASPinDownCheck = 0;                                                                                        //After releasing the butten can presst again ones
}```

This kind of work but it switchs the Modes super fast even if there is the SASPinUp/DownCheck that should stop it from happen. The declaration is like that

void controls(){

int SASPinUpCheck;

int SASPinDownCheck;...

Maybe it doesn't work correct because of the Data Type of the NavballSASMode (byte)

The Arduino site sayes for Incrementel ++:

Quote

Parameters

x: variable. Allowed data types: integer, long (possibly unsigned)

int SASPinDownCheck;

So the main question is: "How can I edit the bits/bytes correctly and get Data from it?"

And second because the byte is split in half can I set the whole byte like a number.

Like SAS Mode 5 = Antinormal and Navball Mode 3 = Target means in the byte

B00110101

0011 for Navball 3 and 0101 for SAS Mode 5???

And if I want to change the Navball Mode add or subtract 16 if the byte can be seen and treated as number?

Thanks for the help!

Scorch93

P.S.: Because I can't find suitable joystick, I want to build my own ones. I'm not sure if I should build some with potentiometers or micro switches or a mix.

##### Share on other sites

Hey there

I´m working on a kind of hardware Navball. I´ve got all the wiring for the servos finished today and tested with my Arduino (currently an Uno).

So I´ve searched on ways to receive telemetry data (pitch, yaw, roll position; SAS targets like Prograde, Retrograde, etc..) and naturally stumbled across this mod/plugin/framework.

It contains the most essential data (pitch, yaw, roll) I will need, but some data are missing for me (well, maybe I diid not find them...):

- position or coordinates of the different SAS-modes (the "symbols" of PO, RE. Target etc... on the ingame navball)

- pitch, yaw and roll rate like for the indicators in the lower left corner of the hud or in IVA mode

I´m pretty new to coding for KSP (got some extended basic knowledge of coding of common languages) and so my question is, if it is possible to add an output for these values to the source code and if someone can give me a little hint where to look/start, since I don´t know how KSP handles these two sets of data.

Kind regards and thanks in advance for any help, Naru

##### Share on other sites

@Scorch93 The reason why it changes is that you SASPin<Up,Down>Check are defined inside controls(). It means they get redefined at every loop. You need to declare them as static in their declaration. In Demo16, zitronen has included the functions to separate the first and last bits, call setSASMode with your desired mode, it resides in output.ino.

@naru1305 You want to talk to @stibbons, he did a software navball implementation. And @richfiles has been working on a hardware navball for quite a while, I seem to recall a discussion in the Custom Hardware thread as well, but I think @Pvt. KASA had to abandon it for some reason?

##### Share on other sites
20 hours ago, Freshmeat said:

@naru1305 You want to talk to @stibbons, he did a software navball implementation. And @richfiles has been working on a hardware navball for quite a while, I seem to recall a discussion in the Custom Hardware thread as well, but I think @Pvt. KASA had to abandon it for some reason?

##### Share on other sites
On 7/2/2018 at 4:12 AM, naru1305 said:

I´m pretty new to coding for KSP (got some extended basic knowledge of coding of common languages) and so my question is, if it is possible to add an output for these values to the source code and if someone can give me a little hint where to look/start, since I don´t know how KSP handles these two sets of data.

The API for the game is at https://www.kerbalspaceprogram.com/api/index.html . If you dig around in either the Vessel or Orbit classes you should be able to find information about current prograde and other orbital information. Looking at how the current vessel heading code works in the KSPSerialIO plugin is valuable as well.

A rough overview of what you need to do: Get a vector for the direction you're interested in, say orbital prograde. Transform that vector so that it's relative to a fixed reference point - from memory the current code that does this for the active vessel heading uses the world normal. Then you need to transform that vector to a set of Euler angles.  The Euler angles are what you should report to the client.

There's been a couple of projects that can take the Euler angles being reported for vessel attitude and render a software navball, the code for mine is based on catmacey's project. It's applying the rotations to a 3D rendering of a navball, but should give you an idea of a client implementation for a real one too.

I've had a few stabs at adding the information you're looking for, but always fell down - getting my head around the Unity-specific bits, and 3D programming in general, proved to be too much to handle on my own. But I wish you well if you want to give it a try!

##### Share on other sites

@naru1305 You can get the directions without digging in the KSP API by using kRPC. That will however entail writing your own client in python. I am slowly getting my own toes wet in that particular can of worms, but I foresee several weeks of work before I get beyond simple attitude control.

##### Share on other sites

@stibbons thank you very much for these informations!  That is directly what I was searching for  I will have a look on it and from there I will see.

@Freshmeat thank you, too! That sounds like an valuable alternative! But this would also mean to go back again into the snakepit Python, which gave me in my final schoolyears informatic-class nightmares

I will first give me an idea on how things in detail are working and we will see