An update on some stuff


Okay so I went away for the weekend for a cycling race and on the car drive out I mostly just messed around with my new macbook air (more on that later). On the way back when I was extremely tired (as one is after racing the national champion) I remembered that I’d promised to write up some better behaviour code for that little robot I built.

The end result of that lazy little 20 minutes of code writing is pretty simple. Basically the program picks whether the robot is going to be happy, sad or neutral when the door is opened. Once it has determined that it simply shakes it’s head, blinks it’s eyes or plays a tune based on how happy it is. There are varying levels of action for each part of the robot to correspond with how happy or sad it is. Anyway, here’s the code so you can get a feel for what it’s like:

//Servo robot behaviour program
//Created by Lochie Ferrier to control a small robot
//the servo control information
#include <Servo.h>
Servo head;
//the mood of the robot when the program runs for the first time
String mood=”happy”;
//initiate variables
int selector, speakerPin, lastReading, pos,range,ledPin,lastPitch,pitch,additive,timeAfterAction=60000;
void setup(){
head.attach(9);
Serial.begin(9600);
}
//HEAD FUNCTIONS
//function to shake the head slowly
void headShakeSlow(){
pos= random(0,180);
head.write(pos);
delay(3000);
head.write(0);
delay(3000);
}
//function to shake the head moderately
void headShakeMedium(){
pos= random(0,180);
head.write(pos);
delay(2000);
head.write(0);
delay(2000);
}
//function to shake the head fast
void headShakeFast(){
pos= random(0,180);
head.write(pos);
delay(1000);
head.write(0);
delay(1000);
}
//LIGHT FUNCTIONS
void eyeBlinkSlow(){
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
}
void eyeBlinkFast(){
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
}
//SOUND FUNCTIONS
void melodyLow(){
pitch=200;
int count=0;
if(count<10){
range=50;
additive=random(-range,range);
pitch = pitch+additive;
tone(speakerPin, pitch);
pitch=lastPitch;
count++;
delay(500);
noTone(speakerPin);
}
}
void melodyHigh(){
pitch=400;
int count=0;
if(count<10){
range=50;
additive=random(-range,range);
pitch = pitch+additive;
tone(speakerPin, pitch);
pitch=lastPitch;
count++;
delay(500);
noTone(speakerPin);
}
}
void loop(){
if((lastReading+2)>analogRead(0)){
selector=random(0,2);
if(selector==0){
mood=”sad”;
//perform sad behaviour for the robot
Serial.write(“:(“);
eyeBlinkSlow();
headShakeSlow();
melodyLow();
//this function ensures the robot does not continue to bug me once it has finished
//it’s little hello
delay(timeAfterAction);
}
if(selector==1){
mood=”neutral”;
//perform neutral behaviour for the robot
Serial.write(“:|”);
eyeBlinkSlow();
headShakeMedium();
melodyLow();
//this function ensures the robot does not continue to bug me once it has finished
//it’s little hello
delay(timeAfterAction);
}
if(selector==2){
mood=”happy”;
//perform happy behaviour for the robot
Serial.write(“:)”);
eyeBlinkFast();
headShakeFast();
melodyLow();
//this function ensures the robot does not continue to bug me once it has finished
//it’s little hello
delay(timeAfterAction);
}
//end of robot action
}
}

The other cool thing that has happened recently is I have received on loan for a year a Macbook Air from my school for learning iOS development. Aside from all the pretty Mac graphics and hardware design I think one of the main cool things about this computer is it’s flash hard drive. This means it’s boots up REALLY fast making it a much more useful device than my slower booting (but to be fair, faster overall Dell). Also it’s weight and size is quite impressive. I’m going to be pretty tempted to buy it from the school at the end of the year.

I was also asked by my iOS dev teacher to share my thoughts on objective-c. Well seeing as I really haven’t done that much in it already I thought I’d just make a meme about it instead.

Advertisements

One thought on “An update on some stuff

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