Hi, I am making a 10x10 led matrix and am using the fast led library. I was wondering if anyone could point me in the right direction on how to change scripts or effects using a momentary switch with an Arduino Nano? I am very sorry for this very basic question but I am very new to Arduino.
You'll need one or more [u]if-statements[/u].
i.e. The logic would be if the button is pushed, switch to the next effect.
So for example you could make a counter that increments by one every time the button is pushed. If the variable == 1 run effect 1, if the variable == 2 run effect 2, etc. Then if you've got 10 effects and you count to 11, reset the counter to 1, etc.
You'll probably want to start by switching between two effects. Or start by turning an LED on when you push the button once and off when you push it again, in a loop so you can do that over-and-over.
The tricky part is that you have to read the switch frequently, no matter what effect is running. So, no delay()!
If you use a counter there is also a control structure called [u]switch-case[/u] that sort-of works like a series of if-statements.
...And when you're ready for it, you can better-organize your code by writing [u]functions[/u]. For example, each effect could be a different function. The counter-increment could be a function too. Then your main program loop can just decide which function to run at the moment.
...and don't forget that your momentary switch may "bounce" -- that is, go from open to closed and back several times before is settles into one final state or the other. The bounces may occur very slowly compared to the speed of your Arduino.
If you don't understand yet what is meant regarding switch case, here is an example:
/*
Using a single switch to select between 3 modes
*/
// Schematic: http://www.pwillard.com/files/mode4.jpg
//===============================================================
// Global Variables & Constants
//===============================================================
const int ledPinOne = 2; // LED1 ANODE
const int ledPinTwo = 4; // LED2 ANODE
const int ledPinThree = 7; // LED3 ANODE
const int modePin = 13; // Active HIGH, held low by 4.7K
int mode = 0; // Selector State (Initial state = ALL OFF)
int val = 0; // Pin 13 HIGH/LOW Status
int butState = 0; // Last Button State
int modeState = 0; // Last Mode State
boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
//===============================================================
// SETUP
//===============================================================
void setup () {
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(modePin, INPUT);
if (debug){
Serial.begin(9600);
Serial.print("Initial Mode: ");
Serial.println(mode);
Serial.print("Setup Complete\n");
}
}
//===============================================================
// Main Loop
//===============================================================
void loop() {
val = digitalRead(modePin);
// If we see a change in button state, increment mode value
if (val != butState && val == HIGH){
mode++;
}
butState = val; // Keep track of most recent button state
// No need to keep setting pins *every* loop
if (modeState != mode){
// If no keys have been pressed yet don't execute
// the switch code below
switch ( mode ) {
//case 1 is actually handled below as default
case 2:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
showState();
break;
case 3:
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
showState();
break;
default:
mode = 1;
// loop back to 1 by default, seems redundant but
// it also handles the "mode is > 3" problem
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinOne, HIGH);
showState();
break;
} // end switch
} // end of ModeState check
modeState = mode; // Keep track of most recent mode value
delay(10); // slow the loop just a bit for debounce
}
//===============================================================
// Subroutine
//===============================================================
void showState() {
if (debug){
Serial.print("Mode: ");
Serial.println(mode);
}
}