# The trigger count is not constant

Everytime I try to run my 2 codes, the trigger count fluctuates, I started understanding that it is due to memory access.
When I increase the number of iterations for the h loop above 1, the number of trigger count starts changing every run. Do you have any clearer explanation behind this? My assumption that after a certain amount of memory accesses, something random in accessing memory or getting/storing data starts to occur which causes fluctuations in the trigger count.

``````void Training(float x_coords[], float y_coords[], float *cx, float *cy, int k, int num_centers, int num_coords, struct Point arr1[])
{
int count;
float min, from1st, from2nd;
// mean & their pointers
float mean_x[k], mean_y[k];
// sum & their pointers
float sum_x[k], sum_y[k];
// prev & their pointers
float prev_x[k], prev_y[k];
// number of items in the cluster
int c1 = 0, c2 = 0;
int counters[k];
// Initialize Means
// check this part
for (int a = 0; a < k; a += 1)
{
if (a%2==0){
mean_x[a] = x_coords[a];
mean_y[a] = y_coords[a];
}
else{
mean_x[a] = x_coords[num_coords - a];
mean_y[a] = y_coords[num_coords - a];
}
}
count = 0;
int l = 0;
int cluster;

**for (int h=0;h<10;h++)**
**    {**
l++;
h++;

for (int a = 0; a < k; a++)
{
sum_x[a] = 0;
sum_y[a] = 0;
counters[a] = 0;
c1 = 0;
c2 = 0;
}
trigger_high();
for (int i = 0; i < 100; i++) // 100
{ // printf("henaaa %d \n",i);
cluster = 0;
min =10000;
// Determine Which cluster
for (int d = 0; d < k; d++)
{
float diff_x=x_coords[i] - mean_x[d];
float diff_x_2=diff_x*diff_x;
float diff_y=y_coords[i] - mean_y[d];
float diff_y_2=diff_y*diff_y;
float dist=diff_x_2+diff_y_2;
if (dist < min)
{
min = dist;
cluster = d;
arr1[i].val = d;
}
}
sum_x[cluster] = sum_x[cluster] + x_coords[i];
sum_y[cluster] = sum_y[cluster] + y_coords[i];
counters[cluster]++;
}
trigger_low();
for (int z = 0; z < k; z++)
{
prev_x[z] = mean_x[z];
prev_y[z] = mean_y[z];
mean_x[z] = sum_x[z] / counters[z];
mean_y[z] = sum_y[z] / counters[z];
}
int flag = 1;
for (int h = 0; h < k; h++)
{
if (mean_x[h] != prev_x[h] && mean_y[h] != prev_y[h])
{
flag = 0;
break;
}
}
if (flag == 1)
count++;
if (count >2)
{
// printf("count aktr mn 2 \n");
for (int f = 0; f < k; f++)
{
cx = cx + f;
cy = cy + f;
*cx = mean_x[f];
*cy = mean_y[f];
}
break;
}

}

}

int main(void)
{
platform_init();
init_uart();
trigger_setup();
float x1[1];
int num_centers = 20;
int num_coords = 100;
int k = 2;
// centroids
float cx[k];
float cy[k];
struct Point arr1[num_coords];
float x_coords[100] =
{-0.23326076237304383, -2.4185052566839387, 5.5746077216218675, -4.142540493328543, -1.4641026939096018, -3.914324962351639, -3.8654234641273595, -1.8233032077374678, 5.441026659933295, 2.045369270069701, 1.357006356657355, 0.14772928629623516, -2.2359877859851327, 1.408400792029129, 1.8505427054589842, 0.5611701693559632, 3.163541765079106, -0.9266962024963958, -1.047461003920516, -0.5526864667084654, 0.8917233935305304, -0.5284095337312298, 1.5085485053077536, -2.8351849169107055, 3.259383253987475, -1.710129605034532, 4.6671421207296575, -0.6831991548995977, -6.682699368955741, -1.6361213689991505, 0.7498068662679656, -1.6210390350576258, -0.16342146774106192, 1.441112355637244, 0.041612482164377074, -1.7456250146032315, 1.1135456304508333, -2.5191794438035746, -0.18571037367736176, -0.06746636943196282, 0.6666135231658215, -1.041111264781358, -2.7821160016032835, 2.307859776782772, 2.949982044051046, 1.1758217456959987, 1.251257408578737, 1.601095012777076, 1.0077701407089565, -0.25448070190307126, 3.897505632471119, 2.3732573977378744, -0.3549614710886453, 0.9477220076055473, 7.824093600874032, -1.463168767586867, -1.2578824968524738, 5.619356898489784, 4.8970234247541224, -1.6870871643438434, -0.028436057761218603, 3.7850399712777008, 4.37063917507388, 4.625759357140952, -0.9453785207358161, 5.458507298019313, -1.3871384358004293, 0.12441231293420651, 4.90352994221016, -0.5561453341601239, 7.657944402712587, 9.204701846024705, 1.9154731834991992, 3.2432876594178137, 3.184544114899766, 4.512608792025447, 0.03226953946074973, 4.394472132176397, -2.1693888166547826, -0.17899694471244176, 2.183295002079142, 2.555288007550465, 1.9374190667503912, 0.9932857876724261, 3.625097503028139, 1.759909948156581, 7.785533950509318, 6.625722169715258, 0.6886700108808486, 4.817843992775335, 3.9309618745144403, -1.8924547101016858, 3.8265353311232633, 4.072151792470184, 5.435175287752399, 6.578944048996398, 5.354246309094218, 3.5695786751250123, 0.8144105791536358, 8.544975369274947};
float y_coords[100] =
{7.971122538595672, 2.2158387637315475, 8.711863637149245, 10.156113513143758, 6.728527216885815, 5.692134094025713, 3.665566486806484, 1.6988162679854857, 9.99145485554014, 6.423506832024234, 5.509755417782495, 2.9269635447774363, 7.4671425082418, 8.666607848337314, 6.0376918248860365, 6.686259821388894, 4.6907360597206225, 3.2155638294869755, 2.942447214782818, 2.989564422614831, 5.588782939039643, 6.627858519235038, 3.0984445188236043, 7.211977451922424, 4.6688123769268755, 5.464494821026176, 7.910926873801624, 4.248712524225496, 6.264643113769472, 2.5672383331551436, 6.445454845375963, 3.4307525891425783, 5.526707446158144, 5.438274886254911, 4.472283354137626, 4.459623514836807, 3.7422357773708894, 7.006266788310951, 3.3968790757223832, 4.77283423476033, 5.535582833263507, 3.2251278428267662, 6.63625839494412, 5.304810309571191, 4.876821349999028, 5.211203020667734, 3.3050328473271224, 7.2337044368995285, 9.661398809165895, 2.5154112338133885, 3.664283674637517, -0.3049437313832808, -1.1709856733126647, 0.1795261272101577, 5.33920803424121, -0.8750604795466401, 1.05415889772086, 1.8484914957124858, 0.4326333806654343, -1.638602077223391, 0.44876003895492456, 0.27276739320395227, 3.3681761218291313, -1.0554131199625036, -3.736649630394898, 3.6883125361509874, -0.41579647429536526, -5.7725457967353435, 1.1603173840935095, 0.7618907750811537, 3.6157976347640925, 3.7311021209091786, 1.7415001095432157, -2.3815208662535805, -2.4005387118146464, 3.764012820011337, 0.9931553347609904, 0.5993098663874319, 0.04373351591146424, -1.5816519560391065, 2.2493570546274504, 2.8027579706142562, -2.6066168332856554, -3.2271902203161194, 0.38302466627119247, -1.0927711991483853, -0.059309696347904506, 0.2605101440210511, 0.950101137013107, 1.8538466187670004, -3.908509306882306, -0.4870901545061901, 3.8497157025552964, 2.120049168661268, -2.342130865152333, 1.3719536878191567, 2.8634548521057304, 2.077292266229064, -1.3447007743843367, 4.907247508247113};
int labels[100] =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
delay_2_ms();
my_puts("fl100100");
for (int i = 0; i < num_coords; i++)
{ // creating the samples in point structures
arr1[i].x = x_coords[i];
arr1[i].y = y_coords[i];
}

trigger_high();
trigger_low();
return 1;
}

``````
``````
void predict_SimpleCNN(float const * const x, float * pred) {
auto layer_0_output = x;

// Layer 1: Gemm
for (int d = 0; d < 8; d++) {
layer_1_output[d] = layer_1_bias[d];
}
for (int d = 0; d < 8; d++) {
for (int i = 0; i < 16; i++) { //when this loop is i<anything greather than 16, the trigger count starts fluctuating
layer_1_output[d] += layer_1_weight[d][i] ;
}
}
``````

I am using Atxmega/STM32F3 with CWLITE