/* Controlling 3 Axis Mill via NES Controller Created/Modified By Edward Ford 23 February 2010 Dixon, IL Original Stepper Driver Code: Dan Thompson http://danthompsonsblog.blogspot.com/2008/09/easydriver-v31-tutorial.html September 2008 Original NES Controller Work: By Sebastian Tomczak 21 July 2007 Adelaide, Australia */ int i; int latch = 2; // set the latch pin int clock = 3; // set the clock pin int datin = 4;// set the data in pin int selectPin = 7; //set button SELECT arrow to Pin 8 int startPin = 6; //set button START arrow to Pin 7 int xsteppin = 13; //set pin 13 to X axis step pin int xdirpin = 12; //set pin 12 to X axis dir pin int ysteppin = 11; //set pin 11 to Y step pin int ydirpin = 10; //set pin 10 to Y dir pin int zsteppin = 9; //set pin 9 to Z step pin int zdirpin = 8; //set pin 8 to Z dir pin byte controller_data = 0; /* SETUP */ void setup() { Serial.begin(57600); pinMode(latch,OUTPUT); pinMode(clock,OUTPUT); pinMode(datin,INPUT); pinMode(selectPin, OUTPUT); pinMode(startPin, OUTPUT); pinMode(xdirpin, OUTPUT); pinMode(xsteppin, OUTPUT); pinMode(ydirpin, OUTPUT); pinMode(ysteppin, OUTPUT); pinMode(zdirpin, OUTPUT); pinMode(zsteppin, OUTPUT); 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(); /*This is where all the action is. Loop calls the controllerRead function which controllerRead then returns a number in binary form. If the no buttons are pressed then controllerRead returns 11111111. See comments below for specific return values The switch statement then reads controller_data and compares it the the 8 known values it may produce. Those conditions are then mapped to an LED which is defined in the top section of the code. */ switch (controller_data) { case B01111111: // A Button = 01111111 digitalWrite(zdirpin, LOW); // Set the direction. Serial.println("Going Up?"); for (i = 0; i<400; i++) // Iterate for 1600 microsteps. { digitalWrite(zsteppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(zsteppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } break; case B10111111: // B Button = 10111111 digitalWrite(zdirpin, HIGH); // Set the direction. Serial.println("Going Down?"); for (i = 0; i<400; i++) // Iterate for 1600 microsteps. { digitalWrite(zsteppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(zsteppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } break; //In a future version I would like this to trip a relay and start/stop my spindle (dremel) case B11011111: // Select Button = 11011111 digitalWrite(selectPin, HIGH); break; case B11101111: // Start = 11101111 digitalWrite(startPin, HIGH); //turn STRART LED on break; /* Forward/Backward MOTOR CONTROL SECTION Code Here */ case B11110111: // UP = 11110111 digitalWrite(ydirpin, LOW); // Set the direction. Serial.println("Going Forward?"); for (i = 0; i<400; i++) // Iterate for 1600 microsteps. { digitalWrite(ysteppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(ysteppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } break; case B11111011: // DOWN = 11111011 digitalWrite(ydirpin, HIGH); // Set the direction. Serial.println("Going Backward?"); for (i = 0; i<400; i++) // Iterate for 1600 microsteps. { digitalWrite(ysteppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(ysteppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } break; /* END Forward/Backward MOTOR CONTROL SECTION Code Here */ /*LEFT/RIGHT MOTOR CONTROL SECTION Code Here */ case B11111101: // LEFT = 11111101 digitalWrite(xdirpin, LOW); // Set the direction. Serial.println("Going Left?"); for (i = 0; i<400; i++) // Iterate for 1600 microsteps. { digitalWrite(xsteppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(xsteppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } break; case B11111110: // RIGHT = 11111110 digitalWrite(xdirpin, HIGH); // Set the direction. Serial.println("Going RIGHT?"); for (i = 0; i<400; i++) // Iterate for 1600 microsteps. { digitalWrite(xsteppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(xsteppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } break; /*END LEFT/RIGHT MOTOR CONTROL SECTION Code Here */ } delay(10); //Wait for next button push }