Won the ArduCapSense shield last year in a contest and found some time to write a simple Simon Game for it to test it. There are still a few TODO's but too busy with other thingies so I thought => publish . Use at own risk, Simon can be quite addictive

//
// FILE: ArduCapSimon.pde
// AUTHOR: Rob Tillaart
// VERSION: 0.1.03
// PURPOSE: Simon Game for ArduCapSense shield - Arduino
//
// HISTORY:
// 0.1.00 - 2011-04-12 initial serial version of Simon Game
// 0.1.01 - 2011-04-14 added LED feedback
// 0.1.02 - 2011-04-14 added readKey - bug in returned values from shield :(
// 0.1.03 - 2011-04-18 tweaked autocallibrate value to 10 seconds
//
// TODO
// + add audio
// + add random seed (but for testing its nice to know the sequences :)
//
// Released to the public domain
//
// All disclaimers apply use at own risk
//
#include <CapSense.h>
// #include "pitches.h"
CapSense cs1 = CapSense(10,9);
CapSense cs2 = CapSense(10,8);
CapSense cs3 = CapSense(10,7);
CapSense cs4 = CapSense(10,6);
CapSense cs5 = CapSense(10,5);
CapSense cs6 = CapSense(10,4);
CapSense cs7 = CapSense(10,3);
CapSense cs8 = CapSense(10,2);
#define CAPSENSE_THRESHOLD 400
// TODO SHOULD BE IN CAPSENSE.H
#define LED1 16
#define LED2 15
#define LED3 17
#define LED4 13
#define LED5 18
#define LED6 12
#define LED7 19
#define LED8 11
#define AUDIO_OUT 14 // audio out pin
//int notes[] = {
// NOTE_C4,
// NOTE_D4,
// NOTE_E4,
// NOTE_F4,
// NOTE_G4,
// NOTE_A4,
// NOTE_B4,
// NOTE_C5 };
int ledArray[8] = {
LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8 };
long total[8];
void setup()
{
Serial.begin(115200);
// cs1.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1
cs1.set_CS_AutocaL_Millis(20);
cs2.set_CS_AutocaL_Millis(20);
cs3.set_CS_AutocaL_Millis(20);
cs4.set_CS_AutocaL_Millis(20);
cs5.set_CS_AutocaL_Millis(20);
cs6.set_CS_AutocaL_Millis(20);
cs7.set_CS_AutocaL_Millis(20);
cs8.set_CS_AutocaL_Millis(20);
// INIT AUDIO
pinMode(AUDIO_OUT, OUTPUT);
// INIT LEDS
for (int i=0; i< 8; i++) pinMode(ledArray[i], OUTPUT);
for (int i =0; i< 8; i++) digitalWrite(ledArray[i], LOW);
// SEED RANDOM GENERATOR
// TODO
}
void loop()
{
playSimon();
}
#define MAXSIMON 100
int level = 1;
int delayDividor = 1; // TODO so higher levels go faster ...
byte simonArray[MAXSIMON];
byte playerArray[MAXSIMON];
void playSimon()
{
// FORCE RECALLIBRATION EVERY LEVLEL
cs1.reset_CS_AutoCal();
cs2.reset_CS_AutoCal();
cs3.reset_CS_AutoCal();
cs4.reset_CS_AutoCal();
cs5.reset_CS_AutoCal();
cs6.reset_CS_AutoCal();
cs7.reset_CS_AutoCal();
cs8.reset_CS_AutoCal();
// START
Serial.print("SIMON GAME, level : ");
Serial.println(level);
// animation
// for (int i=8; i> 0; i--)
// {
// int led = ledArray[i-1];
// digitalWrite(led, HIGH);
// delay(100);
// digitalWrite(led, LOW);
// delay(100);
// Serial.print(i, DEC);
// }
// Serial.println();
// delay(500);
// SHOW LEVEL by LEDS
for (int i=1; i<=level && i<=8; i++)
{
digitalWrite(ledArray[i-1], HIGH);
}
delay(1000);
for (int i=1; i<=level && i<=8; i++)
{
digitalWrite(ledArray[i-1], LOW);
}
delay(1000);
// GENERATE SIMONARRAY
int rnd = 4 + level/3;
rnd = constrain(rnd, 4, 8);
for (int i=0; i< level; i++)
{
simonArray[i] = random(rnd) + 1;
}
// PLAY SIMONARRAY
Serial.println("listen carefully");
int del = 510 - level * 10;
del = constrain(del, 100, 500); // level 30 - level 1
for (int i=0; i< level; i++)
{
int led = ledArray[simonArray[i]-1];
digitalWrite(led, HIGH);
delay(del);
digitalWrite(led, LOW);
delay(100);
Serial.println(simonArray[i], DEC);
}
// PLAYERS ARRAY
while(Serial.available()) Serial.read(); // flush serial buffer
Serial.println("now it's your turn");
for (int i=0; i< level; i++)
{
int x = readKey();
Serial.print("X : ");
Serial.println(x);
playerArray[i] = x;
//while(Serial.available() < 1);
//playerArray[i] = Serial.read() - '0';
int led = ledArray[playerArray[i]-1];
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(100);
}
// COMPARE AND INCREASE / DECREASE LEVEL
bool OK = true;
for (int i=0; i< level && OK; i++)
{
OK = (simonArray[i] == playerArray[i]);
}
if (OK) level++;
else level--;
level = constrain(level, 1, MAXSIMON);
// TODO COMPARE TO HIGH SCORE (eeprom?)
}
int readKey()
{
int highestTotalIndex = 0;
do
{
total[0] = cs1.capSense(30);
total[1] = cs2.capSense(30);
total[2] = cs3.capSense(30);
total[3] = cs4.capSense(30);
total[4] = cs5.capSense(30);
total[5] = cs6.capSense(30);
total[6] = cs7.capSense(30);
total[7] = cs8.capSense(30);
highestTotalIndex = 0;
for(int i = 1; i < 8; i++)
{
if (total[i] > total[highestTotalIndex])
{
highestTotalIndex = i;
}
}
}
while (total[highestTotalIndex] < CAPSENSE_THRESHOLD);
Serial.print("highest : ");
Serial.println(total[highestTotalIndex]);
// TODO recallibrate
return highestTotalIndex+1;
}
// END OF FILE