Wednesday, July 30, 2008

Source code for evil teddy

#include <Wire.h>
#include <nunchuck_funcs.h>
#include <AFMotor.h>

AF_DCMotor armNeckMotor2(3, MOTOR12_64KHZ);
AF_DCMotor armNeckMotor1(2, MOTOR12_64KHZ);
AF_DCMotor eyesMouthMotor(1, MOTOR12_64KHZ);

int loop_cnt = 0;
float accelx,accely,accelz,joyx,joyy;
byte zbut,cbut;
int speakerPin = 2;
int lightPin = 14;

//These are to detect if buttons are pressed in rapid succession
int sequenceCount1 = 0;
int start1 = false;


void setup() {

Serial.begin(9600);

nunchuck_init_with_power();

eyesMouthMotor.setSpeed(200);
armNeckMotor1.setSpeed(200);
armNeckMotor2.setSpeed(200);

pinMode(speakerPin,OUTPUT);
pinMode(lightPin,OUTPUT);
}

void special()
{

digitalWrite(speakerPin, HIGH);
delay(50);
digitalWrite(speakerPin,LOW);

digitalWrite(lightPin,HIGH);

for (int i = 0; i < 15; i++)
{

eyesMouthMotor.run(BACKWARD);
delay(random(150,250));
eyesMouthMotor.run(FORWARD);
delay(random(75,125));
eyesMouthMotor.run(RELEASE);

}
delay(1000);
digitalWrite(lightPin,LOW);
}

void startTimer()
{
start1 = true;
sequenceCount1 = 0;
}

void stopTimer()
{
start1 = false;
sequenceCount1 = 0;
}


void loop() {
if (loop_cnt>100)
{
if (start1==true)
{
sequenceCount1++;

}

if (sequenceCount1>5)
{
stopTimer();
}

loop_cnt=0;

nunchuck_get_data();

accelx = nunchuck_accelx(); // ranges from approx 70 - 182


accely = nunchuck_accely(); // ranges from approx 65 - 173


accelz = nunchuck_accelz();

// 1 pressed, 0 free
zbut = nunchuck_zbutton();

// 1 pressed, 0 free
cbut = nunchuck_cbutton();

// 23<--125-->227
joyx = nunchuck_joyx();


// 227
// ^
//130
// v
//31
joyy = nunchuck_joyy();

if (cbut)
{
eyesMouthMotor.run(FORWARD);

startTimer();
}
else
{
eyesMouthMotor.run(RELEASE);
}
if (zbut)
{


if (sequenceCount1 > 1 && sequenceCount1 < 5)
{
special();
stopTimer();
}
else
{
eyesMouthMotor.run(BACKWARD);
delay(150);
eyesMouthMotor.run(FORWARD);
}
}

if (joyx>150 || joyx<100)
{

if (joyx>150)
{

armNeckMotor1.run(BACKWARD);

}
else
{
armNeckMotor1.run(RELEASE);
}


if (joyx<100)
{

armNeckMotor2.run(BACKWARD);

}
else
{
armNeckMotor2.run(RELEASE);
}

}
else if (joyy>150)
{


armNeckMotor1.run(BACKWARD);
armNeckMotor2.run(BACKWARD);

}
else if (accelx>160)
{
armNeckMotor1.run(FORWARD);
}
else if (accely>160)
{
armNeckMotor2.run(FORWARD);
}
else
{
armNeckMotor1.run(RELEASE);
armNeckMotor2.run(RELEASE);
}

//Serial.println(accelx,DEC);
//Serial.println(accely,DEC);
// Serial.println(accelz,DEC);
// Serial.println(joyx,DEC);
// Serial.println(joyy,DEC);
// Serial.println(zbut,DEC);
// Serial.println(cbut,DEC);


}
loop_cnt++;
delay(1);
}

0 Comments:

Post a Comment

<< Home