posts tagged with the keyword ‘code’

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!

2013.05.13

Makey Makey Apple Piano

I’m one of those people who say “I’ll publish that code later!” and then I get busy with other projects and never publish the code, so here’s me publishing the code.

So back in January I built the MaKey MaKey Apple Piano for one of Art Milwaukee‘s events that the Milwaukee Makerspace was involved in. And yeah, here’s the code!

MakeyPlayer

I’ve provided the Processing sketch and related data files, as well as full applications (for multiple platforms) for those who don’t want to mess around with compiling sketches, and just want to download and run an application for their MaKey MaKey.

Could it use improvements? Of course it could! Feel free to make it better, and if you do, please share your changes with others.

2012.12.12

CheerLCD

Here’s my CheerLight 2012 device, which I call the CheerLCD! And what is CheerLights you say?

CheerLights is an ioBridge Labs project that allows people’s lights all across the world to synchronize, stay linked based on social networking trends. It’s a way to connect physical things with social networking experiences and spread cheer at the same time.

Much like last year, I’ve opted for a small desktop display—a USB-powered computer peripheral—rather than some giant string of multicolored lights…

CheerLCD

With the combined power of 3D printing, affordable electronics, and the duct tape of programming languages that is Perl, we’ve developed a device that informs you of what color the CheerLights around the globe are, not only with color, but with words!

(Though we’ve not yet done extensive testing, the text should be legible even by those suffering from color blindness. Accessibility, FTW!)

CheerLCD

The CheerLCD consists of a USB + Serial Backpack Kit and LCD Display from our friends at Adafruit Industries. But you can’t just have a display without some sort of thingy to display the display properly… enter the 3D Printer!

CheerTree

The CheerTree was designed specifically to hold the LCD Display. I utilized Inkscape for the design of the front plate, and then brought that shape into OpenSCAD to add the base and create an STL file for printing. (It ended up warping a bit but that just adds to the charm and aesthetic of the overall device.)

I know what you’re saying, “This is all well and good… but we need to see the CheerLCD in action!” As you wish, my friends… as you wish.

There’s some code over on github/CheerLCD, and some files on Thingiverse for the CheerTree.

Enjoy the Holiday Cheer!

2012.02.07

Processing

I had a lot of fun with Processing last year when I joined in with a few other folks and declared May as “Processing Month” and my final project was the Make A Sketch, which was an Arduino + Processing piece.

There’s two other guys at Milwaukee Makerspace with an interest in Processing, and we figured it was worth sharing what we know, and we’ve decided that a 3-hour workshop on the subject would be a good idea, so…

Join us Thursday, February 23rd, 2012 at 6pm to learn about Processing! [Sign up here]

We’ll expect you to have Processing installed on a laptop, basic knowledge of writing code, and a few simple sketches running. If you can do all that, and want to dive a bit deeper into Processing, we’d love to have you there. (If you’re a Milwaukee Makerspace member the cost is $20, otherwise it’s $25 for the general public.)

Besides some basics of Processing we’ll be creating a collaborative team project, so it should be all-around awesome. If you’re a coder who wants to make some interactive art, or an interactive artist who wants to write some code, well… you’ll fit right in. :)

sketch

2011.07.26

Temperature and Humidity

I’m always annoyed with myself when I don’t publish my code, so this is a follow-up to my Logging the temperature and humidity post.

This whole thing is definitely a work in progress. The hardware has exposed wires, and the software is all just cobbled together pieces. Making something that works is much different than making something that will continue to work, and work properly, and efficiently. Making something that works is a first step. It’s an important step, but it’s only the first step. So let’s begin!

I started with the example from Adafruit over on GitHub and about the only changes I made was to the format output of the data.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  dht.begin();
  delay(1000);
}

void loop() {
  int h = dht.readHumidity();
  int t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print(h);
    Serial.print("\t");
    int temperatureF = (t * 9 / 5) + 32.5;
    Serial.print(temperatureF);
    Serial.print("\n");
  }
}

So this outputs the value for the humidity, then a tab, then the value for the temperature, and then a line return. Oh, I did change the code to output the temperature in Fahrenheit instead of Celcius. If we get a bad value, it’ll print “Failed to read from DHT” though I’ve yet to see that in the log files, I should probably take it out. Also, the Arduino just streams data at a fast rate, much faster than I need. I typically put the sleep functions (to limit the amount of data logged) in my reading code which runs on the computer. I think this is the right way to go, but if you have other ideas, please let me know.

Here’s a fairly simple Processing sketch that will read the data coming in from the Arduino. (Obviously you need the Arduino connected to the computer via USB.)

import processing.serial.*;

PrintWriter output;

Serial myPort;    // The serial port: 
PFont myFont;     // The display font: 
String inString;  // Input string from serial port: 
int lf = 10;      // ASCII linefeed 

void setup () {
  // set the window size:
  size(500,200);  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  background(50);
  output = createWriter("output.txt");
}

void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
  
    String[] words = split(inString, '\t');
    String humid = words[0];
    String tempe = words[1];
  
    background(50);
    textSize(40);
    text("   Humidity: " + humid, 40,90);
    text("Temperature: " + tempe, 40,160);

   output.printf("%d-%02d-%02d %02d:%02d:%02d\t%s", year(), month(), day(), hour(), minute(), second(), inString); // Write to the file
   output.flush(); // flush output
}

Besides displaying a small window with the humidity and temperature (seen at the top of this post) it will also log the data to a file named output.txt

Processing Output

2011-07-25 19:09:14     44      91
2011-07-25 19:09:14     44      91
2011-07-25 19:09:15     44      91
2011-07-25 19:09:15     44      91
2011-07-25 19:09:16     44      91
2011-07-25 19:09:17     44      91
2011-07-25 19:09:17     44      91
2011-07-25 19:09:18     44      91
2011-07-25 19:09:18     44      91

This is pretty terrible, as we’re getting way too much data. I should probably add some delay into the Processing sketch, as it’s creating a log file line much faster than the sensor can even output data! This will result in large log files and is just wasteful. If all I wanted was the window showing the current temperature and humidity, I’d probably just delete the logging part of the sketch. Add that to the TODO list. Anyway, it works, and as a proof of concept, it’s a start.

Here’s the Perl code I was using:

#!/usr/bin/perl

use Device::SerialPort;
use IO::Handle; FILE->autoflush(1);
my $port = Device::SerialPort->new("/dev/tty.usbmodemfd411");

$port->baudrate(9600);
$port->databits(8);
$port->parity("none");
$port->stopbits(1);

my $fileout = './templight.log';
my $filerec = './templight-recent.log';

open (FILE, ">>$fileout") or die ("Ooops! Could not open $fileout: $!");

while () {
    # Poll to see if any data is coming in
    my $char = $port->lookfor();

    # If we get data, then print it
    if ($char) {
	$time = localtime(time);
	if (length($char) > 1) {

		my $str = ($time . "\t" . $char . "\n");
		print FILE $str;

		open (FREC, ">$filerec") or die ("Ooops! Could not open $filerec: $!");
		print FREC $str;
		close (FREC);
	}
    }
    # Uncomment the following lines, for slower reading, 
    # but lower CPU usage, and to avoid 
    # buffer overflow due to sleep function. 

    $port->lookclear; 
    sleep (10);
}
close (FILE);

The logging here is a little better than the Processing sketch, at least we add some delay with that sleep at the bottom so our log files won’t be quite as large. We’re saving to two files, templight.log and templight-recent.log. The second one just saves the current data, this just enables us to grab the data from that file and do something else with it, like… send it to Twitter or something.

Perl Output

Tue Jul 26 04:30:39 2011        46      89
Tue Jul 26 04:30:49 2011        46      89
Tue Jul 26 04:30:59 2011        46      89
Tue Jul 26 04:31:09 2011        46      89
Tue Jul 26 04:31:19 2011        46      89
Tue Jul 26 04:31:29 2011        46      89
Tue Jul 26 04:31:39 2011        46      89
Tue Jul 26 04:31:49 2011        46      89
Tue Jul 26 04:31:59 2011        46      89

So our output from Perl looks similar, though we have much less data (but still plenty of data) and the date format is a little different. (Chalk that up to laziness.) Oh, the Perl code also has the serial port hardcoded to /dev/tty.usbmodemfd411, which is lame, and I do have code somewhere to grab the serial port dynamically, but I didn’t implement it yet.

Whew! It feels so liberating to publish all that terrible code… (I mean my own code, not Adafruit’s!) Now I feel the need to start re-writing it and making it less terrible.

« Older Entries |

buy the button:

Buy The Button

recently at:


top recent artists: