So here is the accelerometer toy. This toy exhibits 4 states. Off, attract, play, and angry. Basically, you use a potentiometer switch to turn it on. Once it is on, it goes into attract mode and blinks a blue light. Next, if the accelerometer detects movement, it will go into play mode, and an orange led will light up. And if the accelerometer detects too much movement, it will go into angry mode, where it will light up a red light and set a buzzer off denoting to stop playing so hard.
State machine toy diagram- Circuit Diagram for the final set up
SM Toy BB set up- Picture of the final bread board set up
Code:
int statusPin = 13; // LED connected to digital pin 13
int xPin = 3; // X input from accelerometer
int yPin = 4; // Y input from accelerometer
int zPin = 5; // Z input from accelerometer
int redPin = 9; // Red LED's connected to pin 6
int orangePin = 10; // Orange LED's connected to pin 7
int bluePin = 11; // Blue LED's connected to pin 5
int buzzPin = 12; // buzzer connected to pin 12
int potPin = 2; // potentiometer connected to pin 2
int xval;
int yval;
int zval;
int potval;
int val;
void setup() // run once, when the sketch starts
{
pinMode(statusPin, OUTPUT); // sets the digital pin as output
pinMode(redPin, OUTPUT); // sets the digital pin as output
pinMode(orangePin, OUTPUT); // sets the digital pin as output
pinMode(bluePin,OUTPUT); // sets the digital pin as output
pinMode(buzzPin,OUTPUT); // sets the digital pin as output
pinMode(xPin, INPUT); // sets the analog pin as input
pinMode(yPin, INPUT); // sets the analog pin as input
pinMode(zPin, INPUT); // sets the analog pin as input
pinMode(potPin, INPUT); //sets the analog pin as input
Serial.begin(9600);
}
void loop() // run over and over again
{
xval = analogRead(xPin); // set xval egual to the xPin reading
yval = analogRead(yPin); // set yval equal to the yPin reading
zval = analogRead(zPin); // set zval equal to the zPin reading
potval = analogRead(potPin); // set potval equalt to the potentiometer reading
Serial.print("X Value: ");
Serial.println(xval);
Serial.print("Y Value: ");
Serial.println(yval);
Serial.print("Z Value: ");
Serial.println(zval);
Serial.print("Potentiometer:");
Serial.println(potval);
digitalWrite(statusPin, HIGH);
delay(15);
digitalWrite(statusPin, LOW);
delay(15);
if ( potval > 1000){
//ATTRACT
if ( (xval < 560) && (yval < 560) && (zval < 560)) // if x-y-z values are
{
digitalWrite(bluePin,HIGH);
digitalWrite(orangePin,LOW);
digitalWrite(redPin,LOW);
digitalWrite(buzzPin,LOW);
Serial.print("Attract: Come Play With ME!!");
}
//PLAY
if ( (xval > 560) && (xval < 660)) // if x-y-z values are
{
digitalWrite(orangePin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(redPin,LOW);
digitalWrite(buzzPin,LOW);
Serial.print("Play Play Play!!");
}
else {
if ( (yval > 560) && (yval < 660))
{
digitalWrite(orangePin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(redPin,LOW);
digitalWrite(buzzPin,LOW);
Serial.print("Play Play Play!!");
}
else {
if ( (zval > 560) && (zval < 660))
{
digitalWrite(orangePin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(redPin,LOW);
digitalWrite(buzzPin,LOW);
Serial.print("Play Play Play!!");
}
}
}
//ANGRY
if (xval>700) {
digitalWrite(redPin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(orangePin,LOW);
digitalWrite(buzzPin,HIGH);
Serial.print("STOP IT!!");
}
else {
if (yval > 700)
{
digitalWrite(redPin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(orangePin,LOW);
digitalWrite(buzzPin,HIGH);
Serial.print("STOP IT!!");
}
else{
if (zval > 700)
{
digitalWrite(redPin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(orangePin,LOW);
digitalWrite(buzzPin,HIGH);
Serial.print("STOP IT!!");
}
}
}
}
//OFF
else {
digitalWrite(redPin,LOW);
digitalWrite(bluePin,LOW);
digitalWrite(orangePin,LOW);
digitalWrite(buzzPin,LOW);
}
}