How to use array in Xiao or QT PY

something strange going on with cortexM0 i.e. seeed xiao or adafruit qt py..
arrays dont work.. I am using plain ol code but cant make a simple array work..
everything compiles, just doesnt work at runtime.. get crazy results.. does anyone have gone thru this and figured it out?? regards--

Code?

int16_t scanVitals(){
               uint32_t adc=0;
               const uint32_t OFFSET=300;
               const int16_t comp=540; //compensate for high current pulling up voltages    .1v per amp..approx
               int16_t WTTS=0;
               volatile static int32_t pvv[]  ={0,0,0,0,0,0,0,0,0,0,0,0};
               volatile static int32_t pvi[]  ={0,0,0,0,0,0,0,0,0,0,0,0};
               volatile static int32_t battv[]={0,0,0,0,0,0,0,0,0,0,0,0};
               //memcpy(pvv, sizeof(pvv));
               int16_t        x=0;
               static int16_t y=0;
               int16_t        z=0;

               y++;
               if(y>=10)y=0;  //array loop cntr
                 
               //get PVcurrent FIRST
               for(x=0; x<=5; x++){adc=analogRead(PViIN);}
               adc=0;
               for(x=0; x<=20; x++){adc+=analogRead(PViIN);}
               if(adc>=OFFSET)adc-=OFFSET;  //subt offset
               pvi[y]=adc/270;  //PVi= scale factor
               PVi=0;
               for(y=0; y<=9; y++){PVi+=pvi[y];}
               PVi=PVi/10;
                
               //PVv
               for(x=0; x<=5; x++){adc=analogRead(PVvIN);}
               adc=0;
               for(x=0; x<=20; x++){adc+=analogRead(PVvIN);}
               if(adc>=OFFSET)adc-=OFFSET;

               adc=adc-(adc/comp)*(PVv/100);
               pvv[y]=adc/54;  //PVv=scale factor
               PVv=0;
               for(y=0; y<=9; y++){PVv+=pvv[y];}
               PVv=PVv/10;
               
               //BATTv
               for(x=0; x<=5; x++){adc=analogRead(BATTvIN);}
               adc=0;
               for(x=0; x<=20; x++){adc+=analogRead(BATTvIN);}
               if(adc>=OFFSET)adc-=OFFSET;  //subt offset

               battv[y]=adc/54;  //BATTv= scale factor
               BATTv=0;
               for(y=0; y<=9; y++){BATTv+=battv[y];}
               BATTv=BATTv/10;

               //calculate BATTi
               BATTi=(PVv*PVi)/BATTv;

               adc=adc-(adc/comp)*(BATTi/100);
               BATTv=adc/54;  //BATTv= scale factor
               
               for(x=0; x<=5; x++){adc=analogRead(TEMP_IN);}
               adc=0;
               for(x=0; x<=20; x++){adc+=analogRead(TEMP_IN);}
               //static int32_t tt=0;
               //if(tt!=TPv)Serial.println(TPv);
               if(adc>=OFFSET)adc-=OFFSET;  //subt offset
               int16_t TPv=adc/21;
               //static int16_t tt=0;
               //if(tt!=TPv)Serial.println(TPv);
               TEMPdeg=calcTemp(TPv);    
        
               return (PVv*PVi)/100;  //WATTS
}

So what is happening exactly that is unexpected?

the reads from the arrays are various and nonsense.. not the same results from each array..different results.. I think I am crossing some 4 byte boundary or something like that..

my code was running just fine but the digits were flickering somewhat, so I wanted to slow down the readings and average them using arrays 10 deep to
average out the readings.. bang..no matter what I do, doesnt work.. 5 hrs nowand no idea ...

its nice to have the 48mhz speed that xiao has, but to make it human-friendly to see I wouldlike to slow down the readouts..using arrays seemed like the logical way to do that..

A couple of things...

First time through routine y will be 1, not 0, as you increment it before using it.

               static int16_t y=0;
               int16_t        z=0;

               y++;

You use y in a number of for loops, so when returning it will have a high value (for example it will be 10 after completing the for loop below.

for(y=0; y<=9; y++){PVi+=pvi[y];}

On a subsequent call to the routine it will be reset back to 0...

               if(y>=10)y=0;  //array loop cntr

...so these statements will not use the correct index.

               pvi[y]=adc/270;  //PVi= scale factor
               pvv[y]=adc/54;  //PVv=scale factor

               battv[y]=adc/54;  //BATTv= scale factor

int16_t scanVitals(){
                uint32_t adc=0;
                const uint32_t OFFSET=300;
                const int16_t comp=540; //compensate for high current pulling up voltages    .1v per amp..approx
                int16_t WTTS=0;
                volatile static int32_t pvv[]  ={0,0,0,0,0,0,0,0,0,0,0,0};
                volatile static int32_t pvi[]  ={0,0,0,0,0,0,0,0,0,0,0,0};
                volatile static int32_t battv[]={0,0,0,0,0,0,0,0,0,0,0,0};
                //memcpy(pvv, sizeof(pvv));
                int16_t        x=0;
                static int16_t y=0;
                int16_t        z=0;

                y++;
                if(y>=10)y=0;  //array loop cntr
                  
                //get PVcurrent FIRST
                for(x=0; x<=5; x++){adc=analogRead(PViIN);}
                adc=0;
                for(x=0; x<=20; x++){adc+=analogRead(PViIN);}
                if(adc>=OFFSET)adc-=OFFSET;  //subt offset
                pvi[y]=adc/270;  //PVi= scale factor
                PVi=0;
                for(z=0; z<=9; z++){PVi+=pvi[z];}
                PVi=PVi/10;
                 
                //PVv
                for(x=0; x<=5; x++){adc=analogRead(PVvIN);}
                adc=0;
                for(x=0; x<=20; x++){adc+=analogRead(PVvIN);}
                if(adc>=OFFSET)adc-=OFFSET;

                adc=adc-(adc/comp)*(PVv/100);
                pvv[y]=adc/54;  //PVv=scale factor
                PVv=0;
                for(z=0; z<=9; z++){PVv+=pvv[z];}
                PVv=PVv/10;
                
                //BATTv
                for(x=0; x<=5; x++){adc=analogRead(BATTvIN);}
                adc=0;
                for(x=0; x<=20; x++){adc+=analogRead(BATTvIN);}
                if(adc>=OFFSET)adc-=OFFSET;  //subt offset

                battv[y]=adc/54;  //BATTv= scale factor
                BATTv=0;
                for(z=0; z<=9; z++){BATTv+=battv[z];}
                BATTv=BATTv/10;

                //calculate BATTi
                BATTi=(PVv*PVi)/BATTv;

                adc=adc-(adc/comp)*(BATTi/100);
                BATTv=adc/54;  //BATTv= scale factor
                
                for(x=0; x<=5; x++){adc=analogRead(TEMP_IN);}
                adc=0;
                for(x=0; x<=20; x++){adc+=analogRead(TEMP_IN);}
                //static int32_t tt=0;
                //if(tt!=TPv)Serial.println(TPv);
                if(adc>=OFFSET)adc-=OFFSET;  //subt offset
                int16_t TPv=adc/21;
                //static int16_t tt=0;
                //if(tt!=TPv)Serial.println(TPv);
                TEMPdeg=calcTemp(TPv);    
         
                return (PVv*PVi)/100;  //WATTS
}
THANKS.. I setup 'z' to do the averaging and then must have had a brain malfunction and used y..dang..looks like it is working.. many thanks..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.