Guest User

Untitled

a guest
Jan 21st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. /*
  2. Reading a NES Controller
  3. By Sebastian Tomczak
  4. 21 July 2007
  5. Adelaide, Australia
  6.  
  7. bit 7 = A
  8. bit 6 = B
  9. bit 5 = select
  10. bit 4 = start
  11. bit 3 = up
  12. bit 2 = down
  13. bit 1 = left
  14. bit 0 = right
  15. */
  16.  
  17. /* INITIALISATION */
  18.  
  19. int latch = 2; // set the latch pin
  20. int clock = 3; // set the clock pin
  21. int datin = 4;// set the data in pin
  22. byte controller_data = 0;
  23.  
  24. /* SETUP */
  25. void setup() {
  26. Serial.begin(57600);
  27. pinMode(latch,OUTPUT);
  28. pinMode(clock,OUTPUT);
  29. pinMode(datin,INPUT);
  30.  
  31. digitalWrite(latch,HIGH);
  32. digitalWrite(clock,HIGH);
  33. }
  34.  
  35. /* CONTROLLER READ */
  36. void controllerRead() {
  37. controller_data = 0;
  38. digitalWrite(latch,LOW);
  39. digitalWrite(clock,LOW);
  40.  
  41. digitalWrite(latch,HIGH);
  42. delayMicroseconds(2);
  43. digitalWrite(latch,LOW);
  44.  
  45. controller_data = digitalRead(datin);
  46.  
  47. for (int i = 1; i <= 7; i ++) {
  48. digitalWrite(clock,HIGH);
  49. delayMicroseconds(2);
  50. controller_data = controller_data << 1;
  51. controller_data = controller_data + digitalRead(datin) ;
  52. delayMicroseconds(4);
  53. digitalWrite(clock,LOW);
  54. }
  55.  
  56. }
  57.  
  58. /* PROGRAM */
  59. void loop() {
  60. controllerRead();
  61. Serial.println(controller_data, BIN);
  62. delay(100);
  63. }
Add Comment
Please, Sign In to add comment