Hello world!.... as you all roll your eyes, I had to do it.
I have been working on using an NES controller as an input device but specifically as a password input. So basically, I want it to recognize the code as I type it and when I have entered the correct code, I will have it do the final task whether that is light up an led or play a sound.
I feel like I am on the right track as it accepts the code but if you press an incorrect button, it will just ignore it and stay on the current level of the array. I tried an ELSE statement that would reset codePosition to zero but it ran every time. Just looking for advice on how to accomplish this. Thanks in advanced.
// Based on: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1254964306/0#0
#define CLOCK 4 // set the CLOCK pin
#define LATCH 3 // set the LATCH pin
#define DATIN 2 // set the data in pin
#define COUNTDOWN 500
/*
Plug:
_____
/
/ 0 --0v (ground)
+5V --- 0 0 --CLOCK
nothing --- 0 0 --LATCH
nothing --- 0 0 --SERIAL OUT
_______
How to: Just push a wire into each hold of the plug and attach
the other end of the wire to the appropriate input pin.
No soldering required so long as you can spare a hand to
hold the wires in place.
If you have a board with fewer pins than the 8 out * 3 in
as described in this code just use as many as you can and
alter the following #defines to set any extra buttons to
the same output pin.
*/
int timer_a = 0;
int timer_b = 0;
int timer_up = 0;
int timer_down = 0;
int timer_left = 0;
int timer_right = 0;
int timer_start = 0;
int timer_select = 0;
byte controller_data = 0;
int currentButton = 0;
int code[] = {5, 5, 6, 6, 7, 8, 7, 8, 2, 1, 4};
int codePosition = 0;
/* SETUP */
void setup() {
Serial.begin(57600);
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATIN, INPUT);
digitalWrite(LATCH, HIGH);
digitalWrite(CLOCK, HIGH);
}
/* THIS READS DATA FROM THE CONTROLLER */
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);
}
}
/* THE LED, SERVO, AND SERIAL MONITOR PROGRAM */
void loop() {
controllerRead();
currentButton = 0;
if (controller_data != B11111111) {
//Serial.println(controller_data, BIN);
}
// The DATIN port will transmit, one bit at a time,
// a full byte of data corresponding to the 8 buttons:
// A 01111111
// B 10111111
// SELECT 11011111
// START 11101111
// UP 11110111
// DOWN 11111011
// LEFT 11111101
// RIGHT 11111110
if (controller_data == B01111111) {
timer_a = COUNTDOWN;
}
if (controller_data == B10111111) {
timer_b = COUNTDOWN;
}
if (controller_data == B11011111) {
timer_select = COUNTDOWN;
}
if (controller_data == B11101111) {
timer_start = COUNTDOWN;
}
if (controller_data == B11110111) {
timer_up = COUNTDOWN;
}
if (controller_data == B11111011) {
timer_down = COUNTDOWN;
}
if (controller_data == B11111101) {
timer_left = COUNTDOWN;
}
if (controller_data == B11111110) {
timer_right = COUNTDOWN;
}
if (timer_a > 0) {
timer_a--;
}
if (1 == timer_a) {
Serial.println("A Button Pressed");
currentButton = 1;
}
if (timer_b > 0) {
timer_b--;
}
if (1 == timer_b) {
Serial.println("B Button Pressed");
currentButton = 2;
}
if (timer_select > 0) {
timer_select--;
}
if (1 == timer_select) {
Serial.println("Select Button Pressed");
currentButton = 3;
}
if (timer_start > 0) {
timer_start--;
}
if (1 == timer_start) {
Serial.println("Start Button Pressed");
currentButton = 4;
}
if (timer_up > 0) {
timer_up--;
}
if (1 == timer_up) {
Serial.println("Up D-pad pressed");
currentButton = 5;
}
if (timer_down > 0) {
timer_down--;
}
if (1 == timer_down) {
Serial.println("Down D-pad pressed");
currentButton = 6;
}
if (timer_left > 0) {
timer_left--;
}
if (1 == timer_left) {
Serial.println("Left D-pad pressed");
currentButton = 7;
}
if (timer_right > 0) {
timer_right--;
}
if (1 == timer_right) {
Serial.println("Right D-pad pressed");
currentButton = 8;
}
if (code[codePosition] == currentButton) {
codePosition++;
Serial.println(codePosition);
}
if (codePosition == 11) {
Serial.println("Congrats!");
}
}