Friday, December 4, 2009

the insult machine - electronic brick version

how it works:
on the first “insult machine”, using the potentiometer (Analog 5), the user scrolls through a list of potential 2-line insults on the 16*2 LCD (BUS 2).
the button (Analog 4) is pressed once to choose an insult.
“You have chosen insult x!”
“Tilt to send”
the button (Analog 4) is pressed again and the insult is “sent” (Digital 9 - comOut) to the 2nd “insult machine” (Digital 8 - comIn)…
“INCOMING INSULT!” flashes on the LCD (BUS 2) of the second machine, interrupting the 2nd user.
the chosen insult is then displayed on the LCD (BUS 2) of 2nd machine followed by computer laughter (HA HA HA HA in random positions alternating between the first and second row of the LCD).
both users return to scrolling through their respective insults using the potentiometers (Analog 5).
i uploaded a slight variation to each machine: machine one has 10 possible send insults and ten possible receive insults, machine 2’s libraries are reversed so the insult you receive is not in the list you have been scrolling through). i change the comebackStrings[] called up in the code and upload to the 2nd machine.
two "insult machines" connectedone "insult machine"
// include the lcd library code:
#include

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 11, 12, 13, 14, 15, 16);

int tilt = 4;
int button = 4;

int comOut = 9; //initiates comunication ports at pins 8(Out) and 9(In)
int comIn = 8;

int Rot = 5;

int valRot = 0;
int valTilt = 0;
int valButton = 0;
int valNewButton = 0;
int valComeback = 0;
int valInsult = 1;
int valIn = LOW;
int comebackIn = 0;

// these constants won’t change.  But you can change the size of
// your LCD using them:
const int numRows = 2;
const int numCols = 16;

char* comebackStrings[]={“”, “Wheel turning…”, “Your village is”,
“You stand close”, “Are your parents”, “You MUST use…”, “No vaccine…”,
“Useless as…”, “No prejudice…”, “Bus leaving…”, “May your soul…”};

char* comebackStrings2[]={“”, “…hamster dead!”, “missing an idiot”,
“I hear the ocean”, “…siblings?!?”, “…birth control”, “..against stupid”,
“Popes testicles”, “…hate everyone”, “pls be under it”, “…rest in piss”};

char* comebackStrings3[]={“”, “Boring as a…”, “I dont date…”,
“Brains of 3 men”, “Does fuzzy logic”, “A closed mouth”, “Assasins do it”,
“Your gene pool”, “Death is…”, “Laugh last…”, “NO shampoo!”};

char* comebackStrings4[]={“”, “pneumatic drill”, “…other species”,
”..the 3 stooges!”, “…tickle?”, “gathers no foot”, “…from behind”,
“could use Cl2”, “hereditary”, “…think slowest”, “REAL POO!”};

void setup()
{
pinMode(comOut, OUTPUT); 
digitalWrite(comOut, LOW);
pinMode(comIn, INPUT);
lcd.begin(numRows, numCols);
Serial.begin(9600);
}

void loop()
{
lcd.clear();
delay(100);
readRot();
printComeback(); 
delay(10);
}

void readRot(){
valRot = analogRead(Rot);
delay(10);
valRot = map(valRot, 0, 1023, 1, 10);
delay(10);
checkValIn();
}

void checkButton(){
if (valButton != 1){
valButton = analogRead(button);
delay(10);
valButton = map(valButton, 0, 1023, 0, 1);
}
delay(10);
if (valButton == HIGH){
choseComeback();
}
}

void checkTilt(){
while (valTilt != 1){
valTilt = analogRead(tilt);
delay(10);
valTilt = map(valTilt, 0, 1023, 0, 1);
delay(10);
}
tiltComeback();
}

void checkValIn(){
valIn = digitalRead(comIn);
delay(10);
if (valIn == HIGH){
Serial.println(“ValIn HIGH”);
insulted();
valIn = LOW;
Serial.println(“ValIn LOW”);
}
}

void recValIn(){ 
valIn = LOW;
while (valIn == LOW){
valIn = digitalRead(comIn);
delay(10);
}
comebackIn = 0;
while (valIn != LOW){
comebackIn++;
delay(100);
valIn = digitalRead(comIn);
}
comebackIn = (comebackIn - 1);
}

void printComeback(){
while (valComeback == 0){
readRot();
lcd.clear();
lcd.print(comebackStrings[valRot]);
checkValIn();
checkButton();
delay(500);
lcd.setCursor(0, 1);
lcd.print(comebackStrings2[valRot]);
checkValIn();
checkButton();
delay(500);
checkValIn();
checkButton();
delay(500);
checkValIn();
checkButton();
delay(500);
}
}

void choseComeback(){
valComeback = valRot;
lcd.clear();
lcd.print(“You chose…”);
delay(500);
lcd.setCursor(0, 1);
lcd.print(“Insult “);
lcd.print(valRot);
lcd.print(“!”);
delay(1000);
lcd.clear();
lcd.print(“TILT to send”);
valTilt = 0;
checkTilt();
}

void tiltComeback(){
lcd.clear();
lcd.print(“TILT!”);
delay(1000);
lcd.clear();
digitalWrite(comOut, HIGH);
while (valIn != HIGH){
valIn = digitalRead(comIn);
delay(10);
}
Serial.println(“ValIn HIGH”);
digitalWrite(comOut, LOW);
Serial.println(“ComOut LOW”);
delay(1000);
digitalWrite(comOut, HIGH);
Serial.println(“ComOut HIGH”);
for (int a=0; a <= valComeback; a++){
delay(100);}
digitalWrite(comOut, LOW);
Serial.println(“ComOut LOW”);
delay(10);  
valTilt = 0;
valComeback = 0;
valButton = 0;
}

void insulted(){
digitalWrite(comOut, HIGH);
Serial.println(“ComOut HIGH”);
delay(10);
digitalWrite(comOut, LOW);
Serial.println(“ComOut LOW”);
valIn = LOW;
Serial.println(“ValIn LOW”);
recValIn();
valInsult = constrain(comebackIn, 1, 10);
lcd.clear();
lcd.print(“INCOMING INSULT!”);
delay(500);
lcd.setCursor(0, 1);
lcd.print(“INCOMING INSULT!”);
delay(500);
lcd.clear();
delay(500);
lcd.print(“INCOMING INSULT!”);
delay(500);
lcd.setCursor(0, 1);
lcd.print(“INCOMING INSULT!”);
delay(500);
lcd.clear();
lcd.print(comebackStrings3[valInsult]);
delay(500);
lcd.setCursor(0, 1);
lcd.print(comebackStrings4[valInsult]);
delay(5000);
for (int count = 0; count <= 5; count++){
lcd.clear();
lcd.setCursor(random(15), 0);
lcd.print(“HA”);
delay(200);
lcd.setCursor(random(15), 1);
lcd.print(“HA”);
delay(200);
lcd.clear();
}
}

Tuesday, December 1, 2009

playing with the arduino

so i took the responsive architectures workshops offered here a couple of weeks back.  i bought a couple of arduinos and electronic brick starter kits.  i am in the process of developing an "insult machine" (more to come...) and found myself short of pins (pins can each control something, like a button, a switch, a potentiometer (the rotary dial), etc.).  so i went to toronto and bought something called a multiplexer ($2.80), which, using 5 pins from the arduino, allows you to control up to 16 pins (basically, you can do MORE with one arduino)
to use the multiplexer i must move from the safety of the electronic bricks to the wilds of the breadboard (what is a breadboard?). ($4.95 - $9.80 depending on the size, plus $0.35 per wire - i spent about $20 on wire)

the multiplexers details are: 16ch analog, #4607 from creatron
i got one working (after about two days, some help from Brandon DeHart and Phillip Beesley)
here is a diagram, a picture and the code to blink an LED on pin y8 (pin 23 on the multiplexer).

the only downside so far is that light doesn’t blink very brightly…any suggestions?

here is a diagram and a pic and the code:



/*
16 Channel Analog Extender for Arduino
*/

// Set Analog I/O pin to use for reading channel values from multiplexer.
int ComIOpin = 6;  // HEF4067BP(X)(ICpin-01)

// Set which pins tell the multiplexer which input to read from.
int DpinA0 = 10;  // HEF4067BP Address Input A0 (A)(ICpin-10)
int DpinA1 = 11;  // HEF4067BP Address Input A1 (B)(ICpin-11)
int DpinA2 = 12;  // HEF4067BP Address Input A2 (C)(ICpin-14)
int DpinA3 = 13;  // HEF4067BP Address Input A3 (D)(ICpin-13)

void setup() {

// Set the digital pins to outputs.
pinMode(DpinA0, OUTPUT);  // HEF4067BP Address Input A = pin10
pinMode(DpinA1, OUTPUT);  // HEF4067BP Address Input B = pin11
pinMode(DpinA2, OUTPUT);  // HEF4067BP Address Input C = pin12
pinMode(DpinA3, OUTPUT);  // HEF4067BP Address Input D = pin13
Serial.begin(9600); // Set Serial COM
}

void loop() {
digitalWrite(DpinA0, LOW);
digitalWrite(DpinA1, LOW);
digitalWrite(DpinA2, LOW);
digitalWrite(DpinA3, HIGH);
analogWrite(ComIOpin, HIGH);
delay(500);
digitalWrite(DpinA0, LOW);
digitalWrite(DpinA1, LOW);
digitalWrite(DpinA2, LOW);
digitalWrite(DpinA3, HIGH);
analogWrite(ComIOpin, LOW);
delay(500);
}