array

How can u combine several arrays into one?

int all [15];

all = [rpm,pressure,temperatures];// Does not work

where they all are one dimensionals arrays.

Kim

You could either do it with a structure:

typedef struct {
  int temp;
  int pressure;
  int rpm;
} engineVals;

engineVals myData [15];

or with an 'n' dimensional array:

int engineVals [N_VALS][3];
or with three one dimensional arrays.

Lots of ways.

well :
maybe i should clearify that rpm, pressure and temperature are arrays:

int rpm[3] = {1,2,3};
int pressure[5] = {4,5,6,7,8};
int tempertures[4] = {9,10,11,12};

then i want my all array to look like:

all = {1,2,3,4,5,6,7,8,9,10,11,12,13,14}

so i can loop through it and print it to the serial port:

for(int i=0; i<14;i++){
Serial.print(all*);*
Serial.print("\t");
}
Serial.println();

If you actually want to store rpm, pressure and temperature 15 times you should do:

struct data {
  int rpm;
  int pressure;
  int temperature;
};

data all[15];

//later
int somePressure = all[2].pressure; //retrieve the pressure stored at index 2

[edit]I see AWOL got to it before I did :-[ :D[/edit]

Or:

[BEWARE OF ARRAY BUONDS]

int rpm[3] = {1,2,3,4,5,6,7,8,9,10};
int pressure[5] = {11,20,30,40,50,60,70,80,90,100};
int temperature[7] = {101,200,300,400,500,600,700,800,900,1000};

int* all[3] = { rpm, pressure, temperature };

int somePressure = all[1][4]; //50

Well here is the complete code: Any suggestion to improvement would be great:)

#include <MegaServo.h>

// Constants regarding logging
int loggingIntervalDefault = 1000; //ms
long previousPrint = 0;
int baudrate = 19200;
byte switchPin1 = 12;
bute switchPin2 = 16;

numberOfSensores = 13;
int* all[numberOfSensores];
char* deliminter = "\t";

// constants regarding moving the servo for the webcam
int servoPot  = 12;
int minPos   = 0;
int maxPos   = 180;
byte servoPin = 9;
boolean servoDirection = 0;
int servoPos = 0;
MegaServo servo;

// constants for RPM
volatile byte rpmcount = 0;
unsigned int rpm = 0;
unsigned long timeold = 0;
byte rpmDisplayPin = 11;
int rpmMax = 4000;
int numberOfSamples = 400;

// constants for pressure sensors
byte pressurePins[5] = {3,0,2,1,13};
int pressureMax[5] = {10,10,10,10,500};
// array holding the values
byte pressure[5]     = {0,0,0,0,0};

// constants for temperture sensors
byte temperturePins[5] = {9,10,6,7,8};
// array holding the values
byte temperatur[5]     = {0,0,0,0,0};

// constants for volume flow sensors
byte flowPins[2] = {5,4};
int flowMax[5] = {40,150};
// array holding the values
byte flow[2]     = {0,0};



// constants for multiplexer
byte S0 = 16;
byte S1 = 6;
byte S2 = 7;
byte S3 = 8;
byte signal = 0;

byte logicArray [16][4] = { {0,0,0,0},
                          {1,0,0,0},
                          {0,1,0,0},
                          {1,1,0,0},
                          {0,0,1,0},
                          {1,0,1,0},
                          {0,1,1,0},
                          {1,1,1,0},
                          {0,0,0,1},
                          {1,0,0,1},
                          {0,1,0,1},
                          {1,1,0,1},
                          {0,0,1,1},
                          {1,0,1,1},
                          {0,1,1,1},
                          {1,1,1,1}};

byte pinSelect[4] =  {S0,S1,S2,S3};




void setup() {
 // Multiplexer
 pinMode(S0, OUTPUT);    // sets the digital pin as output
 pinMode(S1, OUTPUT);    // sets the digital pin as output
 pinMode(S2, OUTPUT);    // sets the digital pin as output
 pinMode(S3, OUTPUT);    // sets the digital pin as output

 // Web camera
 servo.attach(servoPin,800,2200);

 //rpm
 attachInterrupt(0, rpm_fun, RISING);

 // Write
 Serial.begin(baudrate);
 writeHeader();

}
////////////////////////////////////////////////////////////

void loop()
{
Serial.print(millis());
Serial.print("\t");

// Move camera
moveServo(servoPos);

// Read rpm
rpmRead();

// Read pressure sensors
readPressures();

// Read temperatures
readTemperatures();

// Read volume flow
readVolumeFlow();

// Write data to serial port
write2Serial();


}


void write2Serial(){
if(digitalRead(switchPin1) == HIGH){
       loggingInterval = loggingIntervalDefault;// logging at given interval
}
else if(digitalRead(switchPin2) == HIGH
       loggingInterval = 0;//logging as fast as possible
}
else{
 return;// No logging
}

 if(millis()-previousPrint>loggingInterval){

   all = [rpm,pressure,tempertures,flow];
   for(byte i=0;i<numberOfSensores ;i++){
      Serial.print(all[i];
      Serial.print(deliminter);
   }
   Serial.println("");
   previousPrint = millis();
 }

}


void writeHeader(){

  Serial.print("RPM");
  Serial.print(deliminter);

 for(byte i=0;i<5 ;i++){
    Serial.print("PT");
    Serial.print(i);
    Serial.print(deliminter);
 }
 for(byte i=0;i<5 ;i++){
    Serial.print("TE");
    Serial.print(i);
    Serial.print(deliminter);
 }
 for(byte i=0;i<2 ;i++){
    Serial.print("VF");
    Serial.print(i);
    Serial.print(deliminter);
 }

 Serial.println("");
}

void readPressures(){
  for(byte i=0;i<5 ;i++){
   pressure[i] = map(readAnalog(pressurePins[i]),0,1023,0,pressureMax[i]);
  }
}



void readTemperatures(){
  for(byte i=0;i<5 ;i++){
   temperatur[i] = map(readAnalog(temperturePins[i]),0,1023,0,100);
  }
}

void readVolumeFlow(){
  for(byte i=0;i<2 ;i++){
   flow[i] = map(readAnalog(flowPins[i]),0,1023,0,flowMax[i]);
  }
}

void moveServo(){

 servoPos = readAnalog(servoPot);

 if(servoDirection){
      servo.write(map(servoPos, 0,1023,minPos,maxPos));
 }
 else{
      servo.write(map(servoPos, 0,1023,maxPos,minPos));
 }

}

int readAnalog(byte pinNumber){

 for(byte i=0;i<4;i++){
        digitalWrite(pinSelect[i],logicArray[pinNumber][i]);
 }
 delay(2);
 return analogRead(signal);

}





 void rpmRead()
 {
  if (rpmcount >= numberOfSamples ) {
    //Update RPM every numberOfSamples  counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = 30*1000*rpmcount/(millis() - timeold);
    timeold = millis();
    rpmcount = 0;
    analogwrite(rpmDisplayPin,map(rpm,0,rpmMax,0,255);
  }
 }


 void rpm_fun()
 {
  rpmcount++;
  //Each rotation, this interrupt function is run twice
 }

Well, my first suggestion would be to separate constants and variables:

// constants for pressure sensors
byte pressurePins[5] = {3,0,2,1,13};
byte pressure[5]     = {0,0,0,0,0};
int pressureMax[5] = {10,10,10,10,500};

Something tells me only two of those arrays are constants.

// constants for volume flow sensors
byte flowPins[2] = {5,4};
byte flow[2]     = {0,0};
int flowMax[5] = {40,150};

And beware cut'n'paste programming. :wink:

Updateted the code as u told me to, thanks

Kim

Another thing is, you've got a lot of "2" and "5" literals scattered around - it would be a good idea to convert these to constants too.

How can i make it a one dimensional array and not a two dimmensional array?

As you are only storing 0 or 1's in the array then you can store them as bit patterns in bytes:-

byte logicArray [16][4] = { {0,0,0,0},
                          {1,0,0,0},
                          {0,1,0,0},
                          {1,1,0,0},
                          {0,0,1,0},
                          {1,0,1,0},
                          {0,1,1,0},
                          {1,1,1,0},
                          {0,0,0,1},
                          {1,0,0,1},
                          {0,1,0,1},
                          {1,1,0,1},
                          {0,0,1,1},
                          {1,0,1,1},
                          {0,1,1,1},
                          {1,1,1,1}};
byte logicArray [16] = { 0,
                          8,
                          4,
                          12,
                          2,
                          10,
                          6,
                          14,
                          1,
                          9,
                          5,
                          13,
                          3,
                          11,
                          7,
                          15};

Then when you recover the byte just extract the appropriate bit.

something like this:-

int readAnalog(byte pinNumber){
int mask = 1;
int point;
 for(byte i=0;i<4;i++){
        if((logicArray[i] & mask) != 0) point = 1; else point = 0;
        digitalWrite(pinSelect[i],point);
   mask = mask <<1;
 }
 delay(2);
 return analogRead(signal);

}

Note - not tested.