Upload this to any Arduino and be wary of the pinout. The QtPy I used for this has several options available for analog pins.
/*
Random Search For Aaron
Takes a button input, and there is a 50% chance that it will light up the red LED.
shhhhh, there's also a 1% chnance it will light up the green LED.
Compatible Arduino model: QT Py SAMD21 (for the nexopixel pin)
-based on example code from https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
// set up pins
const int buttonPin = 2; // the number of the pushbutton pin
const int redPin = 5; // the number of the LED pin
const int greenPin = 4; // the number of the LED pin
// variables:
int redState = LOW;
int greenState = LOW;
//neopixel variables;
int red = 0;
int green = 0;
int buttonState = LOW; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
bool isFading = false; // flag to prevent button reads during fade
// 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 = 25; // the debounce time; increase if the output flickers
unsigned long theTime;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void fadeRed() {
for (int i = 0; i < 255; i++) {
analogWrite(redPin, i);
delay(10);
}
delay(500);
for (int i = 0; i < 255; i++) {
analogWrite(redPin, 255-i);
delay(10);
}
analogWrite(redPin, 0);
}
void fadeGreen() {
for (int i = 0; i < 255; i++) {
analogWrite(greenPin, i);
delay(10);
}
delay(500);
for (int i = 0; i < 255; i++) {
analogWrite(greenPin, 255-i);
delay(10);
}
analogWrite(greenPin, 0);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
theTime = millis();
// 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 = theTime;
}
if ((theTime - 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;
// Debug output for any state change
Serial.print("State change: ");
Serial.println(buttonState == HIGH ? "HIGH" : "LOW");
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
Serial.println("Button pressed");
//check the time
Serial.print("at");
Serial.println(theTime);
if (theTime % 2 == 0){
if (theTime % 100 == 0) {
fadeGreen();
} else {
fadeRed();
}
}
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}