Daan Pape
Published © MIT

Simon Says with DPT-Board

Get to know your DPT-Board and make the fun Simon Says game. You will learn some basic hardware and Javascript

BeginnerFull instructions provided3 hours524
Simon Says with DPT-Board

Things used in this project

Hardware components

DPTechnics DPT-Board
The DPTechnics IoT-development board
×1
5mm LED
Standard 5mm LED in any color you like
×4
1K resistor
250mW 1k resistor for the LEDs
×4
Tactile switch
Input switches for the game
×4
Breadboard (generic)
Breadboard (generic)
Standard breadboard
×1
Jumper wires
×17

Story

Read more

Schematics

Full schematic

The complete Simon Says schematic

LED connection

This is how you connect the LED's to the DPT-Board

Code

Simon Says for the DPT-Board

JavaScript
Just copy this code into the DPT-Web IDE and start playing
/**
 * Simon Says for the DPT-Board
 * Date: 14 october 2015
 */

// translating to hardware pins. ex: [23, 22, 1, ...]
var output = [6, 7, 16, 8];
var input = [12, 13, 15, 19];

// length of current run
var currLength = 2;

// generated series
var serie = [];

// read series
var read = [];

function setLength(nr) {
	currLength = nr;
}

function isCorrect() {
	print("is " + serie + " === " + read);
	for (var i=0; i < serie.length; i++) {
		if (serie[i] !== read[i]) {
			return false;
		}
	}
	return true;
}

function doWon() {
	setLength(currLength+1);
	print("-- won -- next level " + currLength);
	digitalWrite(output[0], 1);
	digitalWrite(output[1], 1);
	digitalWrite(output[2], 1);
	digitalWrite(output[3], 1);
	setTimeout(restart, 2000);
}

function doLost() {
	setLength(2);
	print("-- lost -- going back to level " + currLength);
	digitalWrite(output[0], 1);
	digitalWrite(output[1], 0);
	digitalWrite(output[2], 0);
	digitalWrite(output[3], 1);
	setTimeout(restart, 1500);
}

function restart() {
	digitalWrite(output[0], 0);
	digitalWrite(output[1], 0);
	digitalWrite(output[2], 0);
	digitalWrite(output[3], 0);
	setTimeout(playRound, 1500);
}

function nextClick(nr) {
	print("button: " + nr);
	read.push(nr);
	if (serie.length === read.length) {
		print("finished run of " + read.length);

		if (isCorrect()) {
			doWon();
		} else {
			doLost();
		}
	}
}

function randPin() {
	var x = Math.random();
	if (x < 0.25) return 0;
	if (x < 0.50) return 1;
	if (x < 0.75) return 2;
	if (x < 1.00) return 3;
}

// call function "button" with button-nr and down/up only once.
// default state = 1
var states = [1,1,1,1];

setInterval(function buttonReader() {
	for(var i=0; i < input.length; i++) {
	var curr = digitalRead(input[i]);
		if (curr !== states[i]) {
			if ((curr === 0)) {
				nextClick(i);
			}
			states[i] = curr;
		}
	}
}, 20);

function playRound() {
	serie = [];
	var nr = 0;

	function doOn() {
		digitalWrite(output[serie[nr]], 1);
		setTimeout(doOff, 600);
	}

	function doOff() {
		digitalWrite(output[serie[nr]], 0);
		nr += 1;
		if (nr < currLength) {
			setTimeout(doOn, 333);
		} else {
			read = [];
		}
	}

	// make random serie
	for (var i=0; i<currLength; i++) {
		serie.push(randPin());
	}
	print("to guess: " + serie);
	doOn();
}

setLength(2);
playRound();

Credits

Daan Pape

Daan Pape

1 project • 0 followers
I'm the founder of www.dptechnics.com and www.bluecherry.io

Comments