I shall call him “Meat”

This is Meat. He is a bit of a nervous thing and really gets upset when the lights dim. Turn them off too quickly, and he screams. If he freaks out too much, he’s going to pass out and only a quick burst of light can wake him up.

Why is he afraid of the dark? Because there are monsters out there…

Meat gets ready for his housingThese are Meat's insides

 

 

How does his little mind work?

With very messy code.

————————————————————————————-

————————————————————————————-

// Dayturnal “meat”

// int vars
int speaker = 9;

int lastlight = 0;
int light;
int mood = 100;
boolean happy = true;
boolean scared = false;

int deathCounter;
boolean dead = false;

void setup() {
// set up pins and serial
pinMode(0, INPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}

void loop() {

// check light sensor
light = analogRead(0)*10;
// make sure that value checking changes in light is same as light on first run
// otherwise, it could falsely register a big change in light
if(lastlight == 0) lastlight = light;

// send values to serial
Serial.println(“light: “);
Serial.println(light);
Serial.println(“mood: “);
Serial.println(mood);
Serial.println();

mood = map(light, 0, 1000, 0, 1000);
// run most of the code unless creature is dead

// lazarus function
if(dead){
if(light – lastlight > 200) {
dead = false;
deathCounter = 0;
// preyRelief();
mood = 100;
lastlight = 120;

}
}

if(!dead) {

// happiness follows light level
if (mood >= 80){
happy = true;
}
if (mood < 80){
happy = false;
scared = true;
}

if(light > lastlight) {
mood+=5;
}
else if(light < lastlight) {
mood-=5;
}

// sudden drop in light, OH NO!
if(lastlight – light > 100) {
preyScream();
mood = 1;
scared = true;
}
// sudden burst of light, OH JOY!
if(light – lastlight > 100) {
preyRelief();
mood = 200;
happy = true;
}

// if too little light, creature starts dying
if(light < 50) deathCounter++;
// otherwise, creature is recovering
else deathCounter–;

// if too little light for too long, DEATH!
if(deathCounter > 20) {
preyScream();
death();
dead = true;
}

// play normal noise on each loop
preyNoise();
// use line below instead if you don’t want the flat-line noise
// when creature dies.
if(!dead) preyNoise();
}

// remember last light value to see changes over time
lastlight = light;
}

// standard creature noise, dependent on mood level
void preyNoise() {

if(happy){
int basetone   = map(mood, 0, 800, 150, 1000);
int basetempo  = map(mood, 0, 800, 20, 600);

//3 notes
tone(speaker, basetone);
delay(basetempo);
noTone(speaker);
delay(basetempo);

tone(speaker, basetone*1.26);
delay(basetempo);
noTone(speaker);
delay(basetempo );

tone(speaker, basetone*1.5);
delay(basetempo);
noTone(speaker);
delay(basetempo);
delay(basetempo*2);
}
else if (scared){
int basetone   = map(mood, 0, 800, 150, 1000);
int basetempo  = map(mood, 0, 800, 10, 600);
basetempo      = basetempo – mood;

//3 notes scared
tone(speaker, basetone);
delay(basetempo);
noTone(speaker);
delay(basetempo / 5);

tone(speaker, basetone);
delay(basetempo);
noTone(speaker);
delay(basetempo / 5);

tone(speaker, basetone*1.6);
delay(basetempo);
noTone(speaker);
delay(basetempo / 5);
delay(basetempo*2);

}

}

// noise when too much light comes abruptly
void preyScream() {
int freq = 2100;
for(int x=0; x < 3000; x++) {
tone(speaker, freq);
delay(2);
if (freq < 2800){
freq++;
}
else {
freq=3000;
}
}
}

// noise when light comes back abruptly.
void preyRelief() {

}

// sound which occurs when creature dies
void death() {
tone(speaker, 200);
delay(40);

}

 

 

————————————————————————————–

————————————————————————————–