/*
j3r03n85's Famciom/NES clone 50/60Hz Video mode switcher for TA-02NP PPU's
In this sketch I use an arduino to switch PIN 16 of my TA-02NP clone NES PPU from 50 to 60Hz
using the reset button.
The reset button still acts as a normal reset button on the console, but when you hold it for
a brief moment you'll see the power LED changing color and the PPU will be switched from
50 to 60Hz.
When holding the reset button again for the same amount of time will switch it back to the mode
it was before.
The main reason I used an arduino is that I didn't want to drill a hole in my case for a
physical switch.
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 8; // the number of the pushbutton pin
const int REDled = 11; // the number of the RED LED pin
const int GREENled = 12; // the number of the GREEN LED pin
const int VIDMODE = 3; // the number of the Video output mode
// Variables will change:
int REDledState = HIGH; // the current state of the output pin
int GREENledState = LOW;
int VIDMODEState = LOW;
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 1000; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(REDled, OUTPUT);
pinMode(GREENled, OUTPUT);
pinMode(VIDMODE, OUTPUT);
// set initial LED state
digitalWrite(REDled, REDledState);
digitalWrite(GREENled, GREENledState);
digitalWrite(VIDMODE, VIDMODEState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
REDledState = !REDledState;
GREENledState = !GREENledState;
VIDMODEState = !VIDMODEState;
}
}
}
// set the LED:
digitalWrite(REDled, REDledState);
digitalWrite(GREENled, GREENledState);
digitalWrite(VIDMODE, VIDMODEState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}