Description:
The goal of the assignment was to design an artifact that showed input/output coincidence.
I tried to create a simple “Simon Says” emulator. Basically the user will enter a combination of key presses with commensurate light blink and buzzer sound for a set duration. After a while, the device will play back the sequence.
Step 1: Trigger Record/Playback State
The device uses a potentiometer to trigger the “recording state”. When turned all the way, the device initiates a loop that sets a record and playback state. The device will continually loop through the record/playback sequence until the potentiometer is turned back to the off state.
Step 2: Recording State
The device uses three colors of LEDs to indicate different states. The device does a countdown to the recording state using the red LED. It blinks down to trigger the active recording state which is indicated by the green LED shining. During the recording state, when the user presses the force sensor, the blue LED blinks and the buzzer sounds. When the recording state ends, the green LED stops shining and blinks rapidly three times.
Step 3: Playback State
In the playback state, the device will play back the inputted sequence. As it plays back, the device shines the three LEDs in succession and buzzes as commensurate.
The hardest part of the program was figuring out how to record a sequence of variable analog inputs from the force sensor. I designed a buffer that essentially polls the force sensor at pre-set intervals. By making the duration between polls shorter (25ms), the accuracy of the measurement improves. The buffer only stores two states: 1 (force triggered) and 0 (no force triggered).
Sample video: http://youtu.be/ZWKE1XkFK00
Components Used:
1- Breadboard
1- Arduino Uno
1 LEDs (Blue, Green, Red)
1 USB interface cable to Arduino
1 220 Ohm Resistors
1 Force sensitive resistor
1 Piezo Buzzer
1 Potentiometer
2 10k Ohm resistor
Code
/*
* Simple Simon Says game captures the key presses from a user with accompanying buzzer. It saves the input and plays back.
* A potentiometer turns on the recording sequence.
* Recording sequence starts with the red LED counting down.
* When recording the green LED shines. When recording, each key press triggers the buzzer and blue LED
* When recording stops, the green light stops.
*/
int sensorPin = 0; // select the input pin for the sensor
int ledPinRed = 11; // select the output pin for the RED LED
int ledPinGreen = 10; // select the output pin for Green LED
int ledPinBlue = 9; // select the output pin for Green LED
int val = 0; // variable to store the value coming from the sensor
int potPin = 1; // set pin for potentiometer
char analogInput[200]; // Array will store key input presses with 1 equal pressed and 0 equal not pressed
int serByte = -1;
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
int timeAccuracy = 25; // time interval to measure force input in milliseconds
// note names and their corresponding half-periods
int speakerPin = 7;
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
void setup() {
//pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
Serial.println("ready");
}
void loop() {
val = analogRead(sensorPin); // read the value from the sensor, 0-1023
// Serial.println(val); // writing the value to the PC via serial connection
// Zero out serial input and analog input buffers
memset(serInString, -1, 100);
memset(analogInput, -1, sizeof(analogInput));
readSerialString(serInString); //
serByte = serInString[0];
// Test code
int readForce = analogRead(sensorPin);
analogWrite(ledPinBlue, readForce/4);
if(readForce>50){
//playNote('c', 25);
}
// See if a character is entered in the serial console to trigger record sequence
if (serByte!=-1 || analogRead(potPin) > 1000){ // recording sequence triggers with potentio meter turned
//printInput(analogInput);// Print
Serial.println("Capturing input. Countdown in");
blink(ledPinRed,1, 1500);
//Print out count down to recording start
for (int counter = 5; counter > 0; counter--){
Serial.print(">>>");
Serial.println(counter);
blink(ledPinRed, 1, 50);
delay(1000);
}
analogWrite(ledPinRed, 0);
// Begin Recording
Serial.println("Start!!");
analogWrite(ledPinGreen,175);
for (int counter = 0; counter < sizeof(analogInput); counter ++){
int readForce = analogRead(sensorPin); // read touch sesnsor
analogWrite(ledPinBlue, readForce/4);
if( readForce >100){ // if sensor value large enough store in array
analogInput[counter] = char(1);
playNote('c',timeAccuracy); // play sound to acknowledge key press
}else{
analogInput[counter]=char(0);
}
delay(timeAccuracy);
}
analogWrite(ledPinBlue, 0);
analogWrite(ledPinGreen,0);
Serial.println("End!!!");
// printInput(analogInput);// Print
// show sequence in blinking LED
Serial.println("Start Blink");
blink(ledPinGreen, 8, 20);
delay(2000);
Serial.println("Now");
for (int counter =0; counter < sizeof(analogInput); counter++){
int blinkValue = analogInput[counter];
if( blinkValue >0){
analogWrite(ledPinBlue, 200);
analogWrite(ledPinRed, 200);
analogWrite(ledPinGreen, 200);
playNote('c',timeAccuracy);
} else{
analogWrite(ledPinBlue, 0);
analogWrite(ledPinRed, 0);
analogWrite(ledPinGreen, 0);
}
delay(timeAccuracy);
}
}//end of if() recording session
delay(50); // rest a little...
}
// simple function takes a pin and triggers blinking sequence
void blink(int ledPin, int intervals, int duration ){
for(int counter = 0; counter < intervals; counter ++){
analogWrite(ledPin, 175);
delay(duration);
analogWrite(ledPin, 0);
delay(100);
}
}
// prints the analog input buffer
void printInput(char *intArray){
Serial.println("Values for input");
for( int counter = 0; counter <sizeof(analogInput); counter ++){
Serial.print(int(analogInput[counter]));
}
Serial.println();
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
//plays a note for cycle
void playNote(int serByte, int cycles){
for (int count=0;count<=8;count++) { // look for the note
if (names[count] == serByte) { // ahh, found it
for( int i=0; i<cycles; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
}
}
}
}
- Login to post comments