Sunday 9 August 2015

Raspberry Pi 2

RPi2 Doc

Backstory

My uncle from California came to visit us up in Canada on August 2, when he asked what I would like as a present I suggested a Raspberry Pi 2 Model B and here it is! My plan is to see what the Pi can do; I'll be mostly focusing on programming, GPIO, and remote access.








Programming

I took programming 11 last semester, we mostly worked in Ruby and then some Java at the end of the year. I found Python to be similar to Ruby and transferred a lot of knowledge over. Making a new script by going to File>New Window I started with the classic 'Hello World!' script that says 'Hello World!' in console, the code looks like this:
print ('Hello World!')
 Yup, it's that simple, now go to Run>Run Module and you will see you script running in the original IDLE3 window!


Comments, Importing, and Variables

Now lets make a code that ask the user for their name, waits one second, then greets the user. I added comments by typing '##' before typing what I want. Comments are important so you (and other people) will know for example, what this specific line is for, comments are not read when the program is running and are purely made to supply information to the person looking at the script. As you can see through the code I explain how time is imported at the beginning to make 'time.sleep(amount of seconds)' work. You can also see I have a variable called 'name' in this script, 'name' says 'What is you name?' in console then waits for user input, it then stores this user input in the variable as a string (ie text so you can't use it in a math calculation, I'll get to variable types in the next section), you can see I can access this variable later and I used it in the last line where the program greets you with your personal 'name' you typed in beforehand. 
The code looks a little weird on the blog (I'll try to figure out how to fix it) but if you copy and paste the text into an IDE or something like Notepad++ (regular notepad is terrible as it doesn't keep formatting like tabs or enters).
import time ##imports the time asset which it required for the sleep function

print ('Greetings, my name is Raspberry Pi the Second of Model B')
name = input('What is your name?') ##this is the variable 'name' it equals input from the user and prints whatever is in the brackets into console
time.sleep(1) ##this is why we imported time at the start of the program, this line here lets the program sleep (wait) for a second before moving to the next line
print ('Nice to meet you ' + name + ', have a wonderful day') ##this prints the preset quotes plus whatever the variable 'name' has in between

Variable Types and Converting Between Them

Lets learn a bit about variable types, the 4 you need to know right now are:
  1. String, a string is used when collecting or printing out text, you can't use a string in a math equation, you can change a (float or int) variable to a string by typing 'str(variableName)'
  2. Float, a variable that holds numbers with decimals, you can use a float in a math equation, for example a line of code to multiply the float by 2 looks like 'variableName*2', you can change a (string or int) variable into a float by typing 'float(variableName)'
  3. Integer, similar float but it doesn't hold decimals, use this if you know you will not be dealing with decimals because it's faster or something, you can change a (string or float) variable to an integer by typing 'int(variableName)'
  4. Boolean, a True/False variable used often in IF and While statements (we will get to these later), when converting if your variable is empty or contains '0' it will be false, if not it's true, you can change a (string, float or int) variable to a boolean by typing 'bool(variableName)' 


GPIO Specific

To start programming your RPi GPIO you need to to 'apt-get -y install python3-rpi.gpio' this allows you to use 'import RPi.GPIO' in your python scripts, this is needed for your scripts to communicate with the RPi GPIO.
Take a look at this quality tutorial on Sparkfun, it's for the original RPi but it still applies to the RPi2 since (most) tutorials transfer seamlessly to the updated model.

*This blog will be updated/edited whenever I have the time so bookmark to keep updated :)

No comments:

Post a Comment