Problem with conversion the int array to one string.

Hello,
Maybe someone can help me with the fallowing issue. I want to read analog values from the photo-diode ten times and each value I assign into array as a int. After that I need to convert each ten values into one string. The purpose for this action is that later I want to simply compare this string with another string.

Many thanks for help.

#define inPin0 0
const int numReadings = 10;
unsigned int analogVals[numReadings];
unsigned int i = 0;
 
void setup(void) {
 
  Serial.begin(9600);
  Serial.println();
     
}
 
void loop(void) {
  int pinRead0 = analogRead(inPin0);
  float pVolt0 = pinRead0 / 1024.0 * 5.0;
  if (pVolt0 >=3) {
    pVolt0 = 1;
  }
    else { 
    pVolt0 = 0;
  }
  Serial.println();
  Serial.println(pVolt0);
 
  analogVals[i] = pVolt0;
  Serial.println(i); //
  i++;
  
    if (i>=numReadings) {
    Serial.println("Wypisuje tablice");
    for(int i=0;i<=9;i++)
     {      
      Serial.print(analogVals[i]);  //Wypisz tablice
     }
  i=0;
  delay(5000);
  }
  delay(1000);
}

After that I need to convert each ten values into one string.

sprintf() can do that.

Comparing one array of ints to another array of ints will be far faster than converting two arrays of ints to strings, and then comparing the strings.

PaulS thanks for tip but I am new in programming microcontrollers and I am afraid that I am not able to use this function. Could you please help me to code it in my case or sent me some examples with explanations?

I am afraid that I am not able to use this function.

There is a difference between not knowing how to use a function and not being able to use a function. There is no valid reason why you can't use the function.

Google can correct the problem with not knowing how to use it.

int stuff[4] = {45, 29, -18, 8};

char sillyString[80];
sprintf(sillyString, "%d, %d, %d, %d", stuff[0], stuff[1], stuff[2], stuff[3]);

At this point sillyString will contain "45, 29, -18, 8".

But I STILL think this is the wrong thing to do.

This would compare two arrays (of integers)

  int ar1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int ar2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  int rc = memcmp(ar1, ar2, sizeof(ar1));

  if (rc)
  {
    Serial.println("mismatch");
  }
  else
  {
    Serial.println("match");
  }

If the arrays are the same, 'rc' will be 0. Note that you can only use the size of the smallest array (if they are not the same size).

memcmp

Thanks to your help I am a little bit further with my work. Now my code looks as fallows:

#define inPin0 0
#include <Wtv020sd16p.h>

int resetPin = 2;  // The pin number of the reset pin.
int clockPin = 3;  // The pin number of the clock pin.
int dataPin = 4;  // The pin number of the data pin.
int busyPin = 5;  // The pin number of the busy pin.
const int numReadings = 10;
unsigned int analogVals[numReadings]; //Deklaracja tablicy
unsigned int i = 0;

Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);

void setup(void) {
  Serial.begin(9600);
  wtv020sd16p.reset();
  Serial.println();    
}
 
void loop(void) {
  int pinRead0 = analogRead(inPin0);
  float pVolt0 = pinRead0 / 1024.0 * 5.0;
  if (pVolt0 >=3) {
    pVolt0 = 1;
  }
    else { 
    pVolt0 = 0;
  }
  Serial.println(); //Aktualnie odczytana wartosc
  Serial.println(pVolt0);
 
  analogVals[i] = pVolt0;  //Przypisz wartosc do i-tego miejsca w tablicy
  Serial.println(i); // Aktualne polozenie w tablicy
  i++;
  
    if (i>=numReadings) { //Jesli zdefiniowan tablica pelna to
    Serial.println("Odebrane dane:");
        for(int i=0;i<=9;i++) {      
          Serial.print(analogVals[i]);  //Wypisz tablice
         }
    
        int pattern[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; //Deklaracja wzorca
        int pattern2[] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 1}; //Deklaracja wzorca
        int pattern3[] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1}; //Deklaracja wzorca

         //int compare = memcmp(analogVals, pattern, sizeof(analogVals));
           int compare;
            if (compare = memcmp(analogVals, pattern, sizeof(analogVals))){
              Serial.println("\nMatch with patter no. 1");
              wtv020sd16p.asyncPlayVoice(0);
              wtv020sd16p.pauseVoice();
              delay(2000);
            }
            else if (compare = memcmp(analogVals, pattern2, sizeof(analogVals))){
              Serial.println("\nMatch with patter no. 2");
              wtv020sd16p.asyncPlayVoice(1);
              wtv020sd16p.pauseVoice();
              delay(2000);
            }
           else if (compare = memcmp(analogVals, pattern3, sizeof(analogVals))){
              Serial.println("\nMatch with patter no. 3");
              wtv020sd16p.asyncPlayVoice(2);
              wtv020sd16p.pauseVoice();
              delay(2000);
           }           
      i=0; 
      delay(5000);
      }
  delay(1000); 
}

The idea is to play different audio files depending on the signal received by photo diode. So I created three patterns and used "memcmp" to define which audio file will be played. But now i can only match with patterns 1 and 2. Any tips?

if(compare = .... should be if (compare == .... Not behind a pc at this moment to check more but that is what I noticed.

sterretje:
if(compare = .... should be if (compare == .... Not behind a pc at this moment to check more but that is what I noticed.

I works well now, thanks!

Amator90:
I works well now, thanks!

I can't imagine how. There was no point in having the compare variable at all. Being a local variable, with no initializer, you have no idea what value it contains. What you were doing was assigning a value to compare in the if statement. The assignment operator returns a value - the value that was assigned.

So, your if statement was equivalent to:

compare = memcmp(...);
if(compare)

If you changed the if statement to if(compare == memcmp(...)), you changed the meaning to if(someUndefinedCrap == memcmp(...)), which makes no sense. So, how can that be said to work?

OOPS again, should have seen that :frowning: