HELP, Is it possible to put a single array inside a multidimensional array

Hello,

I need help with this code I am writing.

I have been writing a piece of code and it would be great for the purpose if I could contain a single array within a multidimensional array. I hope to perform smoothing on the readings.Also how would I call it later for the smoothing.

The reason I want it is so I can store sensor information for the robot I am working on.

The array layout would need to look something like this:

ARRAY: INFO[ finger 1 average, finger 1 total, array of ten last readings of this finger, finger 2 average, finger 2 total, array of ten last readings of this finger, finger 3 average, finger 3 total, array of ten last readings of this finger, finger 4 average, finger 4 total, array of ten last readings of this finger, finger 5 average, finger 5 total, array of ten last readings of this finger, elbow emg average, elbow total, array of the ten last readings of this finger,

How would I do this?

Thanks

Why not use a two-dimensional array? It’s easy to set it to zero and manipulate it as you need:

#define FINGERS      6
#define SAMPLES     10

int myData[FINGERS][SAMPLES];

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

  Serial.begin(9600);
  FillArray();
  ShowArray();
  Serial.println("========================");
  ZeroOut();
  ShowArray();

}

void ShowArray()
{
  for (int i = 0; i < FINGERS; i++) {
    for (int j = 0; j < SAMPLES; j++) {
      Serial.print(myData[i][j]);
      Serial.print("  ");
    }
    Serial.println();
  }
  
}
void FillArray()
{
    for (int i = 0; i < FINGERS; i++) {
    for (int j = 0; j < SAMPLES; j++) {
      myData[i][j] = i + j;
    }
  }
}


void ZeroOut()
{
  memset(myData, 0, sizeof(myData));
}

void loop() {
}

Start by commenting out stuff which does not compile or is really not required at this point.
Partially done.
Than set your preferences to “verbose all” and “add lines”.
Than troubleshoot each and every typo error compiler reports.
Than do same for syntax errors. Not to worry about if it is logically correct.

After it compiles add some data emulation - will help you with it depending on your response.

DO NOT ATTEMPT to troubleshoot / fix (improve ) arrays , not at this point!
It is easier to do that later when the code does function as required.
It is better to make it work first “long hand” before any optimization is attempted - as long as it fits into RAM OK.
.
.

//#include <SoftwareSerial.h> //Libraries not used now willbe used later

#include <LiquidCrystal.h> //Change Pins

#include <Wire.h>

#include <Servo.h>

Servo finger1;
Servo finger2;
Servo finger3;
Servo finger4;
Servo finger5;

Servo elbowServo;
//research BLE protocal and find seeedstudio source code for BLE shield v1
//Add LED's to show when it receives the command and when it is supposed to work
//Add ensure LCD runs accurately
// D research on encoder code
///////////////////////////////////////////////////////////////////
int servoPin1 = 11;
int servoPin2 = 3;
int servoPin3 = 5;
int servoPin4 = 6;
int servoPin5 = 9;
int emgPin = 12;

int elbowServoPin = 10;

int emgVal;
int flexVal1;
int flexVal2;
int flexVal3;
int flexVal4;
int flexVal5;

int servoVal1;
int servoVal2;
int servoVal3;
int servoVal4;
int servoVal5;

int servoValElbow;

const int flexPin1 = A0;
const int flexPin2 = A1;
const int flexPin3 = A2;
const int flexPin4 = A3;
const int flexPin5 = A4;

const int emgpin = A5;

// also do wrist pin - potentiometer

int numReadings = 10; HERE 


int R1[numReadings]; // array to store finger 1 readings
int R2[numReadings]; // array to store finger 2 readings
int R3[numReadings]; // and so forth
int R4[numReadings];
int R5[numReadings];
int R6[numReadings]; // array to store emg
int* readings[6] = {R1, R2, R3, R4, R5, R6}; //array to store arrays

int readIndex[10]; //keeps the index of the smoother for the certain reading
int* total[10]; //keeps the totals for the designated finger or elbow
int* singleTotal; //variable to assign the designated total down in smoother function
int* average[10]; //keeps the designated average

void setup() {
  Serial.begin(115200);
  finger1.attach(servoPin1);
  finger2.attach(servoPin2);
  finger3.attach(servoPin3);
  finger4.attach(servoPin4);
  finger5.attach(servoPin5);
  elbowServo.attach(elbowServoPin);
  /*
    readings[1] = readings1;
    readings[2] = readings2;
    readings[3] = readings3;
    readings[4] = readings4;
    readings[5] = readings5;
    readings[6] = readings6;
  */
  for (int n = 1 ; n < 7; n++) { //I NEED THIS FIXED to set all readings to 0
    for (int thisReading = 0; thisReading < numReadings; thisReading++) {
      readings[n] = 0;
      //Give intro for program on lcd
      //Start the BLE conncetion with myo
    }
  }
}

void loop() {
  //get the bluetooth connection sending info
  // if statements to deduce movement
  //create function(s) for movement

  int pos2;

  emgVal = analogRead(emgPin); //read values of elbow EMG
  flexVal1 = analogRead(flexPin1); //read values of fingers
  flexVal2 = analogRead(flexPin2);
  flexVal3 = analogRead(flexPin3);
  flexVal4 = analogRead(flexPin4);
  flexVal5 = analogRead(flexPin5);
  servoVal1 = smoothing(flexVal1, 1); //send values to smoothing function
  servoVal2 = smoothing(flexVal2, 2);
  servoVal3 = smoothing(flexVal3, 3);
  servoVal4 = smoothing(flexVal4, 4);
  servoVal5 = smoothing(flexVal5, 5);
  servoValElbow = smoothing(emgVal, 6);
  int servoVal1 = map(servoVal1, 560, 740, 0, 180);
  servoVal1 = constrain(pos2, 0, 180);
  int servoVal2 = map(servoVal1, 560, 740, 0, 180);
  servoVal2 = constrain(pos2, 0, 180);
  int servoVal3 = map(servoVal1, 560, 740, 0, 180);
  servoVal3 = constrain(pos2, 0, 180);
  int servoVal4 = map(servoVal1, 560, 740, 0, 180);
  servoVal4 = constrain(pos2, 0, 180);
  int servoVal5 = map(servoVal1, 560, 740, 0, 180);
  servoVal5 = constrain(pos2, 0, 180);
  servoValElbow = map(servoValElbow, 200, 400, 0, 180);
  servoValElbow = constrain(sevoValElbow, 0, 180);
  finger1.write(servoVal1); //write smoothed values to servos
  finger2.write(servoVal2);
  finger3.write(servoVal3);
  finger4.write(servoVal4);
  finger5.write(servoVal5);
  elbowServo.write(servoValElbow);
  //display information on serial and LCD
}

int smoothing(int val, int n) { //I NEED HELP with this as well
  total[n] = total[n] - readings[n][readIndex[n]];
  readings[n][readIndex[n]] = val;
  total[n] = total[n] + readings[n][readIndex[n]];
  readIndex[n] = readIndex[n] + 1;
  if (readIndex[n] >= numReadings) {
    readIndex[n] = 0;
  }
  singleTotal = total[n];
  //average[n] = (int) singleTotal / (int) numReadings;
  //return average[n];
  delay(2);
}

//Help is much appreciated

I would use a struct to hold the information for one finger and an array of the structs to hold all finger information like this:

typedef struct fingerdata {
  int average;                  // finger average
  int total;                    // finger total
  int readings[10];             // finger readings

};

fingerdata fingers[10];        // data for all fingers

// average  for finger i =>    fingers[i].average
// total for finger    i =>    fingers[i].total 
// readings for finger i =>    fingers[i].readings[j]  with j = 0 to 9

+1 Blue Eyes.

You can go beyond struct and use a C++ class as a struct packaged with functions.

Paul Murray CBR's Object Oriented page.

It's a nice get you going tutorial with some depth. He doesn't go into .h and .cpp files and I know that there will be people who would arrange the code differently but it works and it is C++.

Arduino compiler can be a real PITA when making your own library (it used to be worse). What Paul shows avoids those bits.

Really though, C++ classes open up loads of flexibility and power.

KEisler: ARRAY: INFO[ finger 1 average, finger 1 total, array of ten last readings of this finger, finger 2 average, finger 2 total, array of ten last readings of this finger, finger 3 average, finger 3 total, array of ten last readings of this finger, finger 4 average, finger 4 total, array of ten last readings of this finger, finger 5 average, finger 5 total, array of ten last readings of this finger, elbow emg average, elbow total, array of the ten last readings of this finger,

How would I do this?

struct info {
  float average;
  int total;
  int reading[10];
};

struct info finger[5];
struct info elbow;

void putReadngIntoInfo(struct info &info, int readingNo, int reading) {
  info.total -= info.reading[readingNo];
  info.reading[readingNo] = reading;
  info.total += info.reading[readingNo];
  info.average = info.total/10;
}

void putReadingIntoFinger(int whichFinger, int readingNo, int reading) {
  putReadngIntoInfo(finger[whichFinger], readingNo, reading);
}

void putReadingIntoElbow(int readingNo, int reading) {
  putReadngIntoInfo(elbow, readingNo, reading);
}