Category Archives: programming

Trial and Error with Python

In my advice to newbie programmers, I recommended making sure you were aware of different versions of the languages. The books I had been using as well as my new books, Learn Python the Hard Way, all use Python 2.7.  I have learned that there is some disagreement about whether you should learn Python 2 or 3. The best piece of advice I read was to do the exercises in Python 2 and then rewrite them in Python 3, so that’s what I have been doing.

I’ve been doing a lot of googling but also some trial and error as I learn the basic differences between the syntax. There’s a little thrill when the Python 3 code runs correctly!

Making Time For…

Somehow, my March Raspberry Pi and Python adventure got lost in April and May. Work, travel, semester end, all those things took up my time. Learning Python was not a priority, and once it fell off the to do list, it stayed off. It was easy to ignore the Raspberry Pi, too, since the huge monitor was gone.

It happens often, doesn’t it? The things we would like to do get pushed aside for all the things we feel like we have to do. I think it’s even harder for someone like me who works independently. I can just keep working, maybe with the sense that I could actually get through the WHOLE to do list and then have some free time.

Let’s face it: that isn’t going to happen. Just as I am checking off the last item, an email shows up with some non-urgent request. Might as well answer it so it won’t be lurking around tomorrow. And, if I worked on the newsletter now–even thought it isn’t due to go out for four days–then it will be done. And, and, and…you are getting the gist of it, right?

So, at some point in the last couple weeks, I started adding non-work, non-priority activities to my to do list including Python programming, piano playing, blogging and yoga, all activities I enjoy but that often got lost in the fog of work. Just having them written down along helped a bit. And then there was the advice of Zed A. Shaw, author of Learn Python the Hard Way in his “Note on Practice and Persistence” in the introduction:

If you break the problem down into small exercises and lessons, and do them every day, you can learn to do almost anything. If you focus on slowly improving and enjoying the learning process, then you will benefit no matter how good you are at it.

And that’s really become my strategy: do a little each day. Sometimes, the few minutes at the piano or on the yoga mat turn into a half hour or hour. One Python exercise becomes two or three. And here’s the blog entry for today: no major lessons but a reminder to use all that if something is important to us, we need to make time for it. In a 24/7 world, we can work all the time…but that, as we know, makes us all dull people.

 

Some Quick Tips for Newbie Programmers

The first steps of learning a new programming language is often to reproduce code written by others in books and on webpages. Seems easy, right? Should work perfectly, right? Not always.

Three things to do if borrowed code doesn’t work for you:

  • Always read the comments. Several times now, I’ve found that the original poster was almost right and a commenter with more experience than me had found the error or made the necessary addition.
  • Know your versions. I’m learning Python and I’ve learned that there are two versions with different syntax. Knowing the version of your tutorial is important. Both books I’m using–Make: Getting Started with Raspberry Pi 2nd edition by Matt Richardson and Shawn Wallace and Hello World: Computer Programming for Kids and Other Beginners 1st edition by Warren and Carter Sande to explore python more in depth–use version 2.7 syntax.
  • Retreat and reboot. I’m using IDLE as my text editor and python shell. I thought I had correctly installed pygame and I wasn’t getting syntax errors, but the pygame window refused to open.  The code was right, and it just wouldn’t run. I closed everything out and went away for awhile. When I returned and fired up my little python program, the window opened perfectly. Two lessons here: sometimes you just have to walk away. And that usually means a complete reboot that allows your computer to do its own retreat.

I am having fun doing some learning and being able to access it on my laptop is much more natural. I think I’ll be able to find more pockets of time including in hotel rooms now that I don’t have to carry all the peripherals. The RPi and a short ethernet cable are all I need.

 

Running Pi On My Mac

I’ve fiddled around a bit with my Raspberry Pi in the past but wanted to be able to run it using my laptop rather than having all the parts (monitor, keyboard, and mouse) cluttering up my workspace. Yesterday, in between Spring cleanup and gardening, I managed to get it to work. It took several chunks of time: I would work on it, get to a stopping place of either a roadblock or success, go outside and play in the dirt, then return with renewed energy.

I knew I wanted to do it via Ethernet as that way I could do demos without having to worry about connecting over wireless networks. And the two devices had to be connected to each other rather than through a router for the same reason.

There was a lot of trial and error but by last evening, I was playing with python via my mac. The steps below are NOT in the order I did them, and I wasted a whole block of time trying to get a small screen I have to display before giving in and setting up the RPi workstation. (The screen issues had to do with the screen and not the RPi.)

I tried the first option here and that at least got me as far as discovering and setting network addresses on the two devices. But, as the site points out, without having a keyboard and display, it’s tough to test. Once I gave in to setting up the workstation, I was able to have both devices talk to each other through ping pretty quickly.

I installed a VNC server via this Instructables site. The comments led me to the X11vnc as well as the missing code in the original directions. I figured I had to get X11vnc to autostart so I followed these directions. I never did get autostart to work, but the other issue that stumped me for a bit was the fact that I had to login to the RPi after start up before I could view it on my laptop, and I wasn’t sure how to do that without having the RPi plugged into its own desktop peripherals. I followed some directions for auto login but that seemed to take away my root abilities.  This was the point where I had to do some backing out, unediting a few files.

Finally, just as I was thinking it would have to wait for another day, I found this site by using the keyword “headless.” I had done a lot of the directions, but it was the ssh part that I hadn’t quite figured out. Worked wonderfully and left me wondering how much time I might have saved had I found this site first. But, I also learned a lot through the trial and error, getting comfortable with terminal windows and linux commands so it wasn’t a complete waste of time.

Now, I use the terminal window on my Mac to ssh into the RPi. I start up the X11vnc server, start up the the RPi graphical user interface and then use VNC to interact with it since I get an error message when I try to connect via the server command on my Mac.

Lessons learned? The Internet has a wealth of information and I am grateful to everyone who posted all the links above. However, it was not always exactly what I wanted or needed so I had to pick up bits and pieces from here and there. I did things that needed to be undone so a little ongoing documentation helped. (I have a paper journal where I can jot short term notes.) Finally, as always, knowing the language of the project helps to choose the right keywords.

 

Ozobot Out of the Box

I finally put “robots” on the top of the to do list for the weekend. Opened up the Ozobot Bit, did a little reading, and started drawing.

I started pretty simple with changing the speed. It slowed down correctly and made the left turn, but the turbo boost color combo didn’t do what I expected:

 

Exit Instead of Turbo from Karen Richardson on Vimeo.

I was also having trouble getting it to do U turns.

A little troubleshooting and I discovered that the blue marker I was using was too dark so the Ozobot couldn’t distinguish it from the black. Here it is hitting the turbo boost and then doing the Win/Exit dance:

Turbo Plus Win/Exit from Karen Richardson on Vimeo.

Here’s the U turn. The light blue color makes everything work perfectly:

A Working U Turn from Karen Richardson on Vimeo.