Classes in arduino problem with the loop

1

good morning

I have a problem when doing a class in Arduino and it is that the code is not repeated as in a loop, I explain:

I made a library that allows me to control a series of LEDs in a blinking way and I want that blinking sequence to repeat itself in a definite way. However, I have run into the problem in which at the time of implementing it only runs once and does not blink again the times I indicate they are. The code is as follows

Header

#ifndef Cluster_h
#define Cluster_h

#include "Storm.h"
#include "Arduino.h"

class Cluster{
    public:
        Cluster(int pin[]);
        void lightning(unsigned long g_glag[]);
    private:
        unsigned long currentmillis,prevmillis,g_flag[];
        int counter,u_flag;
        Storm* _led;
};



#endif

cpp file

#include <Cluster.h>

Cluster::Cluster(int pin[]){
    Storm led[6]={Storm(pin[0]),Storm(pin[1]),Storm(pin[2]),Storm(pin[3]),Storm(pin[4]),Storm(pin[5])};
    _led=led;
    prevmillis=0;
    counter=0;
}

void Cluster::lightning(unsigned long g_flag[]){


    while(counter<=5){  
        currentmillis=millis();

        if(currentmillis-prevmillis>=g_flag[0]){
            _led[0].blinkled();
        }
        if(currentmillis-prevmillis>=g_flag[1]){
            _led[1].blinkled();
        }
        if(currentmillis-prevmillis>=g_flag[2]){
            _led[2].blinkled();
        }
        if(currentmillis-prevmillis>=g_flag[3]){
            _led[3].blinkled();
        }
        if(currentmillis-prevmillis>=g_flag[4]){
            _led[4].blinkled();
        }
        if(currentmillis-prevmillis>=g_flag[5]){
            prevmillis=currentmillis;
            counter=counter+1;
        }

    }
}

the blinkled function of the Storm class, which is imported from the header, is the one that makes the LED flashed a predefined number of times. I would expect that with this code and at the time of implementation this sequence of full flickers would repeat itself 5 times, but only once. I have not found the possible cause of this, if they had any idea of the possible error, it would be very helpful to listen to their proposals.

Thank you in advance, here I leave the implementation code in case it becomes useful.

#include <Cluster.h>

int p[6]={3,4,5,6,7,8};
unsigned long gf[7]={50,100,120,150,200,1000,0};
Cluster cluster(p);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  cluster.lightning(gf);
} 
    
asked by INME 22.04.2017 в 02:03
source

1 answer

3

Look at what you do in your constructor:

Cluster::Cluster( int pin[] ) {
  Storm led[6] = {
    Storm( pin[0] ),
    Storm( pin[1] ),
    Storm( pin[2] ),
    Storm( pin[3] ),
    Storm( pin[4] ),
    Storm( pin[5] )
  };
  _led = led;
  ...

You create a array local to the function , and you use its address to initialize your instance variable _led .

What will happen when you exit from the constructor, and the stack space occupied by led[] is reused for other things ? Where will you point _led then?

EDITO

Change your constructor to this; I hope that Arduino supports the dynamic memory of C ++ (should):

Cluster::Cluster( int pin[] ){
  prevmillis = 0;
  counter = 0;

  _led[0] = new Storm( pin[0] );
  _led[1] = new Storm( pin[1] );
  _led[2] = new Storm( pin[2] );
  _led[3] = new Storm( pin[3] );
  _led[4] = new Storm( pin[4] );
  _led[5] = new Storm( pin[5] );
}

And a minor change in your class:

class Cluster {
  ...
private:
  ...
  Storm *_led[6];

};

    
answered by 22.04.2017 / 07:17
source