Really struggling with finishing touches to my code.... any help please?

Hi all...

I am still trying to get to grips with Arduino coding.. I have bought 4 books, and am doing all I can to get my head around it. But I'm still struggling a bit... If anyone had any ideas about the code below (query is in the //COMMENTS at the top of code), I would really appreciate it.

Many Thanks
Dara

//SO HERE IS MY PROBLEM... I HAVE THREE SEPARATE SEQUENCES GOING....
//THE FIRST TWO ARE STANDARD FLASHING LEDS...
//THE THIRD IS A FADING LED..
//I HAVE TRIED MANY DIFFERENT WAYS TO SOLVE THIS (USING A BOOK "GETTING STARTED WITH ARDUINO)... NO JOY...

//I WOULD LOVE TO BE ABLE TO GET TO A STAGE WHERE THE FOLLOWING HAPPENS..
//PUSH A SWITCH TO TURN ON SEQUENCE 1...
//SECOND PUSH TURNS ON SEQUENCES 2 AND 3..
//PUSH TO SWITCH OFF...

//IS THIS EVEN POSSIBLE? OR AM I TRYING TO DO TOO MUCH??

//ANY AND ALL HELP AND ADVICE IS APPRECIATED...
//I AM *FAIRLY* OKAY WOTH CODING, BUT AM STILL LEARNING, SO I MIGHT NOT UNDERSTAND 
//ALL SUGGESTIONS, BUT ALL HELP APPRECIATED...

//THANKS DARA




const byte LEDA8Pin = A8;
const byte LEDA9Pin = A9;


const byte LEDFADE13Pin = 0;

//-------------------------------------------------
void setup()
{
pinMode(LEDA8Pin, OUTPUT);
pinMode(LEDA9Pin, OUTPUT);


pinMode(LEDFADE13Pin, OUTPUT);

}
//-------------------------------------------------
void loop()
{
SequenceRunA8(LEDA8Pin);//ROSSLARE EUROPORT OC WRG (G) 5s OK ok120
SequenceRunA9(LEDA9Pin);//BALLINACOURTY POINT FL(2)WRG (W) 10s OK

SequenceFade13(LEDFADE13Pin);//TUSKAR ROCK FL(2) 7.5s OK


}
//-------------------------------------------------
//START OF LIGHT LED FLASHING, INDEPENDENT OF FADE

void SequenceRunA8(const byte pin)
{
unsigned long currentMillis = millis();
const int sequence[][2] =
{

{HIGH, 3500},
{LOW, 121500}

};
const byte sequenceLength = sizeof sequence / sizeof sequence[0];
static unsigned long intervalStart = 0;  
static unsigned long interval;
static byte index = 0;  
if (intervalStart == 0)
{
digitalWrite(pin, sequence[index][0]);
interval = sequence[index][1];  
intervalStart = currentMillis;  
}

if (currentMillis - intervalStart >= interval)
{
index = (index + 1) % sequenceLength;  
digitalWrite(pin, sequence[index][0]);  
interval = sequence[index][1];  
intervalStart = currentMillis;   
}
}
//END OF SEQUENCE
//-------------------------------------------------
//START OF SECOND LIGHT LED FLASHING, INDEPENDENT OF FADE

void SequenceRunA9(const byte pin)
{

unsigned long currentMillis = millis();
const int sequence[][2] =
{
{HIGH, 500},
{LOW, 1000},
{HIGH, 500},
{LOW, 8000}

};
const byte sequenceLength = sizeof sequence / sizeof sequence[0];
static unsigned long intervalStart = 0;  
static unsigned long interval;
static byte index = 0;  
if (intervalStart == 0)
{
digitalWrite(pin, sequence[index][0]);
interval = sequence[index][1];  
intervalStart = currentMillis;  
}

if (currentMillis - intervalStart >= interval)
{
index = (index + 1) % sequenceLength;  
digitalWrite(pin, sequence[index][0]);  
interval = sequence[index][1];  
intervalStart = currentMillis;   
}
}
//END LIGHT SEQUENCE
//-------------------------------------------------
//START OF FADING LIGHT SEQUENCE

void SequenceFade13(const byte pin)
{
  unsigned long currentMillis = millis();
  static unsigned long intervalStart = 0;
  static unsigned long interval;
  static byte index = 0;
  static int fadeValue = 0;
  if (intervalStart == 0)
  {
    analogWrite(pin, fadeValue);
    interval = 1;
    intervalStart = currentMillis;
  }
  if (currentMillis - intervalStart >= interval)
  {
    intervalStart = currentMillis;
    switch (index)
    {
      case 0:   // pause
        interval = 1000;
        index++;
        break;
      case 1:   // counting up
        analogWrite(pin, fadeValue);
        interval = 1;
        fadeValue++;
        if (fadeValue > 100)
        {
          index++;
          fadeValue = 100;
        }
        break;
      case 2:   // counting down
        analogWrite(pin, fadeValue);
        interval = 10;
        fadeValue--;
        if (fadeValue < 0)
        {
          index++;
          fadeValue = 0;
        }
        break;
      case 3:   // pause
        interval = 500;
        index++;
        break;
      case 4:   // counting up
        analogWrite(pin, fadeValue);
        interval = 1;
        fadeValue++;
        if (fadeValue > 100)
        {
          index++;
          fadeValue = 100;
        }
        break;
      case 5:   // counting down
        analogWrite(pin, fadeValue);
        interval = 10;
        fadeValue--;
        if (fadeValue < 0)
        {
          index++;
          fadeValue = 0;
        }
        break;
      case 6:   // pause
        interval = 4800;
        index = 1;  // start over
        break;
    }
  }
}

{LOW, 121500} Which Arduino are you running this on?

A Mega... If I can get this switching done, I will actually be doing away with that 121500.... must have left it in there by accident.... My initial plan (when this wasn't working out) was to run sequence 1, pause it for 120sec, and then run sequences 2 and 3 in parallel.... and start again...

I believe that the Arduino Mega 2560 has a 16 bit signed integer that tops out at 32,767. 121500 is well beyond this and is likely to lead to incorrect results.

This is the definition of the problem - extracted from the program.

//SO HERE IS MY PROBLEM... I HAVE THREE SEPARATE SEQUENCES GOING....
//THE FIRST TWO ARE STANDARD FLASHING LEDS...
//THE THIRD IS A FADING LED..
//I HAVE TRIED MANY DIFFERENT WAYS TO SOLVE THIS (USING A BOOK "GETTING STARTED WITH ARDUINO)... NO JOY...

//I WOULD LOVE TO BE ABLE TO GET TO A STAGE WHERE THE FOLLOWING HAPPENS..
//PUSH A SWITCH TO TURN ON SEQUENCE 1...
//SECOND PUSH TURNS ON SEQUENCES 2 AND 3..
//PUSH TO SWITCH OFF...

//IS THIS EVEN POSSIBLE? OR AM I TRYING TO DO TOO MUCH??

//ANY AND ALL HELP AND ADVICE IS APPRECIATED...
//I AM FAIRLY OKAY WOTH CODING, BUT AM STILL LEARNING, SO I MIGHT NOT UNDERSTAND
//ALL SUGGESTIONS, BUT ALL HELP APPRECIATED...

What you want is certainly possible and should not be too difficult. However you need to tell us in detail what the program actually does and what you want it to do that is different. That way we can focus on the parts you need help with rather than wasting time on things that you can do.

For example I don't know if everything works except for the buttons sequence, or whether you have a problem with the fading sequence. Or both.

If you have a problem with the fading sequence I suggest making a copy of your program and removing from it everything that is not part of the fading sequence.

...R

Thanks for your reply... it is very encouraging that it might be possible...

So, my circuit is to have LEDs flash (or fade as the case may be), on certain parts of a map...

Normally, this works fine for me. I just conect the power, and away it goes... but now I want to be able to show something else on the map, when I click the button...

So basically, 1st press = turn on single sequence, on its own...
2nd press, switch off the 1st sequence, and turn on the second and third ones (or as many as there are)
3rd press... turn the whole lot off...

I have corrected the code (removed the 121500), and I assigned the pins to test it.... it runs fine...

Again, I do appreciate the help. I am trying to work around it myself, but keep tripping myself up...

Corrected code below.

//NEW NOTES:

//I WANT TO START THIS ONE ON FIRST CLICK
const byte LEDA8Pin = A8;

//I WANT TO START THESE TWO ON THE SECOND CLICK, AND THE ONE ABOVE STOPS
const byte LEDA9Pin = A9;
const byte LEDFADE13Pin = 13;

//-------------------------------------------------
void setup()
{
pinMode(LEDA8Pin, OUTPUT);
pinMode(LEDA9Pin, OUTPUT);


pinMode(LEDFADE13Pin, OUTPUT);

}
//-------------------------------------------------
void loop()
{
SequenceRunA8(LEDA8Pin);
SequenceRunA9(LEDA9Pin);

SequenceFade13(LEDFADE13Pin);


}
//-------------------------------------------------
//START OF LIGHT LED FLASHING, INDEPENDENT OF FADE

void SequenceRunA8(const byte pin)
{
unsigned long currentMillis = millis();
const int sequence[][2] =
{

{HIGH, 3500},
{LOW, 1500}

};
const byte sequenceLength = sizeof sequence / sizeof sequence[0];
static unsigned long intervalStart = 0;  
static unsigned long interval;
static byte index = 0;  
if (intervalStart == 0)
{
digitalWrite(pin, sequence[index][0]);
interval = sequence[index][1];  
intervalStart = currentMillis;  
}

if (currentMillis - intervalStart >= interval)
{
index = (index + 1) % sequenceLength;  
digitalWrite(pin, sequence[index][0]);  
interval = sequence[index][1];  
intervalStart = currentMillis;   
}
}
//END OF SEQUENCE
//-------------------------------------------------
//START OF SECOND LIGHT LED FLASHING, INDEPENDENT OF FADE

void SequenceRunA9(const byte pin)
{

unsigned long currentMillis = millis();
const int sequence[][2] =
{
{HIGH, 500},
{LOW, 1000},
{HIGH, 500},
{LOW, 8000}

};
const byte sequenceLength = sizeof sequence / sizeof sequence[0];
static unsigned long intervalStart = 0;  
static unsigned long interval;
static byte index = 0;  
if (intervalStart == 0)
{
digitalWrite(pin, sequence[index][0]);
interval = sequence[index][1];  
intervalStart = currentMillis;  
}

if (currentMillis - intervalStart >= interval)
{
index = (index + 1) % sequenceLength;  
digitalWrite(pin, sequence[index][0]);  
interval = sequence[index][1];  
intervalStart = currentMillis;   
}
}
//END LIGHT SEQUENCE
//-------------------------------------------------
//START OF FADING LIGHT SEQUENCE

void SequenceFade13(const byte pin)
{
  unsigned long currentMillis = millis();
  static unsigned long intervalStart = 0;
  static unsigned long interval;
  static byte index = 0;
  static int fadeValue = 0;
  if (intervalStart == 0)
  {
    analogWrite(pin, fadeValue);
    interval = 1;
    intervalStart = currentMillis;
  }
  if (currentMillis - intervalStart >= interval)
  {
    intervalStart = currentMillis;
    switch (index)
    {
      case 0:   // pause
        interval = 1000;
        index++;
        break;
      case 1:   // counting up
        analogWrite(pin, fadeValue);
        interval = 1;
        fadeValue++;
        if (fadeValue > 100)
        {
          index++;
          fadeValue = 100;
        }
        break;
      case 2:   // counting down
        analogWrite(pin, fadeValue);
        interval = 10;
        fadeValue--;
        if (fadeValue < 0)
        {
          index++;
          fadeValue = 0;
        }
        break;
      case 3:   // pause
        interval = 500;
        index++;
        break;
      case 4:   // counting up
        analogWrite(pin, fadeValue);
        interval = 1;
        fadeValue++;
        if (fadeValue > 100)
        {
          index++;
          fadeValue = 100;
        }
        break;
      case 5:   // counting down
        analogWrite(pin, fadeValue);
        interval = 10;
        fadeValue--;
        if (fadeValue < 0)
        {
          index++;
          fadeValue = 0;
        }
        break;
      case 6:   // pause
        interval = 4800;
        index = 1;  // start over
        break;
    }
  }
}

Hi Dara,

the code will need a single button

in the following picture the button is representing the "pushing force"

imagine a rotary-switch with multiple positions. every "push" turns the rotary-switch to the next position.
depending on the position different actions take place

the basic idea is to have code that detects if an inputpin with a button conected changes from "unpressed" to "pressed"
when detecting the statechange a countervariable gets incremented.

if counter == 1 execute sequence 1

if counter == 2 execute sequence 2

if counter == 3 execute sequence 3
etc etc etc.

so there are two things to write code for

a: detecting io-pin-change and if it has happened increment variable

b: the if-conditions

You did't wrote anything about your knowledge how to connect buttons to a microcontroller
In the mictocontroller-world things are not super-standardized like USB-devices.
You have to take care of more things than "fits the plug into the socket?"

So here is a tutorial about how to connect buttons and switches to microcontrollers

here is a tutorial about detecting statechanges

the if-conditions can create the functionality to execute code depending on the counter-variable

a more elegant way is to use the switch case -function
https://www.arduino.cc/en/Reference.SwitchCase

best refards Stefan