Thursday, March 17, 2016

Raspberry Pi Explorer Hat Pro Tutorial: Mod of Potentiometers controlling a Virtual Minecraft Bar: Part One

Feel free to use this blog post and it's follow up, Part Two, as a tutorial. I chose to not make this a step-by-step procedural tutorial. I intentionally left if open-ended to push learners new to coding (like myself) to be a bit more challenged by having to do some problem solving. I also included screenshots of my code in the hopes of prompting learners to write their own code in IDLE instead of copying/pasting the code. Typing out the code from scratch helped me understand the importance of the text formatting rules for Python.

Here's the list of parts you will need to do this tutorial:

Raspberry Pi Model B+ (slower, but slightly cheaper) = $30 or RPi2 or RPi 3 = $40
also the typical peripherals needed to start using your Raspberry Pi. These components vary in price, but Adafruit does sell a Raspberry Pi Starter Packs (monitor not included) for $60.
  • a wireless keyboard/mouse set
  • LED monitor with HDMI and HDMI cable
  • Micro USB Power Supply
  • micro SD card to install Raspberian (Jessie) onto
  • USB Wi-Fi for the RPi
Python installed on RPi free
  • here's a great tutorial video on the procedure for installing and getting started with Python (IDLE comes bundled with Raspbian Jesse OS) on RPi.
Pimoroni Explorer Hat Pro = $23
Rotary Potentiometer x 2 = $2.50
Jumper Wires Bundle = $2

Trying to modify the Explorer Hat Pro/Minecraft Virtual Thermometer Project turned out to be much more challenging than I imagined it would be. The first mod I wanted to attempt was to change the code to convert the analog voltage readings from the TMP36 sensor to Fahrenheit not Celsius when printing to the Python Shell with IDLE. I spent a few hours researching coding options and mainly just found a lot of info on Celsius to Fahrenheit conversion scripts. I tried and failed several times to take parts of conversion code that I found and use it to replace the following "while true" code:

while True:
temperature = 25 + (explorerhat.analog.one.read() - 0.75) * 100
print("The temperature is {}c".format(temperature))
thermometer.update(temperature)
time.sleep(0.1)

So I gave up after 10 remix attempts. I tried loading script variations into the Python shell and was greeted with a plethora of bright red errors that basically alerted me about my ineptitude with following proper Python protocols.

I then moved on to a totally different remix idea based on another script I found in the GitHub ExplorerCraft directory where a TMP36 sensor fed voltage into the #1 analog input of the Explorer Hat Pro and a variable resistor potentiometer sending voltage into the #2 analog input. 

Here is a screenshot of the code from GitHub:

I felt the script was short and simple; therefore, with my newbie coding abilities, I should be able to alter the code fairly quickly (a very naive thought). My goal was to eliminate the TMP36 sensor from the circuit, replace it with a 2nd potentiometer, and then replace the temperature related code with code for the 2nd potentiometer which would control a second virtual bar in Minecraft. I also renamed the print command code, replacing the variables of "thermometer" and "potentiometer" with "red" and "green" to represent the two virtual bars manipulated by the potentiometers.

My first semi-successful script caused both the green and the red bar to respond to just the analog #1 input from the smaller blue poteniometer because I copied and pasted the same "while true" lines for each variable.
1st attempt = just one potentiometer input worked.

It took me awhile to figure out that the first 3 digits in the variable code lines were the X,Y,Z Cartesian coordinates for the bars in Minecraft. The original script had the potentiometer-controlled bar's X coordinate at 1 (with the thermometer's X at 0); therefore the two bars stood flush against one another. I decided in my mod to set X at 2 for the second potentiometer variable (green), thus creating a bit of space between the two bars. Other changes I entered into my mod were, changing the first bar color to red and changing the temperature value to "value2". After four code rewrites I finally got everything to work.

2nd attempt = duplicate Cartesian coordinates created a "bar overlap".

3rd attempt = both potentiometers worked, but can't locate the bars in Minecraft

4th attempt = success with separating the bars and controlling each independently.

Here is a screenshot of my modified code:

I found this remix project to really help me understand the different components of a Python script. I really enjoyed solving the puzzle by identifying how each code line related to other lines. The error messages I got when trying to run the module also helped me rewrite faulty sections of code. Since there were no detailed step-by-step instructions included in the GitHub directory, I had to ramp-up my problem solving skills to a higher level, carefully examining cause and effect results. Being forced to not have step-by-step instructions, Fritzing circuit diagrams, or code that was ready to copy/paste into IDLE really pushed me to be slightly more independent. In Part Two, I will (and you will too if you are using this blog post as a tutorial) add a 3rd variable.

In part two of this project (see my next blog post), I will add a 3rd variable and matching sensor. If you are actually trying to follow along and do your own remix or my remix, I encourage you to try and insert the TMP sensor back into the circuit and add the temperature variable back into my modified script posted above.


No comments:

Post a Comment