Kotlin – Buttons and TextViews

Set Text for text view

val textBoxVar: TextView = findViewById(R.id.textBoxID)
textBoxVar.text = "Hello World!"

Button clicked – do something

val buttonVar: Button = findViewById(R.id.buttonID)
buttonVar.setOnClickListener {
    buttonVar.text = "Button Pushed..."  //Change text on button


Toast notification

val text = "Hello World! This is a toast message!"
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(applicationContext, text, duration)

Using Seeed Studio O2 Sensor Arduino Without Grove Breakout board


The Seeed Studio O2 sensor is a nice Oxygen sensor. It works up to 25% Oxygen concentration and is easy to use in an Arduino project.

Wiring Up the O2 Sensor to Arduino

If you look on the back of the sensor there are only 3 wires used. VCC, GND, and a SIG. The NC pin is “Not Connected”

Wire the
GND -> Arduino ground
VCC -> Arduino 3.3V (Looks like it may also accept 5V)
SIG -> A5 (Analog 5)

Code to read O2 Sensor

Copy and paste the following code into the Arduino IDE

// Grove - Gas Sensor(O2) test code
// Note:
// 1. It need about about 5-10 minutes to preheat the sensor
// 2. modify VRefer if needed

const float VRefer = 3.3;       // voltage of adc reference

const int pinAdc   = A5;

void setup() 
    // put your setup code here, to run once:
    Serial.println("Grove - Gas Sensor Test Code...");

void loop() 
    // put your main code here, to run repeatedly:
    float Vout =0;
    Serial.print("Vout =");

    Vout = readO2Vout();
    Serial.print(" V, Concentration of O2 is ");

float readO2Vout()
    long sum = 0;
    for(int i=0; i<32; i++)
        sum += analogRead(pinAdc);
    sum >>= 5;
    float MeasuredVout = sum * (VRefer / 1023.0);
    return MeasuredVout;

float readConcentration()
    // Vout samples are with reference to 3.3V
    float MeasuredVout = readO2Vout();
    //float Concentration = FmultiMap(MeasuredVout, VoutArray,O2ConArray, 6);
    //when its output voltage is 2.0V,
    float Concentration = MeasuredVout * 0.21 / 2.0;
    float Concentration_Percentage=Concentration*100;
    return Concentration_Percentage;

Upload and Launch the Serial Monitor
Tools -> Serial Monitor
Ctrl + Shift + M

Bash Loop Examples

For i in 1-100 do

Basically count to 100 and perform an operation each time i increases.

for ((i=1; i<=100;i++))
  echo $i

for loop 1 liner

for ((i=1; i<=100;i++)) do echo $i ; done

While true (Execute forever)

Handy if you just want a script to run and repeat the same thing over and over again. Doesn't stop till you kill it.

while true
  echo "Repeat till infinity"
  sleep 1

While command is true

The following will execute the loop as long as the command in the () returns true. Once it returns false, it'll stop the loop

while (fping incredigeek.com | grep alive); 
  echo alive
  sleep 1

Bash array example

array=(one two three)
echo "Printing first object in array."  #Replace 0 with the place number of the array item
echo ${array[0]}

echo ""

echo "Whole array"
echo ${array[*]} 

echo "" 

echo "Array indexes" 
echo ${!array[*]}


Printing first object in array. 

Whole array
one two three

Array indexes
0 1 2


Clamp rotation of camera in Unity

Some notes and issues when trying to clamp the rotation of a camera in Unity. All the code was put on a script assigned to a controller that controlled the camera view. Used the Simple Touch Controller from the asset store.

Problem trying to clamp EulerAngles

It appears that the following code does not work as EulerAngles expects a value from 0-365.

transform.localEulerAngles = new Vector3(Mathf.Clamp(transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook, 0f , 20f ),transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook, 0f);

Extra debug info

Debug.Log("Rotation = " + transform.localEulerAngles.ToString());

Code to clamp rotation

You have to do some math to take the value from 0-365, then translate it to -90 – 90 so it can be clamped properly

// Put these with the rest of your public variables.  These should show up in the Unity Inspector and allow you to modify the values
public float speedProgressiveLook;
public float clampXRotationUp;
//  The following lines allow the script to Clamp the camera X rotation and should go in the Update function

float playerRotation = transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook; 

if (playerRotation > 180)        {             
    playerRotation = playerRotation - 360 ;  // Sets our working numbers between -180 - 180  
playerRotation = Mathf.Clamp(playerRotation, clampXRotationDown, clampXRotationUp);

Vector3 newRotation;
newRotation.x = playerRotation;
newRotation.y = transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook;
newRotation.z = 0;

transform.localEulerAngles = newRotation;

The following links were helpful.




Raspberry Pi – Ping IP Address and Toggle LED

The following script is for monitoring if an IP address is reachable or not. If it becomes unavailable the script will turn on a LED that is plugged into one of the GPIO pins of the Raspberry Pi. View pinout here


# Script to ping ip address and turn on LED on if device is unreachable.
                                                                                                                                                                                                 nPin="18"  # Change if GPIO pin is different                                                                                                     
ledPin="gpio${nPin}"                                                                                                                                                                                                                            toPing=""  # Change to address you want to ping

echo "${nPin}" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/${ledPin}/direction

if ( fping -r1 $toPing | grep -v alive ); then
         echo "Internet unreachable"
         # Turn on LED
         echo "1" > /sys/class/gpio/${ledPin}/value
         # Turn off LED 
         echo "0" > /sys/class/gpio/${ledPin}/value

Save script as ping_led.sh and make it executable.

chmod +x ping_led.sh

and run the script.

sh ping_led.sh

Run script in crontab

You can setup the script to run every minute using a crontab

crontab -e

Add the following line

*/1 * * * * /home/pi/ping_led.sh

Should now execute the script every minute and not need any human interaction.

PlayMaker reverting to local variables instead of using global variable

In PlayMaker you can experience issues with variables if you have a Global variable (Green in picture) and a Local variable (Red in picture) that have the same name. PlayMaker will let you select the Global variable however, you’ll get erratic behavior and it seems to revert to the local variable. Work around? Rename one of the variables.

Unity Button can’t access script functions

When you have a button object there is the On Click () area that can execute different actions like a PlayMaker script or a function in a C# script.

Issue with the C# script. You can’t drag and drop the script from the Assets Panel. The script needs to be assigned to an object, then drag and drop that object on the On Click () object. Then select the script and then the function from the drop down there.


Code snippet below is the EnableDisableAudio function that gets executed by the On Click () function above. Note that the script has to be assigned to an object, in this case the button.

    public void EnableDisableAudio()
        if (counter % 2 == 1)
            Debug.Log("ButtonToggled - On");

            Debug.Log("ButtonToggled - Off");

More info