From f6708e9718286fbf1178162d444e96b177f141b0 Mon Sep 17 00:00:00 2001
From: mcgurk <lehtinen@sci.fi>
Date: Tue, 6 Sep 2016 09:08:23 +0300
Subject: [PATCH] Create SimpleAtariExample_keyboard

---
 Tutorial/SimpleAtariExample_keyboard | 123 +++++++++++++++++++++++++++
 1 file changed, 123 insertions(+)
 create mode 100644 Tutorial/SimpleAtariExample_keyboard

diff --git a/Tutorial/SimpleAtariExample_keyboard b/Tutorial/SimpleAtariExample_keyboard
new file mode 100644
index 0000000..ca153e9
--- /dev/null
+++ b/Tutorial/SimpleAtariExample_keyboard
@@ -0,0 +1,123 @@
+/*
+ * Simple Atari joystick adapter (keyboard HID) for learning purposes
+ * McGurk 6.9.2016
+ */
+
+#include <Keyboard.h>;
+
+// Here we define Arduino pins we use. Along with these we have to connect joystick ground to Arduino GND.
+// Because of Arduino internal pullup resistor off state is 1. Moving joystick switches corresponding pin to ground and then it is 0.
+#define UP 2 // 9-pin D-connector pin 1
+#define DOWN 3 // 9-pin D-connector pin 2
+#define LEFT 4 // 9-pin D-connector pin 3
+#define RIGHT 5 // 9-pin D-connector pin 4
+#define BUTTON 6 // 9-pin D-connector pin 6
+// 9-pin D-connector pin 8 to GND
+
+
+// We keep record how everything was last time, so we can compare if anything have changed.
+byte lastUP = 1;
+byte lastDOWN = 1;
+byte lastLEFT = 1;
+byte lastRIGHT = 1;
+byte lastBUTTON = 1;
+
+byte newUP = 1;
+byte newDOWN = 1;
+byte newLEFT = 1;
+byte newRIGHT = 1;
+byte newBUTTON = 1;
+
+
+void setup() {
+  
+  pinMode(UP, INPUT_PULLUP);
+  pinMode(DOWN, INPUT_PULLUP);
+  pinMode(LEFT, INPUT_PULLUP);
+  pinMode(RIGHT, INPUT_PULLUP);
+  pinMode(BUTTON, INPUT_PULLUP);
+
+}
+
+// We use this flag to indicate if anything changes.
+byte flag = 0;
+
+
+void loop()
+{
+
+  // Read joystick state (1 = off, 0 = on).
+  newUP = digitalRead(UP);
+  newDOWN = digitalRead(DOWN);
+  newLEFT = digitalRead(LEFT);
+  newRIGHT = digitalRead(RIGHT);
+  newBUTTON = digitalRead(BUTTON);
+
+
+  // Check if anything changed. This way we don't have to send anything to USB, if nothing changes.
+  
+  if (newUP != lastUP) {
+    lastUP = newUP;
+    flag = 1;
+  }
+
+  if (newDOWN != lastDOWN) {
+    lastDOWN = newDOWN;
+    flag = 1;
+  }
+
+  if (newLEFT != lastLEFT) {
+    lastLEFT = newLEFT;
+    flag = 1;
+  }
+
+  if (newRIGHT != lastRIGHT) {
+    lastRIGHT = newRIGHT;
+    flag = 1;
+  }
+
+  if (newBUTTON != lastBUTTON) {
+    lastBUTTON = newBUTTON;
+    flag = 1;
+  }
+
+  // If anythings changed, send new state.
+  if (flag) {
+    
+    // Press or release keyboard button according what we read earlier.
+    // We need exclamation mark in these if-conditions, because swithes are active low (0 = on, 1 = off).
+    if (!newUP) {
+      Keyboard.press('w'); 
+    } else {
+      Keyboard.release('w');
+    }
+    if (!newDOWN) {
+      Keyboard.press('s');
+    } else {
+      Keyboard.release('s');
+    }
+    if (!newLEFT) {
+      Keyboard.press('a');
+    } else {
+      Keyboard.release('a');
+    }
+    if (!newRIGHT) {
+      Keyboard.press('d');
+    } else {
+      Keyboard.release('d');
+    }
+    if (!newBUTTON) {
+      Keyboard.press('e');
+    } else {
+      Keyboard.release('e');
+    }
+    
+    // Remember to clear flag.
+    flag = 0;
+    
+  }
+  
+  // Little 1ms breathing break.
+  delayMicroseconds(1000);
+  
+}