Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reading a NES Controller
- By Sebastian Tomczak
- 21 July 2007
- Adelaide, Australia
- bit 7 = A
- bit 6 = B
- bit 5 = select
- bit 4 = start
- bit 3 = up
- bit 2 = down
- bit 1 = left
- bit 0 = right
- */
- /* INITIALISATION */
- int latch = 2; // set the latch pin
- int clock = 3; // set the clock pin
- int datin = 4;// set the data in pin
- byte controller_data = 0;
- /* SETUP */
- void setup() {
- Serial.begin(57600);
- pinMode(latch,OUTPUT);
- pinMode(clock,OUTPUT);
- pinMode(datin,INPUT);
- digitalWrite(latch,HIGH);
- digitalWrite(clock,HIGH);
- }
- /* CONTROLLER READ */
- void controllerRead() {
- controller_data = 0;
- digitalWrite(latch,LOW);
- digitalWrite(clock,LOW);
- digitalWrite(latch,HIGH);
- delayMicroseconds(2);
- digitalWrite(latch,LOW);
- controller_data = digitalRead(datin);
- for (int i = 1; i <= 7; i ++) {
- digitalWrite(clock,HIGH);
- delayMicroseconds(2);
- controller_data = controller_data << 1;
- controller_data = controller_data + digitalRead(datin) ;
- delayMicroseconds(4);
- digitalWrite(clock,LOW);
- }
- }
- /* PROGRAM */
- void loop() {
- controllerRead();
- Serial.println(controller_data, BIN);
- delay(100);
- }
Add Comment
Please, Sign In to add comment