Rock-Paper-Scissors with Netduino and Arduino


Been constantly persuaded by my 6 years old daughter to play Rock-Paper-Scissors I decided that we can make it a bit more interesting, so I’ve put following schematics on the prototyping shield:

Rock-Paper-Scissors

Netduino and Arduino are pin compatible, but the output voltages are different: 3.3V for Netduino and 5V for Arduiono. For current limiting, I’ve selected 150Ohm resistor to avoid changing the elements, when change the platform. The 10K pull-down resistors assure false state on the digital inputs until button is pressed.

I’ve tested it with Netduino Plus, Arduino Uno and Shrimpuino (homebrewed Arduino Uno analog).

Netduino:

using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace RockPaperScissors
{
	public class Program
	{
		private static OutputPort redLed = new OutputPort(Pins.GPIO_PIN_D11, false);
		private static OutputPort greenLed = new OutputPort(Pins.GPIO_PIN_D12, false);
		private static OutputPort blueLed = new OutputPort(Pins.GPIO_PIN_D13, false);

		private static InputPort rockButton = new InputPort(Pins.GPIO_PIN_D10, true, ResistorModes.Disabled);
		private static InputPort paperButton = new InputPort(Pins.GPIO_PIN_D6, true, ResistorModes.Disabled);
		private static InputPort scissorsButton = new InputPort(Pins.GPIO_PIN_D2, true, ResistorModes.Disabled);

		private static InputPort stopButton = new InputPort(Pins.ONBOARD_SW1, true, Port.ResistorMode.Disabled);

		private enum Choice
		{
			None,
			Rock,
			Paper,
			Scissors
		}

		private enum Player
		{
			None,
			First,
			Second
		}

		public static void Main()
		{
			while (stopButton.Read())
			{
				AllLedsOff();
				IntroForPlayer(Player.First);
				Choice firstPlayer = GetChoice();

				AllLedsOff();
				IntroForPlayer(Player.Second);
				Choice secondPlayer = GetChoice();

				AllLedsOff();
				EvaluateAndShowResult(firstPlayer, secondPlayer);

				Thread.Sleep(2000);
			}
		}

		private static void IntroForPlayer(Player player)
		{
			switch (player)
			{
				case Player.First:
					greenLed.Write(true);
					break;
				case Player.Second:
					redLed.Write(true);
					break;
			}
		}

		private static Choice GetChoice()
		{
			var choice = Choice.None;
			while (choice == Choice.None)
			{
				if (rockButton.Read())
				{
					choice = Choice.Rock;
				}

				if (paperButton.Read())
				{
					choice = Choice.Paper;
				}

				if (scissorsButton.Read())
				{
					choice = Choice.Scissors;
				}
			}

			Thread.Sleep(500);
			return choice;
		}

		private static void EvaluateAndShowResult(Choice firstPlayerChoice, Choice secondPlayerChoice)
		{
			if (firstPlayerChoice == secondPlayerChoice)
			{
				ShowWinner(Player.None);
				return;
			}

			if ((firstPlayerChoice == Choice.Rock) && (secondPlayerChoice == Choice.Scissors) ||
				(firstPlayerChoice == Choice.Paper) && (secondPlayerChoice == Choice.Rock) ||
				(firstPlayerChoice == Choice.Scissors) && (secondPlayerChoice == Choice.Paper))
			{
				ShowWinner(Player.First);
			}
			else
			{
				ShowWinner(Player.Second);
			}
		}

		private static void ShowWinner(Player player)
		{
			for (var i = 0; i < 8; i++)
			{
				if (player == Player.None)
				{
					blueLed.Write(true);
				}
				else
				{
					IntroForPlayer(player);
				}

				Thread.Sleep(200);
				AllLedsOff();
				Thread.Sleep(100);
			}
		}

		private static void AllLedsOff()
		{
			redLed.Write(false);
			greenLed.Write(false);
			blueLed.Write(false);
		}
	}
}

Arduino:

int redLed = 11;
int greenLed = 12;
int blueLed = 13;

int rockButton = 10;
int paperButton = 6;
int scissorsButton = 2;

int choiceNone = 0;
int choiceRock = 1;
int choicePaper = 2;
int choiceScissors = 3;

int playerNone = 0;
int playerFirst = 1;
int playerSecond = 2;

void AllLedsOff() {
	digitalWrite(redLed, false);
	digitalWrite(greenLed, false);
	digitalWrite(blueLed, false);
}

void ShowWinner(int player) {
	for (int i = 0; i < 8; i++) {
		if (player == playerNone) {
			digitalWrite(blueLed, true);
		} else {
			IntroForPlayer(player);
		}
		delay(200);
		AllLedsOff();
		delay(100);
	}
}

int GetChoice() {
	int choice = choiceNone;
	while (choice == choiceNone) {
		if (digitalRead(rockButton) == HIGH) {
			choice = choiceRock;
		}
		if (digitalRead(paperButton) == HIGH) {
			choice = choicePaper;
		}
		if (digitalRead(scissorsButton) == HIGH) {
			choice = choiceScissors;
		}
		delay(10);
	}
	delay(500);
	return choice;
}

void IntroForPlayer(int player) {
	if (player == playerFirst) {
		digitalWrite(greenLed, true);
	} else if (player == playerSecond) {
		digitalWrite(redLed, true);
	}
}

void EvaluateAndShowResult(int firstPlayerChoice, int secondPlayerChoice) {
	if (firstPlayerChoice == secondPlayerChoice) {
		ShowWinner(playerNone);
		return;
	}
	if ((firstPlayerChoice == choiceRock) && (secondPlayerChoice == choiceScissors) ||
	    (firstPlayerChoice == choicePaper) && (secondPlayerChoice == choiceRock) ||
	    (firstPlayerChoice == choiceScissors) && (secondPlayerChoice == choicePaper)) {
		ShowWinner(playerFirst);
	} else {
		ShowWinner(playerSecond);
	}
}

void setup() {
	pinMode(redLed, OUTPUT);
	pinMode(greenLed, OUTPUT);
	pinMode(blueLed, OUTPUT);

	pinMode(rockButton, INPUT);
	pinMode(paperButton, INPUT);
	pinMode(scissorsButton, INPUT);
}

void loop() {
	while(true){
		AllLedsOff();
		IntroForPlayer(playerFirst);
		int firstPlayer = GetChoice();

		AllLedsOff();
		IntroForPlayer(playerSecond);
		int secondPlayer = GetChoice();

		AllLedsOff();
		EvaluateAndShowResult(firstPlayer, secondPlayer);

		delay(2000);
	}
}

What the code is doing and how to play:
After MCU initialise, RGB LED becomes steady GREEN thus indicating first player’s move. In secret from the other player, press the chosen button (from left to right in order: ROCK, PAPER, SCISSORS). RGB LED becomes steady RED and now this is the second player’s turn (at that stage of the game, the player may not press the button in secret any more). After the second player’s choice, the RGB LED will flash with the winners colour: GREEN (first player), RED (second player) and BLUE for TIE. Few seconds later, game restarts and you can play again.

Enjoy.

My late night project: The Shrimp + Arduino = Shrimpuino :)


All started with having blank ATMega328P-PU. The idea was to use it with CP2102 USB UART module to build The Shrimp but having seen also Arduino on breadboard I decide to combining the two and the result is:

When uploading applications using the USB UART, connect the +Vcc to USB UART (yellow jumpwire on 28th position) and disconnect the power supply

DSCF4374 DSCF4375

When running the uploaded application, unplug the yellow jump wire on 28th position and plug the power (of course if you wish you can use the USB as power, then just do not disconnect it):

DSCF4369 DSCF4370 DSCF4373

In this case, the uploaded application is Arduino’s example Blink. It blinks the LED connected to Arduino’s pin 13 which corresponds to pin 19 on ATMega328P.

High-performance monolithic graphene transistors created


What wonderful news! For more details visit this page.

Read Netduino for beginners tutorial online on this blog


Hi guys,

You can now read the tutorial directly here! Just visit/navigate to this page.

Improving slowly but surely 🙂

Cheers

CircuitLab


John Kerr just pointed to me a good new addition to the list of circuit diagramming toolsCircuitLab.

Netduino for beginners tutorial release v1.11


Hi guys,

I’ve done it again! New version of the tutorial (v1.11) is here and can be downloaded from Box section on the right, Netduino’s wiki page or read it online here.

Enjoy!

Scheme-It


Scheme-It is my new addition to previously posted list of circuit diagramming tools.

Read the documentation and watch this video tutorial.