Anonymous avatar Anonymous committed 1cc8c34

Added snes/snes.pde: read SNES buttons and control 2 servomotors and 1 speaker

Comments (0)

Files changed (1)

+/*
+Created by Álvaro Justen aka Turicas
+Contact: alvarojusten@gmail.com
+License: GPLv2
+*/
+
+#define CLOCK 3
+#define STROBE 2
+#define DATA 4
+
+#define SOUND 9
+#define MOTOR1 10
+#define MOTOR2 11
+
+#define SNES_B       0b0000000000000001
+#define SNES_Y       0b0000000000000010
+#define SNES_SELECT  0b0000000000000100
+#define SNES_START   0b0000000000001000
+#define SNES_UP      0b0000000000010000
+#define SNES_DOWN    0b0000000000100000
+#define SNES_LEFT    0b0000000001000000
+#define SNES_RIGHT   0b0000000010000000
+#define SNES_A       0b0000000100000000
+#define SNES_X       0b0000001000000000
+#define SNES_L       0b0000010000000000
+#define SNES_R       0b0000100000000000
+
+unsigned int getSNESbuttons(int clock, int strobe, int data) {
+  /*
+    Mapping from http://www.gamesx.com/controldata/snesdat.htm
+    | B | Y | Select | Start | Up | Down | Left | Right | A | X | L | R | N/A | N/A | N/A | N/A |
+  */
+  unsigned int fromController = 0b0000000000000000, i;
+
+  //Do the strobe to start reading button values
+  digitalWrite(strobe, HIGH);
+  delayMicroseconds(12);
+  digitalWrite(strobe, LOW);
+
+  delayMicroseconds(6); //Wait for controller to start sending the button values 
+  for (i = 0; i < 16; i++) {
+    fromController |= digitalRead(data) << i; //read the value, shift it and store it as a bit on fromController
+
+    //More one cycle on the clock pin...
+    digitalWrite(clock, HIGH);
+    delayMicroseconds(6);
+    digitalWrite(clock, LOW);
+    delayMicroseconds(6);
+  }
+
+  //return the read bits (16)
+  return ~fromController;
+}
+
+void setup() {
+  pinMode(CLOCK, OUTPUT);
+  pinMode(STROBE, OUTPUT);
+  pinMode(DATA, INPUT);
+}
+
+unsigned int buttonsPressed;
+void loop() {
+  buttonsPressed = getSNESbuttons(CLOCK, STROBE, DATA);
+  if (buttonsPressed & SNES_UP) {
+    analogWrite(MOTOR1, 20);
+    analogWrite(MOTOR2, 20);
+  }
+  else if (buttonsPressed & SNES_DOWN) {
+    analogWrite(MOTOR1, 240);
+    analogWrite(MOTOR2, 240);
+  }
+  else {
+    analogWrite(MOTOR1, 0);
+    analogWrite(MOTOR2, 0);
+  }
+
+  if (buttonsPressed & SNES_A) {
+    analogWrite(SOUND, 150);
+  }
+  else {
+    analogWrite(SOUND, 0);
+  }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.