I see everyone do it in youtube videos... What I want to do is have a mode select button. When you press it, it goes to mode 2, press again for mode 3, ect... and after the last omde, it returns to mode 1. How do I do this?
I know how to wire a button to arduino. I dont know what code I need... Please guide me through this... I know that you guys are all good at it...
What you want to do is increment or decrement the variable then test the variable with something like select to do what you want. If the variable gets past your limit you reset it to a known default.
Look at the examples switchCase for how select works.
IF is a simple operation as well
The youtube videos should have a link to the code. In general the Arduino IDE uses C language though with some hand holding and predefined functions.
I recommend reading the examples built in and learning why they work.
if(counter < 100)
{
// if counter is less than 100 do something
counter++; //advance the counter by 1
}
else counter = 0; // else ignore and reset counter
or
if(counter < 100) counter++; //advance the counter by 1
else
{
// if counter is greater than 100 do something
counter = 0; // reset the counter
}
here is what i know how to write... i don't know how to read a momentary pushbutton though... also, I don't know how to make the button cycle the modes... ignore the led definitions... theyre just for my modes...
int counter = 0;
int buttonPin = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop()
{
digitalRead(buttonPin);
}
First wanted to say thanks for all the input on this thread as it's exactly what I was wanting to do with a mode select switch and some LED patterns. That said, I'm having troubles getting mine to work correctly and was wondering if someone could offer some input on the code:
I'm using a QT100 as the input for the switch if that makes any difference (which i don't think it should since it outputs a high (3vdc in this case) to pin 13 when touched.
Syntactically correct, but pointless. counter++; if you actually want to affect the counter variable.
digitalRead(buttonPin);
if(buttonPin = HIGH)
{
The digitalRead function returns the state of the pin, which you then discard.
Next, you assign the value of HIGH to the pin number. The result of that assignment will be the value assigned, which, being non-zero, is considered true, so the if block will be executed.
You probably want == in there, to compare the values. And the value you want to compare is not the pin number, but the value read from the pin (that you didn't save).
The whole if/else if/else if... mess shoudl be replaced by a switch statement. Much shorter, much cleaner, and much easier to understand.
Thanks for the quick reply Paul.
Ok, so hopefully I understood what you are telling me and I looked at the examples in the included sketch files for switch statements...hopefully this is correct :-[
int counter = 0;
int buttonPin = 13;
int LEDB = 11;
int LEDG = 10;
int LEDR = 9;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
}
void loop() {
//Handle input
if(buttonPin == HIGH)
{
counter ++;
//Reset count if over max mode number
if(counter == 7)
{
counter = 0;
}
}
//Change mode
switch (counter) {
case 1:
analogWrite(LEDR, 255);
break;
case 2:
analogWrite(LEDR, 127);
break;
case 3:
analogWrite(LEDB, 255);
break;
case 4:
analogWrite(LEDB, 127);
break;
case 5:
analogWrite(LEDR, 255);
analogWrite(LEDG, 255);
analogWrite(LEDB, 255);
break;
case 6:
analogWrite(LEDR, 127);
analogWrite(LEDG, 127);
analogWrite(LEDB, 127);
break;
}
}
Well, I'm almost positive that HIGH is not defined as having the value of 13, so that test will almost assuredly return false, and the if block will be skipped.
I think you want something like this:
int switchVal = digitalRead(buttonPin);
if(switchVal == HIGH)
{
// Do something
}
Really, you probably do not have a button connected to the Arduino. It's a switch. So, the number of the pin it is attached to should be stored in a variable named switchPin. Just a pet peeve of mind.
Ok, Paul. Thanks again. Most of the tutorials/examples I was reading used button, so I just used that. Added a delay before counter ++ also, since my switch seems to be excessively sensitive, so acts a little like a debounce function...or just a lazy way to do it. Anyways, works great now and thanks for the help.
Final setup:
int counter = 0;
int switchPin = 13;
int LEDB = 11;
int LEDG = 10;
int LEDR = 9;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
}
void loop() {
//Handle input
int switchVal = digitalRead(switchPin);
if(switchVal == HIGH)
{
delay(500);
counter ++;
//Reset count if over max mode number
if(counter == 8)
{
counter = 0;
}
}
else
//Change mode
switch (counter) {
case 1:
analogWrite(LEDR, 255);
break;
case 2:
analogWrite(LEDR, 127);
break;
case 3:
analogWrite(LEDR, 000);
analogWrite(LEDB, 255);
break;
case 4:
analogWrite(LEDB, 127);
break;
case 5:
analogWrite(LEDR, 255);
analogWrite(LEDG, 255);
analogWrite(LEDB, 255);
break;
case 6:
analogWrite(LEDR, 127);
analogWrite(LEDG, 127);
analogWrite(LEDB, 127);
break;
case 7:
analogWrite(LEDR, 000);
analogWrite(LEDG, 000);
analogWrite(LEDB, 000);
break;
}
}