*Description
I’ve made some modifications on my previous assignment to make a machine that has four different states, rather than a machine that has one state that has four different interactions. The lamp that I made uses two sensors to respond to its environment; a photocell and I/R sensor.



Sleep- The lamp is in “sleep” state when the light is on. The green light is constantly on and it will stay like this as long as the light is there. In this state, even if you come close to the I/R sensor, the lamp will not respond. The only way to wake up the lamp is to turn off the light.
Arousal- When you turn off the light and it is dark enough, the lamp is now awake and ready to respond to its environment. In this state, the yellow light fades in and out. As you come close to the I/R sensor, the light will change its color to red, entering “attract” state.
Attract- The lamp is now in “attract” state. When the I/R sensor detects an object in front of the lamp, the red light will fade in and out. If the object stays in one position for long enough, the lamp will automatically switch to “reward” state.
Reward- The lamp flashes colorful lights in “reward” state. Once the lights start flashing, the lamp is satisfied and it does not detect the proximity of the object anymore. The lamp goes back to “sleep” state when you turn on the light again. Unless that happens, it will stay “happy” and flash its lights forever.
Video clip is attached below:
http://www.youtube.com/watch?v=kocl6r1G-ig
*Code
int lightVal = 0;
int distanceVal = 0;
int lightsensor = 0;
int distancesensor = 1;
int green = 13;
int yellow = 11;
int red = 9;
int blue = 10;
int staytime = 0;
int value = 0;
int state = 1;
void setup() {
pinMode(lightsensor, INPUT);
pinMode(distancesensor, INPUT);
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
Serial.begin(9600); // Set up the serial communication.
}
void loop()
{
lightVal = analogRead(lightsensor);
distanceVal = analogRead(distancesensor);
Serial.print("Raw Sensor value");
Serial.println(lightVal);
if (state == 1) {sleep ();}
else if (state == 2) {arousal ();}
else if (state == 3) {attract ();}
else if (state == 4) {reward ();}
}
void sleep()
{
alloff();
digitalWrite(green, HIGH);
if (lightVal <= 30) {state = 2;}
if (lightVal =150) {state = 3;}
}
void arousal()
{
alloff();
for(value = 0 ; value =0; value-=5)
{analogWrite(yellow, value);
delay(20);}
if (lightVal > 30) {state = 1;}
if (distanceVal >=150) {state = 3;}
}
void attract ()
{ alloff();
for(value = 0 ; value =0; value-=5)
{analogWrite(red, value);
delay(20);}
staytime++;
if (staytime > 10) {
state = 4;
staytime = 0;
}
if (lightVal > 30) {state = 1;}
if (distanceVal 30) {state =1;}
}
void alloff()
{
digitalWrite(red,LOW);
digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(blue,LOW);
}
void flicker ()
{
digitalWrite(red, HIGH);
delay(50);
digitalWrite(red,LOW);
digitalWrite(red, HIGH);
delay(50);
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
delay(50);
digitalWrite(green,LOW);
digitalWrite(yellow,HIGH);
delay(50);
digitalWrite(yellow,LOW);
digitalWrite(blue,HIGH);
delay(15);
digitalWrite(blue,LOW);
}
void repeatflicker (int n) {
for (int i = 0; i<=n ; i++)
{
flicker();
}
}
void final()
{
digitalWrite(red, HIGH);
delay(500);
digitalWrite(green, HIGH);
delay(500);
digitalWrite(yellow,HIGH);
delay(500);
digitalWrite(blue,HIGH);
delay(500);
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(yellow,HIGH);
digitalWrite(blue,HIGH);
}