sort array by counts helpppp

hi all,

I try for days to sorting table but I couldn’t.

I upload the latest update code.

I receive on screen only sorting from low to high all elements butttt :stuck_out_tongue_closed_eyes:

I would like to show only the first of same-repetetive elements and the volume of them.

how can I do?? would you change my little code??

const int analogInPin = A0;  // Analog input pin that the signal pulses from opamp LM833 is attached to
int volEn,a,i,j,savePulse = 0; 
const int Energies = 20;
const int b = 20;
const int c = 20;
int energiesTable[20]{};
int sortTable[20]{};
int volTable[20]{};



void setup() {
  
  Serial.begin(9600);   // initialize serial communications at 9600 bps:
}

void loop() {


///////// read and write in table energies from scin probe    ////////////

        Serial.print("Energy: ");
        
        for( a=0; a<Energies; a++) 
        { 
nextSear:
          int aread = analogRead(analogInPin);
          float readval = (float)aread / 1023.0;
          readval = readval * 5.0;
          readval = readval * 100.0;
          int finalval = int(readval);
          char buffer [3] = {0, 0, 0};
          itoa (finalval,buffer,10);
          savePulse = finalval;

          delayMicroseconds(110); // wait 2 milliseconds before the next loop for the analog-to-digital converter to settle after the last reading

          if(savePulse == 0 || savePulse >= 1024) { goto nextSear; }
        
          else 
          {  
              Serial.print(savePulse);
              Serial.print("-");
              
              energiesTable[a]=savePulse; 
            }
          }    
          
/////////    sorted Energies low to high  //////////       

    
for( i = 0; i < Energies - 1; i++)
    {
      int currentMin = energiesTable[i];
      int currentMinIndex = i;

      for( j = i + 1; j < Energies; j++)
      {
        if(currentMin > energiesTable[j])
        {
          currentMin = energiesTable[j];
          currentMinIndex = j;
        }
      }
           
        if(currentMinIndex != i)
        {
          energiesTable[currentMinIndex] = energiesTable[i];
          energiesTable[i] = currentMin;
        }
    }
    
    Serial.println();
    Serial.print("sorted Energies low to high :");

    for( i = 1; i < Energies; i++)
    {
        Serial.print(energiesTable[i]);
        Serial.print("-");
    }

/////////   Sorted Energies - volume        /////////////
                  
         for(int f = 1; f < Energies; f++)
         { 
           for(int e=f+1; e<Energies; e++) 
           {  
             if(energiesTable[e] != energiesTable[f])
             {    
              sortTable[b] = energiesTable[f];
               volTable[c] = volEn++;
               volEn=0;
               goto nextEn;
             }
             else
             {
               volEn++;
               goto nextEn;
             }     
nextEn:
            int x = 0;            
           }
         }
         
         Serial.println();
         Serial.print("Sort Energies   : ");
         
         for(int g =0; g < b; g++){ Serial.print(sortTable[g]); }
            
         Serial.println();
         Serial.print("volume Energies : ");
        
         for(int k = 0; k < c; k++){ Serial.print(volTable[k]); }
        
         Serial.println();
         Serial.println();
        
delay(5000);  
}//end
       char buffer [3] = {0, 0, 0};
          itoa (finalval,buffer,10);

And if your analogue reading exceeds two decimal digits (very likely) ?

And please, don't use goto. It's simply not necessary.

AWOL:

       char buffer [3] = {0, 0, 0};

itoa (finalval,buffer,10);



And if your analogue reading exceeds two decimal digits (very likely) ?

And please, don't use goto. It's simply not necessary.

dear many thanks for your quick reply!!!!!!

what can I do with char buffer and itoa?

must I do that with first conversation? fron analog to digital? and how?

I see on oscilloscope projects like this...

You could use an adequately-proportioned buffer to write the ASCII value to, to prevent overwriting memory you don't own.

dear many thanks for your quick reply!!!!!!

sorry...

I read again your answer,

how can I read the anaog value on screen?

but and the other problem , how can I sort by ount the elements?
the code is good?

I'm not sure I understand the question; you don't appear to use "buffer" for anything, but you're happy to endure the potential collateral damage associated with writing to it.

nextEn:
      int x = 0;           
    }
  }

Does this look pointless to you?

AWOL:
I'm not sure I understand the question; you don't appear to use "buffer" for anything, but you're happy to endure the potential collateral damage associated with writing to it.

many many thanks!!!!

first, I made simple project

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

I check the ADC ,
I receive about 2 v more or less when have nothing on ADC input
I receive zerroo when I connect pulse positives up to 5v more pulses are about volt
I receive may the correct pulses-voltage when I connect only the ground on arduino from my pcb board about volt or lower , 0,05v 0,70v...

may daMAnge ADC ON Arduino???

dut the project with potensiometer on adc woork correct....give me different numbers - values when I change voltage with trim pot.

john44:
I receive on screen only sorting from low to high all elements butttt :stuck_out_tongue_closed_eyes:

I would like to show only the first of same-repetetive elements and the volume of them.

Well, how would you do this job if you were a computer?

Step through the sorted elements one at a time.
if the element is the same as the previous one, then count it.
if the element is different, then print off the previous element and its count, and start counting from one

You need some extra code to catch the start (when there is no "previous element"), and the end (to cause the final run of elements to be printed).

how can I find if the ADC is good or not?

may this question is frm other category but I have the programming questios

arduino board from china with ch… chip on board seams to be “hard to die” haha…

after day , now I upload with float arrays and float values from ADC pin the corect convertation (no buffer , ita fuction ) and I see on screen ,serial. print,float values!!!

now, I try to show non-repetitive elements of array and counts of them and I can’t…

I upoload the code for this… I see only zerro…on screen…

/////////   Sorted Energies - volume        /////////////
                  
         for(int f = 1; f < Energies; f++)
         { 
           for(int e=f+1; e<Energies; e++) 
           {  
             if(energiesTable[e] != energiesTable[f])
             {    
               sortTable[b] = energiesTable[e];
              
             }
             else { volTable[c] = volEn++; } 
           }
         }
         
         Serial.println();
         Serial.print("Sort Energies   : ");
         
         for(int g =0; g < b; g++){ Serial.print(sortTable[g]); }
            
         Serial.println();
         Serial.print("volume Energies : ");
        
         for(int k = 0; k < c; k++){ Serial.print(volTable[k]); }
        
         Serial.println();
         Serial.println();

where is the error on the following code? it can’t show me the last two 1) show only non repetetive elements and counts of them.

I try to save values in arrays with bubble sort methode but nothing.

Also, the piece of code which show the elements low to high works correct (save on thhe first one for loop array in variable and compare with the next iside for loop the array with first an d second eleent)

/*
  read Analog input 
  
 */

const int analogInPin = A0;  // Analog input pin 
int scinSignal;
int volEn,a,i,j = 0; 
const int Energies = 20;
const int b = 20;
const int c = 20;
int energiesTable[Energies]{};
int sortTable[b]{};
int volTable[c]{};
int Table [c]{};



void setup() {
  
  Serial.begin(9600);   // initialize serial communications at 9600 bps:
}

void loop() {


///////// read and write in table energies from scin probe    ////////////

        Serial.print("Energy: ");
        
        for( a=0; a<Energies; a++) 
        { 
nextSear:
          scinSignal = analogRead(analogInPin);
          delay(scinSignal);

          if(scinSignal == 0 || scinSignal >= 1024) { goto nextSear; }
        
          else 
          {  
              Serial.print(scinSignal);
              Serial.print("-");
              
              energiesTable[a]=scinSignal; 
            }
          }    
          

/////////    sorted Energies low to high  //////////       

    
for( i = 0; i < Energies - 1; i++)
    {
      int currentMin = energiesTable[i];
      int currentMinIndex = i;

      for( j = i + 1; j < Energies; j++)
      {
        if(currentMin > energiesTable[j])
        {
          currentMin = energiesTable[j];
          currentMinIndex = j;
        }
      }
           
        if(currentMinIndex != i)
        {
          energiesTable[currentMinIndex] = energiesTable[i];
          energiesTable[i] = currentMin;
        }
    }
    
    Serial.println();
    Serial.print("sorted Energies low to high :");

    for( i = 1; i < Energies; i++) 
    { 
      Serial.print(energiesTable[i]); 
      Serial.print("-");
    }
    
/////////   Sorted Energies - volume        /////////////
                  
         for(int f = 0; f < Energies; f++)
         { 
          int currentEqu = energiesTable[f];
           for(int e=f+1; e<Energies; e++) 
           {  
             if(currentEqu != energiesTable[e]) {Table[currentEqu]; }
             else { volTable[c] = ++volEn; } 
           }
         }

         
         Serial.println();
         Serial.print("Sort Energies   : ");
         
         for(int g =0; g < b; g++)
         { 
           Serial.print(sortTable[g]);
           Serial.print("-");
         }
            
         Serial.println();
         Serial.print("volume Energies : ");
        
         for(int k = 0; k < c; k++)
         { 
           Serial.print(volTable[k]);
           Serial.print("-");
         }
    
         Serial.println();
         Serial.println();
       
delay(5000);   

}//end