"program" inside program

Hi,

I was doing Arduino for 2 week for now and now I've got a problem. I could easily google it, but I have no idea how should I call it and Google offers no help.

So, this is what I want: I have 3 LEDs, connected to digital pins, potentiometer on Analog 2, and pushbutton on 7 digital. And I want to do that pushbutton have function of changing different programs of these LEDs. So, for example; first program'd be all LEDs blinking and speed is controlled with potentio, program two leds different fading etc. There is no problem about those little programs, I can write them, but what about that pushbutton changes me between them. Let's say when you on your Arduino first program is on, if you press pushbutton it goes to second program etc. Any suggestions?

Thanks

Let's say when you on your Arduino first program is on, if you press pushbutton it goes to second program etc. Any suggestions?

Let's use some proper terminology, OK. There is only one program running on the Arduino. That program could call any number of functions on each pass through loop(). You could have something like:

int currState;
int prevState = HIGH;
int mode = 0;
const int swPin = 7;

void loop()
{
   currState = digitalRead(swPin);
   if(currState != prevState)
   {
      // A transition occurred
      if(currState == LOW)
      {
         // to pressed
         mode++;
      }
   }
   prevState = currState;

   switch(mode % 4)
   {
      case 0: fun1(); break;
      case 1: fun2(); break;
      case 2: fun3(); break;
      case 3: fun4(); break;
   }
}

Push the button - the mode changes, and a different function gets called.

The key, then, is to write the various functions in a non-blocking way.

Well, that's useful, thanks.

If you don't mind, since I'm beginner, can you explain me what following does in code?

  1. What means!=
  2. What this does mode++
  3. What does break do? Is this where I should put my code?       case 0: fun1(); break;

All of those are listed on the Reference page: Arduino - Home

You might want to try searching for "implementing state machine" as a start for some interesting info.

dava_2:
Well, that's useful, thanks.

If you don't mind, since I'm beginner, can you explain me what following does in code?

You really should look up a reference on the C and/or C++ language to gain a better understanding; the Arduino references will help, too - but all Arduino code is C and C++, so understanding the basics of that language (because C++ is more or less a "superset" of C) will be helpful.

dava_2:

  1. What means!=

In the context of a conditional test, that means "not" (!) "equal" (=); thus:

if (1 != 0) { // evaluates to TRUE

dava_2:
2. What this does mode++

What that does is increment a variable - in essence, it is equivalent shorthand to:

mode = mode + 1;

Note, though, that mode++; and ++mode;, while equivalent in a "standalone" situation, are -not- equivalent in other situations (which I won't go into here; just know that the first is called "post-incrementing" and the second "pre-incrementing"; in the context of using them in a for() loop, or during a conditional check, you can get two different results depending on which you use).

dava_2:
3. What does break do? Is this where I should put my code?       case 0: fun1(); break;

I can't answer in regards to "your code", but the break statement is to prevent "fall-through" to subsequent case statements below the one you are currently in (note, you may want this to occur, depending on your logic needs).

Thanks everyone for answers. I have done some researching on Reference site and remade my program.

If you do not mind, I'd like to ask you another question. I have made program for 4LEDs for fading. But I want to 2 leds going on and two going off. If we could show it as brightness, I want at specific moment have brightness1 at 127(going on), brightness2 at 225(max. on), brightness3 at -127(going off) and brightness4 at -255(or 0, total off). How should I write it? This is what I've wrote until now.

const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 6;
const int potPin = 2;
int brightness1 = 127;
int brightness2 = 255;
int brightness3 = -127;
int brightness4 = 0;
int fadeAmount1 = 1;
int fadeAmount2 = 1;
int fadeAmount3 = 1;
int fadeAmount4 = 1;
int val = 0;


void setup() 
{
  
pinMode (ledPin1, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
pinMode (ledPin4, OUTPUT);
}

void loop()
{
val = analogRead(potPin);
analogWrite(ledPin1, brightness1);
analogWrite(ledPin2, brightness2);
analogWrite(ledPin3, brightness3);
analogWrite(ledPin4, brightness4);

brightness1 = brightness1 + fadeAmount1;
brightness2 = brightness2 + fadeAmount2;
brightness3 = brightness3 + fadeAmount3;
brightness4 = brightness4 + fadeAmount4;

  if (brightness1 == 0 || brightness1 == 255) {
        fadeAmount1 = -fadeAmount1 ; }
  if (brightness2 == 0 || brightness2 == 255) {
        fadeAmount2 = -fadeAmount2 ; }
  if (brightness3 == 0 || brightness3 == 255) {
        fadeAmount3 = -fadeAmount3 ; }
  if (brightness4 == 0 || brightness4 == 255) {
        fadeAmount4 = -fadeAmount4 ; }
        }

Valid values for analogWrite() range from 0 to 255, not -255 to 255.

Got it, so how should I write it? At one led should go from 127 upper, and at second from 127 under values..

Got it, so how should I write it? At one led should go from 127 upper, and at second from 127 under values..

I'm not sure what your requirement is. Can you try explaining again, or give an example?

If you want a loop where one value increments from 127 to 255 while another decrements from 127 to 0, that is easy:

for(int inc=127, dec=127; inc<=255; inc++, dec--)
{
   // inc will be 127, 128, 129, ..., 255
   // dec will be 127, 126, 125, ..., 0
}