Skip to content

Instantly share code, notes, and snippets.

@jekkos
Created May 2, 2013 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jekkos/5500636 to your computer and use it in GitHub Desktop.
Save jekkos/5500636 to your computer and use it in GitHub Desktop.
Some code for a teensy birthday present, to be used on mac os x Questions can be entered and should be answered correctly before a coupon code is displayed to the player. Possible improvement could be opening a screen session with the device through the terminal. In this case a session has to be opened by the user himself. Questions and answers …
/* Simple USB Keyboard Example
Teensy becomes a USB keyboard and types characters
You must select Keyboard from the "Tools > USB Type" menu
This example code is in the public domain.
*/
int count = 0;
boolean firstRun = false;
boolean questionShown = false;
int qNumber = 0;
char *questions[2] = {"Cryptische hint: Sing, sing, sing, start the party TCP-style!", "Cryptische hint: Give me a break (hex)!"};
char *answers[2]= {"antwoord1", "antwoord2"};
void setup() {
Serial.begin(9600);
delay(5000);
pinMode(11, OUTPUT);
}
void loop() {
if (firstRun) {
Keyboard.set_modifier(MODIFIERKEY_RIGHT_GUI);
Keyboard.set_key1(KEY_SPACE);
Keyboard.send_now();
delay(2000);
Keyboard.println("Terminal");
delay(1000);
Keyboard.set_key1(KEY_ENTER);
Keyboard.send_now();
Keyboard.set_key1(0);
Keyboard.send_now();
digitalWrite(11, HIGH);
delay(5000);
Keyboard.println("say 'Happy Birthday to you!..'");
digitalWrite(11, LOW);
delay(1000);
digitalWrite(11, HIGH);
Keyboard.println("say -v 'Bruce' 'Happy Birthday to you!..'");
digitalWrite(11, LOW);
delay(1000);
digitalWrite(11, HIGH);
Keyboard.println("say -v 'Kathy' 'Happy Birthday Teensy Tony!..'");
digitalWrite(11, LOW);
delay(1000);
digitalWrite(11, HIGH);
Keyboard.println("say -v 'Alex' 'Happy Birthday to you!..'");
digitalWrite(11, LOW);
delay(2000);
Keyboard.println("say -v 'Alex' 'In order to get your birthday present, open a screen session with this Teensy serial device at 9600 bauds'");
delay(1000);
Keyboard.println("say -v 'Kathy' 'Don't forget to set line ending to Carraige return, so you can provide us some correct answers!'");
}
firstRun = false;
serialReader(questions[qNumber], answers[qNumber]);
}
void serialReader(const char* question, const char* answer) {
int makeSerialStringPosition;
int inByte;
char serialReadString[50];
const int terminatingChar = 13; //Terminate lines with CR
if (!questionShown && qNumber < sizeof(&questions)) {
Serial.print("question ");
Serial.print(qNumber + 1);
Serial.print(": ");
Serial.println(question);
}
questionShown = true;
inByte = Serial.read();
makeSerialStringPosition=0;
if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)
while (inByte != terminatingChar && Serial.available() > 0) { // As long as EOL not found and there's more to read, keep reading
serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
makeSerialStringPosition++; //Increment position in array
//if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
inByte = Serial.read(); // Read next byte
}
if (inByte == terminatingChar) //If we terminated properly
{
serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
Serial.println(serialReadString);
if (strcmp(serialReadString, answer) == 0)
{
qNumber++;
questionShown = false;
Serial.println("Correct answer!");
if (qNumber == sizeof(&questions)) {
digitalWrite(11, HIGH);
Serial.println("ACK!\n");
Serial.println("Gelukkige verjaardag!");
Serial.println("Je code is xxx");
Serial.println("Please go here Please go here and type that in: https://www.sparkfun.com/gift_certificates/redeem");
Serial.println("Van Daan, Linne, Jekke, Marie, Maarten, Ben, Borre, Ryan, Mathijs en Miet!!!\n");
}
} else {
Serial.println("NAK!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment