Wednesday, November 27, 2013

most useless machine ever

I saw this funny video awhile ago and I thought it'd make a good gift.

https://www.youtube.com/watch?v=Z86V_ICUCD4

If anybody gets the courage to build one, here's a neat game I wrote based on that idea:


/**
 * my dumbox game:
 * 
 * 3 leds, 1 button, 1 servo to reset game
 * leds on pins 2, 3, 4
 * switch on pin 5
 * servo on pin 9
 * 
 * init:
 * toggle switch lights up each of the 3 lights, then begins game.
 * if game is lost, lights turn off and box resets
 * 
 * game:
 * first led blinks random times at some rate then goes to second
 * and third led, user must flip switch between last 2 leds to
 * continue to faster rate, losing resets game
 * 
 * inspired by the most useless machine ever:
 * https://www.youtube.com/watch?v=Z86V_ICUCD4
 */

#include <Servo.h> 

Servo myservo;

bool inGame;
int initStep;
bool gameInitted;
int gameRate;

const int INIT_STEP_COUNT = 3;
const int PRE_DELAY = 1000;
const int SWITCH_PIN = 5;
const int SERVO_DELAY = 700;
const int SERVO_MIN = 3;
const int SERVO_MAX = 118;
const int MIN_RANDOM = 2;
const int MAX_RANDOM = 10;
const int GAME_DELAY = 300;
const int GAME_RATE_SLOW = 1000;
const int GAME_RATE_FAST = 200;
const int GAME_SPEEDUP = 200;

void setup() 

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);

myservo.attach(9);
reset();  
}

void reset() {
inGame = false;
initStep = 0;
gameInitted = false;
gameRate = GAME_RATE_SLOW;

myservo.write(SERVO_MIN);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}

void doPreGame() {
if (digitalRead(SWITCH_PIN) == LOW) {
delay(random(PRE_DELAY));

myservo.write(SERVO_MAX);
delay(SERVO_DELAY);
myservo.write(SERVO_MIN);

if (initStep >= 0 && initStep < INIT_STEP_COUNT) {
digitalWrite(initStep + 2, HIGH);
}
initStep++;

if (initStep >= INIT_STEP_COUNT) {
inGame = true;
}
}
}

/**
 * flash lights a bit to let player know game is about to start
 */
void initGame() {
for (int i=0; i<10; i++) {
for (int j=2; j<5; j++) {
digitalWrite(j, HIGH);
delay(25);
digitalWrite(j, LOW);
delay(25);
}
}
gameInitted = true;
}

void playGame() {
//first blink random times
int rand = random(MIN_RANDOM, MAX_RANDOM);
for (int i=0; i<rand; i++) {
digitalWrite(2, HIGH);
delay(gameRate);
digitalWrite(2, LOW);
delay(gameRate);
}
if (digitalRead(SWITCH_PIN) == LOW) {
delay(GAME_DELAY);
reset();
return;
}

digitalWrite(3, HIGH);
delay(gameRate);
digitalWrite(3, LOW);
delay(gameRate);

if (digitalRead(SWITCH_PIN) != LOW) {
delay(GAME_DELAY);
reset();
return;
}

digitalWrite(4, HIGH);
delay(gameRate);
digitalWrite(4, LOW);
delay(gameRate);

gameAdvance();
}

/** game advance animation */
void gameAdvance() {
delay(GAME_DELAY);
myservo.write(SERVO_MAX);
delay(SERVO_DELAY);
myservo.write(SERVO_MIN);

gameRate -= GAME_SPEEDUP;
if (gameRate < GAME_RATE_FAST) {
gameRate = GAME_RATE_FAST;
}

for (int i=0; i<10; i++) {
for (int j=2; j<5; j++) {
digitalWrite(j, HIGH);
delay(50);
digitalWrite(j, LOW);
delay(50);
}
}
}

void loop() 
{
if (!inGame) {
doPreGame();
} else if (!gameInitted) {
initGame();
} else {
playGame();
}



Tuesday, August 27, 2013

how to fix ubuntu's dns settings

i typically use slackware and dns settings just work. whenever i try out an ubuntu vm, my dhcp-configured local dns server always partially works. for example:
$ cat /etc/resolve.conf
nameserver 192.168.1.1
search local
$ ping svnserver
PING svnserver.local (192.168.1.100) 56(84) bytes of data.
64 bytes from svn.thedeanda.com (192.168.1.100): icmp_req=1 ttl=64 time=0.627 ms
$ ping svnserver.local
ping: unknown host svnserver.local
now edit /etc/nsswitch.conf and so the hosts line looks like:
hosts: files dns
$ ping svnserver.local
PING svnserver.local (192.168.1.100) 56(84) bytes of data.
64 bytes from svnserver.local (192.168.1.100): icmp_req=1 ttl=64 time=0.271 ms
all the extra entries and the "[NOTFOUND=return]" in the middle of the line makes it give up before trying dns. i'm not sure why ubuntu does it this way. its a tricky one to find so i thought i'd share.