Skip to content

Physical Computing Lab: Digital I/O with Arduino

September 13, 2011


I’m going back to the basics with Arduino and I think will be good for me to review the foundations again. I also think it might be a good time to attempt to teach myself, in parallel, another microcontroller platform. Anticipating this course, a few weeks ago I ordered a Picaxe 08M2 chip from Sparkfun and a programming cable. But before I get into that, I’m going to document the lab exercises with the Arduino. Here’s the code that I wrote for this lab:

// PComp Lab 1

#define switchPin 2
#define yellowPin 3
#define redPin 4

void setup() {
  pinMode(switchPin, INPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(redPin, OUTPUT);
}

void loop() {
 if (digitalRead(switchPin) == HIGH)
 {
   digitalWrite(redPin, LOW);
   digitalWrite(yellowPin, HIGH);
 }
 else
 {
   digitalWrite(redPin, HIGH);
   digitalWrite(yellowPin, LOW);
 }
}

Admittedly, I went into this a little cocky. I wrote the whole sketch and then clicked upload, forgetting that an issue on my main computer prevents me from uploading code to Arduino boards (I think it has to do with the RXTX libraries). In fact, when I do attempt to upload code, Arduino crashes completely. Of course, I didn’t save my work, tried to upload and lost everything. I switched over to my old computer, rewrote the sketch and uploaded it successfully. Here’s where I got knocked down a peg a second time: the yellow LED was always illuminated, whether I hit the button or not and the red LED never illuminated. I went right to my if statement:

if (switchPin == HIGH)

Whoops! I totally forgot that I have to call digitalRead() in order to get the status of that pin. After I got that working successfully (documented above), I moved over into translating it into PICAXE. First, here it is working:

Here’s what the code looks like:

main:
	if pinC.3 = 1 then
		goto yellow
	else
		goto red
	endif
	goto main 
red: 
	high B.1 
	low B.2 
	goto main 
yellow:
	high B.2
	low B.1
	goto main

I learned BASIC as a kid, but moved onto C in high school and never looked back since. This doesn’t look like my old BASIC programs at all! And in fact, I found so much of this implementation to be counterintuitive. Perhaps it’s because I have almost mastered C? I don’t know. Anyway, I got it working. But it has left me questioning whether it’s worth the time to invest in learning this weird (to me) language.

Another post to come, where I will hopefully get creative with digital I/O on Arduino (and maybe Picaxe).

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: