posts tagged with the keyword ‘code’

2016.03.05

Copying and Pasting

I saw many friends share this amusing image of a fake O’Reilly book titled “Copying and Pasting from Stack Overflow”, and if you’re not in on the joke, Stack Overflow is a web site where you can post programming questions and get answers to those questions. It’s also a site (like many others) where people who are not sure what they are doing grab bits of code from when they are not 100% sure what they are doing. (And if you don’t know about O’Reilly, they publish technical books, many about programming.)

This is the first semester I’ve taught a class that includes programming. I was upfront with students about how I’ve learned to write code over the years, and that includes looking at examples, using existing code, and (eventually) reusing my own code. I didn’t force them to type out each line of an Arduino sketch because I didn’t think it was completely necessary. They already know how to type, and once I showed them how to hit the compile button, they learned about syntax errors. Occasionally in class I’d demo things by writing code on the fly and inviting them to type up what I just typed, and to play around with the values, add more lines, etc. (I showed them the “Examples” menu early on, which contains plenty of useful code to get started with.)

Here’s something that may be news to some readers – I’m not a great programmer. I’m a hacker, and I can write enough code to get by, but more importantly I can find enough sample code and examples and tutorials to write the code that I need to do what I want to do. I think that’s the key to things.

If you’ve ever heard the expression “To be a great writer, you need to be a great reader” then perhaps this also applies to programming, and reading code is an important part of writing code.

Now, with all that said, I still expect students to learn some of the basics of writing code and understanding it. If at the end of the semester all they’ve done is copied & pasted code that someone else wrote, and by some miracle it compiles and runs and works for their project, that’s still sort of good, but not quite as good as having a basic understanding of what exactly those lines of code are doing.

In the meantime, keep copying code, and pasting code, and while you’re at it, try to read it and understand it.

2016.01.31

Slideshow

I’ve been using Raspberry Pi single-board computers for video players for years now, and I’ve also used them as audio players, but I was missing a good way to use them as slide show devices… Until now.

I’ve used Screenly OSE for the MMPIS at Milwaukee Makerspace, and while Screenly is great for what it is (a network-connected, browser-controllable, digital signage device) sometimes you don’t want all the options and features and you don’t have a network. So I needed another solution.

I found fbi, “the Linux framebuffer imageviewer”, which can run at boot up and display a folder full of images at full screen with a configurable delay between changing images. Perfect!

Boot Volume

Now, there’s one more thing… If I’m going to put this somewhere that doesn’t have a network connection (like, in a museum) I want to be able to easily update the slides. The slides won’t be changing daily, but may be changing every month or so. For something like this it’s easy to store the images directly on the /boot volume, which is accessible on the SD card when you pop it in a Mac OS X or Windows computer.

You’ll notice a folder names “slides” and a file named “slideshow.sh”, which do the hard work here. It’s actually ridiculous to call it “hard work” because it’s dead simple. The slides folder contains images which will be displayed in alphabetical order. (Sadly, my screen shot does not reflect this!) In an ideal world you’d name your images 0001.jpg, 0002.png, 0003.jpg, etc. Just name them in order, and they’ll display in order. Easy.

Images

The SD card only has about 40MB of free space for you to put images on, but with JPG compression of photos, you can probably fit plenty of them. OK, so once you’ve got your folder full of images (which you can easily update on the SD card) you’ll need some way to run the slideshow.sh script.

Typically I do a sudo nano /etc/rc.local and add what’s needed to run my script. In this case it’s the /bin/bash /boot/slideshow.sh & you see on line 20. Oh, don’t forget to set the Pi to auto-login at boot. Since it’s been added to recent versions of Raspbian you don’t even have to muck around anymore.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/bin/bash /boot/slideshow.sh &

exit 0

So yeah, we’re all set… but you probably want to see the code in slideshow.sh, right? This is it. Right here. Yes, it’s pretty much just two lines. Linux is like that sometimes… It’s not hard implementing something, it’s just hard finding out exactly how to implement something. It can be hard finding those two lines that you need! (The 20 may need adjustment. Longer may be better, but in my testing on a Raspberry Pi 2 Model B it worked well.)

#!/bin/sh

sleep 20

fbi -a -t 6 --blend 200 --readahead --noverbose -T 1 /boot/slides/* >/dev/null 2>&1

There’s some parameters you can set, like -t 6 which sets each slide to display for 6 seconds, and the blend thing, which sort of works. Hey, it’s a slide show, okay!?

I’ll probably work on this more, but I was pleased to find a solution so I thought it was worth sharing.

2015.07.14

Teensy++ 2.0 LED Pin

Yes, this post is actually titled “Teensy++ 2.0 LED Pin” because it’s really specific. This is the solution to a problem that took me a while to fix. Actually, it didn’t take a long time to fix, it just took a long time for me to figure it out and implement it. (Maybe it did take a long time for me to fix…)

Anyway, when using most pins on a Teensy++ 2.0 (and probably every other Teensy) with Arduino code, you may have an issue using the LED pin as an input, because it functions differently than all the other pins. You might say “Hey, just use another pin!” but the project I did required every single pin on the Teensy++ 2.0. (Yes, all 46 pins!)

The code is below. The LED pin is sort of treated opposite of how other pins are treated. You short it with +5v instead of ground, and swap the risingEdge and fallingEdge typically used with the bounce library.

// LEDPinButton

#include <Bounce.h>
 
Bounce buttonD6 = Bounce(6, 80); // LED Pin - tie to +5v instead of GND
Bounce buttonD7 = Bounce(7, 80); // Normal Pin - tie to GND
 
void setup() {
  pinMode(PIN_D6, INPUT);        // LED Pin - use INPUT not INPUT_PULLUP
  pinMode(PIN_D7, INPUT_PULLUP);
}
 
void loop() {
  
  buttonD6.update();
  buttonD7.update();
 
  // D6 - LED Pin - tie to +5v instead of GND
  // use risingEdge instead of fallingEdge
  // a
  if (buttonD6.risingEdge()) {
    Keyboard.set_key1(KEY_A);
    Keyboard.send_now();
  }
  if (buttonD6.fallingEdge()) {
    Keyboard.set_key1(0);
    Keyboard.send_now();
  }
  
  // D7 - Normal Pin - tie to GND
  // b
  if (buttonD7.fallingEdge()) {
    Keyboard.set_key1(KEY_B);
    Keyboard.send_now();
  }
  if (buttonD7.risingEdge()) {
    Keyboard.set_key1(0);
    Keyboard.send_now();
  }
  
}

You can also grab this code from github.

2014.05.09

Banana Logo

I’m posting this because someone asked for it, and I aim to please… Here’s the Banana Pong code. I used code someone else wrote to bootstrap this thing, but there was no comment about who wrote it, and I didn’t make record of where I grabbed it, so… no attribution. Sorry! If it’s your code, let me know.

Have fun playing Banana Pong with your MaKey MaKey!

Note: The first ZIP file is the Processing source code. The second is a Mac OS X application. Since Processing has changed how it exports applications I can’t easily create versions for Windows and Linux like I did for the Apple Piano code. So if you want a Windows or Linux standalone version, you’ll need to grab the code and do it yourself. It should serve as a good starting point.

2013.12.10

code!

There’s been so much talk about learning to code lately… I figured I should provide some insight, as someone who has been writing code that computers can understand (sometimes) since the early years of the 1980s.

Writing code is fun! Well, sometimes. You know, on the rare occasions when your code actually works. Writing code can be fun, just like things blowing up can be fun… under the right circumstances. Fireworks on the fourth of July? Fun! Your car’s engine blowing up? Not fun! Don’t worry, your code will “blow up” on occasion, and you’ll have to fix it. Often this will happen at 2:30am.

Maybe writing code is fun like driving is fun. You know, a cross-country trip, 20 hours straight in a car with cranky, smelly people… that’s fun! Having things break for no reason and having to spend lots of time (and possibly money) fixing things? Fun!

I’m partially kidding, of course. Writing code can be fun, but it can also be terrible. You’re typing at a keyboard with the goal of telling a computer what to do. We really are slaves to the machine. Computers were supposed to help automate everything so we’d have more free time for leisurely activities like building robots, which of course require you to program a computer to make them work. It’s a vicious cycle!

But hey, you should write code. You should write code to solve problems. You could be awesome and release that code under an open source license so the next poor sucker doesn’t spend 10 hours solving the problem you already solved. Of course some people/companies/organizations believe that everyone should solve their own problem, so they won’t let you share your code, even if you wrote it on your own time… don’t work for them. Work for the betterment of humanity.

You should also write code for fun, real fun, enjoyment, for real! Write code that does something silly, or stupid, or amazing, or to play a prank on someone. Don’t lose sight of the fact that learning should be an enjoyable experience. Most programmers I know have a sense of humor. It’s probably a requirement for that line of work.

But… which language should you choose? Eh, it doesn’t matter. Start with the easy ones, perhaps a scripting language. It may be all you need. I’ve been using Perl for about 16 years, and I still use it all the time because it’s the fast and easy way for me to get things done. I like getting things done more than I like buzzwords or whatever the popular kids are using to write code.

So yeah, write some code… see if you like it. If you hate it, don’t write code. Find something else to do. The world always needs people to bake things. Baked goods are delicious and very enjoyable, and it’s extremely rare that someone complains that a loaf of bread just “stopped working” or somehow didn’t do what they wanted it to do.

Bake on, man… Bake on!

« Older Entries |


buy the button:

Buy The Button