saving into an array

Greetings,

i am half way through to getting my work done, but am still struggling in this array issue..and would be more than thankful to get some help.
i have a device that sends 2 sensor values if it is subjected to external power.

#include <>
#include <>

ASZ_11_CX Asz_Sens;

uint16_t first_samp, second_samp;
volatile bool tgr_lt = false;

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void checkSensorActivity()
{
  if(Asz_Sens_1.GetValue(&Asz_Sens2))
    tgr_lt = true;
}

void loop() {

     if(tgr_lt)
    {
       tgr_left = false; 
      first_samp = 1024 - Asz_Sens.x;
      second_samp = 1024 - Asz_Sens.y;
       Serial.print(first_samp);
       Serial.print(",");
       Serial.println(second_samp);

       
       //delay(50);
    }
}

so my plan is to save first_sample and second_sample in an array that can take as much input as it gets. (not more than 100).
Array[first_samp]
[second_samp]
[first_samp]
[second_samp]
[first_samp]
.
.
.

and when the device is done sending its readings, the array should be closed and the values should be printed out through serial to the computer.

serial.print(Array[0]);
serial.print(Array[1]);
serial.print(Array[2]);
.
.
.
serial.print(Array[last value sent from sensor]);

P.S: this is not a homework, and not a please do my work for me. i tried sending the values without saving them in an array, but the other side (TEENSY 3.2) got the values very slowly, and that affected the whole program.
thanks for any help

So what is the question? Arrays are treated the same as any other variable, except the use of an index.

Instead of this:

serial.print(Array[0]);
serial.print(Array[1]);
serial.print(Array[2]);

write:

   for(i=0; i<3; i++) serial.print(Array[i]);

Your code does not compile

Pete

thanks for the quick reply guys.
this code would never compile without the include and the mistake i found now in the void loop

tgr_left = false;
should be
tgr_lt = false;

the problem is how to create this array that can take as many input as it gets.
do i have to make a while loop in the void loop?

the problem is how to create this array

type arrayName[arraySize];

that can take as many input as it gets.

Forget that crap right now. Figure out how much data it is reasonable to store, based on why you are storing the data in an array. Size the array accordingly.

hey.. thats not the answer i was looking for. what do you mean, i have to specify its capacity?

Either tell the compiler how much space you need, at compile time, or new/malloc at runtime.
Microcontrollers aren't psychic.

ok, so i tried something out, i got the following

call of overloaded write//or print i tried both is ambiguous

why am i getting this error? any suggestions thanks?

Arduino: 1.6.10 (Windows 7), TD: 1.30, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz optimize speed (overclock), Canadian French"

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino: In function 'void setup()':

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:77:13: warning: unused variable 'started' [-Wunused-variable]

     uint8_t started = false;

             ^

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:78:14: warning: unused variable 'counter' [-Wunused-variable]

     uint16_t counter = 0;

              ^

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino: In function 'void loop()':

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:116:122: error: call of overloaded 'print(unsigned int [100][10])' is ambiguous

                                                                                                     Serial1.print(drawing);     

                                                                                                                          ^

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:116:122: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Stream.h:24:0,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/HardwareSerial.h:238,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/WProgram.h:16,

                 from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Arduino.h:1,

                 from C:\Users\Labor2\AppData\Local\Temp\buildbbc044dcbd5b4acdda5e77d8657d9725.tmp\sketch\TEST1-DONE.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:57:9: note: size_t Print::print(const String&) <near match>

  size_t print(const String &s);

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:57:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'const String&'

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:58:9: note: size_t Print::print(char) <near match>

  size_t print(char c)    { return write((uint8_t)c); }

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:58:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'char'

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:62:9: note: size_t Print::print(uint8_t) <near match>

  size_t print(uint8_t b)    { return printNumber(b, 10, 0); }

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:62:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'uint8_t {aka unsigned char}'

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:63:9: note: size_t Print::print(int) <near match>

  size_t print(int n)    { return print((long)n); }

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:63:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'int'

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:64:9: note: size_t Print::print(unsigned int) <near match>

  size_t print(unsigned int n)   { return printNumber(n, 10, 0); }

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:64:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'unsigned int'

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:65:9: note: size_t Print::print(long int) <near match>

  size_t print(long n);

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:65:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'long int'

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:66:9: note: size_t Print::print(long unsigned int) <near match>

  size_t print(unsigned long n)   { return printNumber(n, 10, 0); }

         ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Print.h:66:9: note:   no known conversion for argument 1 from 'unsigned int [100][10]' to 'long unsigned int'

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino: In function 'void set_PORTA_for_coord(coord_t)':

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:160:8: warning: statement has no effect [-Wunused-value]

    valx;

        ^

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:164:8: warning: statement has no effect [-Wunused-value]

    valy;

        ^

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino: In function 'void change_size(uint8_t (*)[10], uint8_t (*)[2], uint8_t, uint8_t, uint8_t, uint8_t)':

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:422:81: error: 'PIX_DENSITY_THRESHOLD' was not declared in this scope

       if (((double)sum_pix)/((x_box_max-x_box_min+1)*(y_box_max-y_box_min+1)) > PIX_DENSITY_THRESHOLD) {

                                                                                 ^

C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:423:40: error: 'set_norm_matrix' was not declared in this scope

         set_norm_matrix(output, x, y, 1);

                                        ^

In file included from C:\Users\Labor2\Desktop\TEST1-DONE\TEST1-DONE.ino:1:0:

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h: At global scope:

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:121:16: warning: 'LightState' defined but not used [-Wunused-variable]

 static uint8_t LightState[N_LEDS] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 

                ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:124:16: warning: 'LightNr' defined but not used [-Wunused-variable]

 static uint8_t LightNr[N_LEDS] = {9, 10, 12, 13, 16, 17, 18, 20, 21, 

                ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:137:12: warning: 'tableSnap_Left' defined but not used [-Wunused-variable]

 static int tableSnap_Left[N_MOTOR_MODE][3]     = {{1, 2, 1},  // Mode 1

            ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:141:12: warning: 'tableBackSnap_Left' defined but not used [-Wunused-variable]

 static int tableBackSnap_Left[N_MOTOR_MODE][3] = {{1, 2, 1},

            ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:145:12: warning: 'tablePresense_Left' defined but not used [-Wunused-variable]

 static int tablePresense_Left[N_MOTOR_MODE][3] = {{1, 1, 1},

            ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:150:12: warning: 'tableSnap_Right' defined but not used [-Wunused-variable]

 static int tableSnap_Right[N_MOTOR_MODE][3]     = {{1, 2, 1},  // Mode 1

            ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:154:12: warning: 'tableBackSnap_Right' defined but not used [-Wunused-variable]

 static int tableBackSnap_Right[N_MOTOR_MODE][3] = {{1, 2, 2},

            ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:158:12: warning: 'tablePresense_Right' defined but not used [-Wunused-variable]

 static int tablePresense_Right[N_MOTOR_MODE][3] = {{1, 1, 1},

            ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:169:17: warning: 'newGestureCode' defined but not used [-Wunused-variable]

 static uint16_t newGestureCode[13] =   {NewGes_Left,

                 ^

C:\Users\Labor2\ownCloud\GET_Projektlaufwerk\02_Projekte\GM_Arduino\libraries\NinaSecondImpact/NinaSecondImpact.h:183:17: warning: 'newMFLCode' defined but not used [-Wunused-variable]

 static uint16_t newMFLCode[10] =    { NewMFL_No_TouchOnIF,

                 ^

Fehler beim Kompilieren für das Board Teensy 3.2 / 3.1.

Dieser Bericht wäre detaillierter, wenn die Option
"Ausführliche Ausgabe während der Kompilierung"
in Datei -> Voreinstellungen aktiviert wäre.

in this function

      Serial1.print(drawing);
void set_matrix(unsigned int drawing[X_RES][Y_RES/8], uint8_t, uint8_t, uint8_t);

It's a bit tricky if one does not know how many samples are being sent. But you can consider reading with a timeout so e.g. not receiving data for 1 second indicates that the device was done sending.

The fact that the Teensy receives slowly should not affect it; that it is affected might very well be caused by an incorrect approach in the Teensy.

You can NOT print an entire array in one call, unless the array is a char array with a NULL terminator. That does not apply to you, so put a period in place of the comma in the first sentence, and commit it to memory.

tipikosh:
tthe problem is how to create this array that can take as many input as it gets.

Ah. That.

You can allocate memory at run time with the "malloc" function, or with C++ "new" (which I have never used). However, using this stuff correctly is - shall we say - pretty advanced stuff.

Your best bet might be to use the Vector object from the C++ standard library. http://www.cplusplus.com/reference/vector/vector/

It might possibly maybe perhaps be the case that one of the linked list classes might work better: http://www.cplusplus.com/reference/forward_list/forward_list/ . The main trick to using a list, however, is that you do not allocate one reading per list item: you make your items a small fixed-size array of readings (say, 100).

Yeah - go with C++ vector. Just be aware that an arduino has very limited RAM to muck around with, and you must code for the possibility that when you attempt to shove more data into the vector, it won't fit.

ok ill give it a try, thanks for all your replies

i was reading about list, and i guess it could work out for me, the vector class is somehow complicated..
so i tried the following:

#include
#include

var list = new List<int>();
volatile bool device_one = false;


void setup() {
  Serial.begin(9600);

  Wire.begin();
  Device_One.begin();
  device_one.state=0;
  attachInterrupt(digitalPinToInterrupt(INTERRUPT_ONE), checkDeviceOne, RISING);
}

void checkDeviceOne()
{
  if(device_one.GetReading(&Device_One))
    device_one = true;
}

void loop() {

     if(Device_one)
    {
       device_one= false;      

       
list.Add(device_one.value1);
list.Add(device_one.value2);
var array = list.ToArray();


       Serial.print(array[i]);

    }

i tried that but it doesnt compile, because the array doesnt stop writing.

device_one is a bool fun that sends the values reading whenever it is subjected to external power. i can use it to terminate filling up the list if it goes false.
any ideas? thanks

device_one is a bool fun

It is NOT. It is a variable, not a fun(ction).

You have something called Device_one that is not the same as device_one, but who knows what the heck it is.

You seem to expect a boolean variable to have value1 and value2 fields. It does NOT.

Why do you bother posting code that hasn't a hope in hell of compiling, and then claim it does something? Code that doesn't compile doesn't do anything.

mr paul thanks for replying.
first, i explained what i want more than 3 times.. i need an array to be filled up with those sensor values until no value arrives anymore.
second, i tried correcting the mistake:
if(Device_one)
{
device_one= false;
to
if(device_one)
{
device_one= false;
but i couldnt, i got the error message that i cant post more than like 5 times or so in 5 min, so i couldnt correct it.
third, i dont want to waste your time and neither my time.
i need help and thats why there is something called a forum..i have a problem with arduino so i wrote in the arduino official forum to get help from the experts.
if u can help me, i am thankful for you
it is actually very serious and very important to get this work done.
and its not a (School Project!)..
if someone knows how to create an array to fill it up with 2 values until no data arrives, then please post your help.
thanks

Your requirements are still confusing and poorly stated, so I'm assuming English is not your first language!

"if someone knows how to create an array to fill it up with 2 values until no data arrives, then please post your help."

Is it, or is it not, 2 values, or is it, as you also say, till it's full.

Question - how does one FILL an array of unknown size?

How will you know when NO data arrives?

tipikosh:
mr paul thanks for replying.

No probs.

second, i tried correcting the mistake:
if(Device_one)
{
device_one= false;
to
if(device_one)
{
device_one= false;
but i couldnt, i got the error message that i cant post more than like 5 times or so in 5 min, so i couldnt correct it.

Could you post the code you have right now? The original post doesn't mention device_one, so I assume you have done some more work on the sketch in the meantime.

it is actually very serious and very important to get this work done.
and its not a (School Project!)..

Then you might want to go to the "Gigs and collaborations" board and offer money. Here, people will try to help you learn programming rather than simply giving you the answer.

if someone knows how to create an array to fill it up with 2 values until no data arrives, then please post your help.
thanks

Dude - what you are asking as you have asked it is impossible. Why? Because an Arduino has a limited amount of memory. The only way to manage an arbitrarily large number of samples is to take some sort of running average of them (several way of doing that), or to stuff them out to external storage somehow.

On the other hand, filling up an array until no data arrives up to a limit of (however many) samples is possible. Just make an array big enough to hold the maximum number of samples, and hold a variable "number of samples currently taken".

Because your code is a bit of a mess, only the loop part

...
...

void setup()
{
  ...
  ...
}

// array to store maximum of 100x2 samples
uint16_t data[200];

void loop()
{
  // index iinto data array
  static int index = 0;
  // start time for timeout
  static unsigned long startTime;
  // current time
  unsigned long currentTime;

  currentTime = 0;

  // if something received
  if (tgr_lt)
  {
    // if first sample
    if (startTime == 0)
    {
      // set the start time for the timeout
      startTime = currentTime;
    }
    // reset the flag
    tgr_left = false;

    // add data to array at correct location
    data[index++] = 1024 - Asz_Sens.x;
    data[index++] = 1024 - Asz_Sens.y;
  }

  // if 1 second timeout between data packets or if array full
  if (currentTime - startTime >= 1000 || index >= sizeof(data) / sizeof(data[0]))
  {
    // display
    for (int cnt = 0; cnt < index; cnt += 2)
    {
      Serial.print(data[cnt]);
      Serial.print(",");
      Serial.println(data[cnt + 1]);
    }

    // reset startTime of timeout
    startTime = 0;
    // reset index for next set of readings
    index = 0;
  }
}