TrollCoding & Circuit

A long evening (and night) of coding and testing with iron-clad boots rampaging around in our workshop pretending to be trolls…..

Here are the code for our Troll Detector v.1.0b

// Trolldetektor
// By nullohm 2010
// Code By Mad Pro…
// Using a 3D accelerometer with analog outputs, and an atMega168pv programmed with Arduino C++ (as an Lilypad)

int x,y,z; // The axes
long ax=0,ay=0,az=0; // The averages
int tx,ty,tz;
int trollsense;
int stepcounter = 0;
int seismikk;
unsigned long firststep;
unsigned long nextstep;
unsigned long trollstep;
int stepnr;

// Troll characteristics variables
int smallest = 100;
int rimtusseImpact = 100;
int raglefantImpact = 300;
int skogstrollImpact = 500;
int rimtusseFreq = 1000;
int raglefantFreq = 3000;
int skogstrollFreq = 5000;
int rimtusseCounter = 0;
int raglefantCounter = 0;
int skogstrollCounter = 0;

// Sensitivity
int impactVariation = 20;
int freqVariation = 200;

void setup(){
Serial.begin(9600);
analogReference(EXTERNAL);
average();
}

void loop(){
trollcheck();

}

void trollcheck(){
x = analogRead(0);
delay(2);
y = analogRead(1);
delay(2);
z = analogRead(2);
delay(2);
tx = abs(ax-x);
ty = abs(ay-y);
tz = abs(az-z);
trollsense = tx+ty+tz;

seismikk = 255 – trollsense;
if(seismikk<1){
seismikk=0;
}

analogWrite(11,trollsense);

if(trollsense>smallest){
trollclassification();
}
}

void trollalert(){
Serial.println(“TROLL!!!!!”);

if(rimtusseCounter > 10){
analogWrite(3,255);
delay(10000);
analogWrite(3,0);
}

if(raglefantCounter > 10){
analogWrite(3,255);
analogWrite(9,255);
delay(10000);
analogWrite(3,0);
analogWrite(9,0);
}
if(skogstrollCounter > 10){
analogWrite(3,255);
analogWrite(9,255);
analogWrite(10,255);
delay(10000);
analogWrite(3,0);
analogWrite(9,0);
analogWrite(10,0);
}

rimtusseCounter = 0;
raglefantCounter = 0;
skogstrollCounter = 0;
}

void trollclassification(){
if(stepcounter == 0){
firststep = millis();
stepcounter = 1;
return;
}
nextstep = millis();
trollstep = nextstep – firststep;
firststep = nextstep;

// If no steps are registered for 10 seconds, reset everything
if(trollstep > 10000){
rimtusseCounter = 0;
raglefantCounter = 0;
skogstrollCounter = 0;
stepcounter = 0;
}

if(trollstep < 500 || trollstep > 5000 ){
stepcounter = 0;
return;
}

// Trying to detect the gait of the different trolls from impact and frequency

// Rimtusse
if(trollsense > (rimtusseImpact – impactVariation) && trollsense < (rimtusseImpact + impactVariation) && trollstep > (rimtusseFreq-freqVariation) && trollstep < (rimtusseFreq+freqVariation)){
rimtusseCounter = rimtusseCounter + 1;
Serial.println(“Rimtusse possibility”);
if(rimtusseCounter > 10){

trollalert();
}
}

// Raglefant
if(trollsense > (raglefantImpact – impactVariation) && trollsense < (raglefantImpact + impactVariation) && trollstep > (raglefantFreq-freqVariation) && trollstep < (raglefantFreq+freqVariation)){
raglefantCounter = raglefantCounter + 1;
Serial.println(“Raglefant possibility”);
if(raglefantCounter > 10){

trollalert();
}
}
// Skogstroll
if(trollsense > (skogstrollImpact – impactVariation) && trollsense < (skogstrollImpact + impactVariation) && trollstep > (skogstrollFreq-freqVariation) && trollstep < (skogstrollFreq+freqVariation)){
skogstrollCounter = skogstrollCounter + 1;
Serial.println(“Skogstroll possibility”);
if(skogstrollCounter > 10){

trollalert();
}
}

}

void average(){
for(int i=0;i<1000;i++){
x = analogRead(0);
delay(2);
y = analogRead(1);
delay(2);
z = analogRead(2);
delay(2);
ax = ax + x;
ay = ay + y;
az = az + z;
}
ax = ax / 1000;
ay = ay / 1000;
az = az / 1000;
Serial.println(“Average seismic in this area”);
Serial.print(ax);
Serial.print(“,”);
Serial.print(ay);
Serial.print(“,”);
Serial.println(az);

for(int i=0;i<255;i++){
analogWrite(11,i);
delay(10);
}
for(int i=255;i>0;i–){
analogWrite(11,i);
delay(10);
}
delay(1000);
}

// PS: This code may be rewritten to detect earthquakes, but trolls are worse…. alot worse….

Update to come soon: Partlists and perhaps even PCB files for you to make your own Trolldetektor1.0

UPDATE:
Circuit: