hello all, i,m new to programming and this is my second very simple project for my own learning base. i,v successfully managed to switch between two loops and now i want to add three more loops for additional blue led blinking and rest two loops for fading and color mixing, my main target is switching between more than two loops. your help will be appreciated to solving this method, thanks in advance.
here is my code.
int greenled = 4; //Led's and pins
int redled = 2;
int modeSwitch = 14;
int buttonState;
int redledState = LOW;
int greenledState = LOW;
boolean modeState;
void setup()
{
pinMode(greenled, OUTPUT); //Pinmodes of the leds
pinMode(redled, OUTPUT);
pinMode(modeSwitch, INPUT);
}
void modeOne()
{
digitalWrite(redled, 255);
digitalWrite(greenled, 0);
delay(500);
digitalWrite(redled, 255);
digitalWrite(greenled, 255);
delay(500);
}
void modeTwo()
{
digitalWrite(redled, 0);
digitalWrite(greenled, 255);
delay(500);
digitalWrite(redled, 255);
digitalWrite(greenled, 255);
delay(500);
}
void loop() {
buttonState = digitalRead(modeSwitch);
if (buttonState == LOW) {
modeState = !modeState; // switch the state of the loops
}
if (modeState == false) {
modeOne();
}
else { // (if modeState == true)
modeTwo();
}
}
How are you planning to switch? Perhaps each button press moves to the next mode function?
Can you write a program to count button presses? This would be the heart of such an approach. There are example programs in the IDE which could lead you to be able to count a button press, if you can't already.
Sooner or later you will notice that your mode functions temporarily blind you to other activities like button presses, so your button will be unresponsive and sluggish.
You may have "modes" in mind that are, in effect, infinite loops, never returning, therefor you would never ever see the button getting pressed. At that time you will need to start programming your modes in a fundamental different way, look forward to that.
'For now, count a button press, use switch/case to select and run the mode of your choice.