Here is the Kitteh pumpkin, also known as the Cat-O-Lantern.
It is very temperamental and has a lot to say about physical contact.
It has two individual LEDs for eyes, driven by pin 2, eight LEDs in series for the mouth driven by pin 5, and a PING ultrasonic sensor driven by pin 7.
The PING ultrasonic sensor discriminates between two states.
The ground state is when there is a distance of at least 50cm between the PING sensor and anything. In the ground state, the eyes blink randomly and the mouth pulsates slowly.
When something comes closer than 50cm, the active state takes over, making the eyes blink rapidly and the mouth pulsate fast.
It returns to normal once the distance of 50cm is restored.
[code]
//——————————————-
// Kitteh
//——————————————-
int randOn = 0;
int randOff = 0;
// int eyePins[3] = {2, 3, 4};
int eyepins = 2;
int mouthPins = 5;
int brightness = 0;
int fadeAmount = 5;
int brightnessActive = 0;
int fadeAmountActive = 5;
int time = 50;
int pingPin = 7;
// int i = 3;
/*——————————
Setup
——————————*/
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(eyepins, OUTPUT);
pinMode(5, OUTPUT);
}
/*——————————
Loop
——————————*/
void loop()
{
int cm = ping();
//PASSIVE STATE
if(cm > 50)
{
//EYES
// for(int i = 0; i <3; i++)
eyeBlink(eyepins, randOn, randOff);
//MOUTH
mouthFade(5, brightness, fadeAmount);
}
//ACTIVE STATE
else
{
//EYES
// for(int i = 0; i <3; i++)
eyeChase(eyepins, time);
//MOUTH
//mouthActive(5, brightnessActive, fadeAmountActive);
// Serial.print(i);
}
}
/*________________________________________________
Ping
________________________________________________*/
long ping()
{
int duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
return cm;
}
/*________________________________________________
MicroSeconds To Centimeters
________________________________________________*/
long microsecondsToCentimeters(int microseconds)
{
return microseconds / 29 / 2;
}
/*________________________________________________
Eye Blink
________________________________________________*/
void eyeBlink(int pin, int randOn, int randOff)
{
randOn = random (300, 6000);
randOff = random (50, 140);
digitalWrite(pin, HIGH);
Serial.print("On = ");
Serial.print(randOn);
Serial.print(", ");
delay(randOn);
digitalWrite(pin, LOW);
Serial.print("Off = ");
Serial.print(randOff);
Serial.print(", ");
delay(randOff);
Serial.println();
}
/*________________________________________________
Eye Chase
________________________________________________*/
void eyeChase(int pin, int duration)
{
digitalWrite(pin, HIGH);
delay(duration);
digitalWrite(pin, LOW);
delay(duration);
}
/*________________________________________________
Mouth Fade
________________________________________________*/
void mouthFade(int pin, int brightness, int fadeAmount)
{
analogWrite(pin, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255)
{
fadeAmount = -fadeAmount ;
}
delay(20);
}
/*________________________________________________
Mouth Active
________________________________________________*/
/*void mouthActive(int pin, int brightnessActive, int fadeAmountActive)
{
analogWrite(pin, brightnessActive);
brightnessActive = brightnessActive + fadeAmountActive;
if (brightnessActive == 0 || brightnessActive == 255)
{
fadeAmountActive = -fadeAmountActive ;
}
delay(5);
}*/
[/code]




Leave a Reply