Assignment 6: Cat and Mouse

Assignment: DC Motor: Actuation Assignment 1

Collaborators:

Assignment: DC Motor: Actuation Assignment 1
Collaborators: karenjoy

Description

We built a carnival-type game in which players have to hit a piece of cheese with a stuffed mouse. Foxes and cats are on the revolving sticks that prevent the mouse from getting to the cheese. If the mouse gets to the cheese, he wins, and lights go off and music plays. If he lands on the mousehole, he's safe, but he doesn't win.

Components Used

  • 2 LEDs
  • 2 FSRs
  • 1 DC motor
  • 4 chopsticks
  • 1 potentiometer
  • 1 diode
  • 1 transmitter
  • 2 10K resistors
  • 2 220 resistors
  • 1 buzzer
  • a lot of wire
  • a lot of tape
  • cardboard

Arduino Code

int motorPin = 9; //DC motor pin (digital) int buzzerPin = 12; //Piezo Buzzer pin (digital) int potPin = 3; //potentiometer (analog) int led1 = 10; // LED pin (digital) int led2 = 11; // LED pin (digital) int fsr1 = 0; //FSR1 (analog) int fsr2 = 1; //FSR2 (analog) int val1 = 0; int val2 = 0; int potVal = 0; int length = 7; char winner[] = "cegCgC "; // a space represents a rest int beats[] = { 1, 1, 1, 2, 1, 4, 4 }; char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; int tempo = 100; void setup() { pinMode(potPin, INPUT); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(buzzerPin, OUTPUT); pinMode(fsr1, INPUT); pinMode(fsr2, INPUT); Serial.begin(9600); } void win(int ledPin) { potVal = 0; analogWrite(motorPin, potVal); //play tune //make ledPin blink for (int i = 0; i < length; i++) { if (winner[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(winner[i], beats[i] * tempo); analogWrite(ledPin, 255); } // pause between notes delay(tempo / 2); analogWrite(ledPin, 0); } } void playNote(char note, int duration) { // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(buzzerPin, HIGH); delayMicroseconds(tone); digitalWrite(buzzerPin, LOW); delayMicroseconds(tone); } } void loop() { potVal = analogRead(potPin); analogWrite(motorPin, potVal/10); val1 = analogRead(fsr1); Serial.println(val1); val2 = analogRead(fsr2); Serial.println(val2); if (val1 > 200 || val2 > 200) { if (val1 > 200) { Serial.println("FSR1 wins!"); win(led1); } else { Serial.println("FSR2 wins!"); win(led2); } } }

Item