Created
November 2, 2010 21:35
-
-
Save thedod/660336 to your computer and use it in GitHub Desktop.
Simplest Simon - my first Arduino sketch :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simplest Simon - a game for a bare naked Arduino | |
Recently I've got my first Arduino ever, and I'm still waiting for my order of | |
breadboard, jumpers, resistors, etc. (where I live, such things aren't common). | |
This led (pun intended) to the question "What shall we do with a bare Arduino", | |
and the unavoidable answer: "Something with the pin 13 on-board LED". | |
So there you have it. A Simon-like game where the Arduino flashes 8 bits on the LED | |
(steady meand 1, blinking means 0), and then you have to send a sequence of 0/1s | |
via the Serial Monitor and get a yay or nay. | |
For a "real" Simon game (with sound and all, like the 80s toy), see | |
http://www.fuzzywobble.com/VIEWproject.php?id=21 | |
created 2 Nov. 2010 | |
by @TheRealDod | |
*/ | |
const int LEDPIN = 13; // On-board LED | |
const int BLINKPERIOD = 50; // Millis. 50 is quite easy, 30 is pretty fast | |
const int SERIALTIMEOUT = 2000; // If user sends less than 8 bytes | |
void setup() { | |
// Analog in 0 should *not* be connected. | |
// It's mama's little PRNG :) | |
randomSeed(analogRead(0)); | |
pinMode(LEDPIN, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
Serial.println("To play - send anything, then watch the LED..."); | |
Serial.flush(); | |
while (Serial.available() <= 0) { | |
delay(100); | |
} | |
Serial.flush(); | |
delay(500); | |
byte simonBits = random(256); | |
bits2Led(simonBits); | |
Serial.println("Now send what you saw in 8 digits (0=blink, 1=steady)."); | |
byte guess = serial2Bits(); | |
if (guess==simonBits) { | |
Serial.print("Yay! It's "); | |
} | |
else { | |
Serial.print("It's not "); | |
bits2Serial(guess); | |
Serial.print(". It's "); | |
} | |
bits2Serial(simonBits); | |
Serial.println('.'); | |
} | |
void bits2Serial(byte val) { | |
for (int b=0 ; b<8 ; b++,val<<=1) { | |
Serial.print((val&128)>0,BIN); | |
} | |
} | |
byte serial2Bits() { | |
// Get 8 bits from the serial as a sequence of ASCII 0/1s | |
// Timeout pads with leading zeros | |
byte res = 0; | |
Serial.flush(); | |
// Wait for first byte | |
while (Serial.available() <= 0) { | |
delay(100); | |
} | |
unsigned long starttime=millis(); | |
for (int i=0 ; i<8 ; i++) { | |
if (Serial.available()) { | |
byte inByte=Serial.read(); | |
res <<= 1; | |
res |= inByte&1; // Checking even/odd works for 0/1, AND y/n :) | |
} | |
else { | |
if ((millis()-starttime)>SERIALTIMEOUT) { | |
// User's input was too short. Assume leading zeros. | |
return res; | |
} | |
} | |
} | |
Serial.flush(); | |
return res; | |
} | |
void bits2Led(byte val) { | |
// Display 8 bits to the user via the led | |
// by calling bit2Led() for each bit | |
for (int b=0 ; b<8 ; b++,val<<=1) { | |
bit2Led(LEDPIN,val&128); | |
} | |
} | |
void bit2Led(int pin,int ledBit) { | |
// Blink/steady to display 0/1, end with a | |
// short "stop bit" for easier "readability". | |
if (ledBit) { | |
digitalWrite(pin,1); | |
delay(8*BLINKPERIOD); | |
} | |
else { // Blink the LED | |
for (int i=0; i<8; i++) { | |
digitalWrite(pin,(i&1)|ledBit); | |
delay(BLINKPERIOD); | |
} | |
} | |
digitalWrite(pin,0); // "stop bit" | |
delay(2*BLINKPERIOD); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment