A too long array?

Hello everyone, I'm building a mechanism using a stepepr motor and a micro servo. My goal is to make a sequence of moviments - about 3000 - with the stepper and my tests have been very successful so far regarding to the programing, I wrote a code in the loop similar to this:

int array[]={p1,p2,p3,p4};

for (int number_of_positions=0; n_of_pos<3; n_of_pos++){
(after this, it calculates how many steps it needs to do to achive the position in the array - array[n_of_pos]- and then i use exit(0) to skip the loop after the array is fully readed.)

And when i use a small amount of positions ( like 5, 6) it works just fine, but my problem is that i need to use an array much larger than this, so i tried to separate it in hundreds of positions instead of thousands and change the '' int array[] '' to a "byte array[]", but in non of theses changes worked, in all those cases, the IDE (?) sends the code to the board but the motors don't move at all. I am very used to work with python and this is my first time using arduino, so I don't know what I'm doing wrong... Can someone help me? hahah

Thank you!

Welcome to the forum.

Which Arduino board do you use ?
There are Arduino boards with enough memory, there are also boards that can put large blocks of data in Flash memory.

You could also write the data in a file on a (micro)SD memory card and read it with the Arduino.

Can you show your sketch ?

Which Arduino are you using?

When you declare your array, it uses up RAM which is in short supply on an UNO - 2Kb or 2048 bytes).

If the positions are fixed, then you can declare the array to be in program space - i.e. stored in the flash memory instead.

EDIT: @Koepel - you beat me to it :laughing:

Which Arduino board are you using ?

On most Arduinos an int takes 2 bytes so it makes sense to use byte when you can to save space

Please post a full sketch that compiles but does not work. Please use code tags when posting your code. See How to get the best out of this forum

The maximum size of an array on an arduino is 32767 bytes, the actual memory is not the limiting factor, it is a limitation imposed by the compiler.

Thank you so much everyone!! I'm using an Arduino UNO, I already tried to use the array in setup part but it seems it didn't worked, as if it was erased before entering in the 'void loop()' I'm gonna put my code here, it's in my laptop.

Have a look at PROGMEM in the Arduino reference.

You might want to read up on variable scope.

Another good reference Putting constant data into program memory

I misunderstood your question with my previous reply, you are nowhere close to hitting the maximum array size.


#include <Servo.h>
#define pinServo 6

Servo s;
int pos;

const int stepp = 2;
const int dir = 3;
void setup() {
  // put your setup code here, to run once:
  pinMode(stepp, OUTPUT);
  pinMode(dir, OUTPUT);
  s.attach(pinServo);
  Serial.begin(9600);
  s.write(150);




}
//HIGH É ANTIHIRÁRIO
void loop() {

  int posi;
  posi = 0;

  int k;
  k = 0;

  //digitalWrite(dir, HIGH);
  int (y);

  //int lposi[]={41, 45, 105, 106, 148, 152, 103, 95, 41, 26, 176, 141, 108, 91, 38, 27, 175, 142, 99, 97, 144, 171, 16, 34, 181, 139, 109, 93, 42, 44, 106, 107, 147, 167, 12, 13, 53, 110, 112, 171, 163, 123, 76, 77, 122, 164, 166, 13, 37, 188, 137, 134, 96, 40, 8, 169, 170, 113, 90, 72, 142, 143, 98, 46, 48, 3, 158, 153, 113, 75, 72, 117, 114, 73, 28, 32, 182, 138, 130, 181, 35, 36, 190, 148, 104, 94, 41, 21, 172, 110, 78, 32, 33, 195, 144, 107, 92, 43, 11, 168, 144, 118, 72, 71, 120, 169, 17, 25, 58, 132, 99, 39, 15, 172, 112, 76, 17, 33, 181, 133, 90, 91, 132, 156, 199, 46, 5, 163, 162, 114, 89, 73, 143, 174, 15, 14, 56, 113, 165, 15, 35, 192, 145, 147, 191, 43, 7, 161, 159, 105, 103, 46, 54, 108, 173, 20, 18, 169, 128, 126, 170, 23, 22, 95, 97, 39, 10, 173, 143, 110, 50, 49, 9, 160, 147, 115, 73, 27, 28, 183, 137, 141, 178, 30, 29, 182, 120, 70, 79, 117, 176, 26, 52, 111, 173, 152, 124, 75, 79, 121, 166, 2, 50, 51, 102, 103, 149, 151, 106, 42, 13, 163, 145, 96, 102, 48, 49, 111, 175, 29, 18, 88, 80, 21, 24, 177, 116, 87, 74, 142, 128, 86, 81, 127, 163, 1, 52, 51, 5, 4, 47, 105, 170, 146, 109, 85, 24, 39, 188, 134, 142, 177, 22, 55, 199, 151, 114, 82, 36, 37, 99, 105, 168, 156, 106, 50, 6, 162, 158, 106, 40, 187, 140, 129, 183, 37, 38, 97, 98, 41, 7, 157, 154, 112, 91, 66, 138, 179, 34, 11, 172, 126, 82, 84, 31, 30, 198, 146, 116, 80, 7, 6, 56, 198, 158, 130, 91, 44, 6, 2, 52, 48, 10, 7, 54, 52, 109, 176, 40, 14, 166, 113, 84, 14, 36, 100, 165, 145, 117, 69, 25, 27, 184, 124, 122, 77, 78, 120, 142, 180, 36, 28, 94, 95, 153, 152, 99, 49, 17, 171, 120, 92, 20, 48, 197, 196, 143, 126, 185, 26, 15, 84, 112, 167, 153, 114, 74, 1, 0, 53, 47, 109, 169, 168, 112, 52, 16, 165, 122, 71, 15, 30, 181, 121, 72, 30, 173, 105, 93, 153, 147, 76, 102, 160, 197, 57, 41, 102, 101, 46, 45, 5, 154, 123, 71, 11, 14, 175, 110, 46, 191, 136, 135, 184, 36, 93, 94, 22, 45, 104, 169, 153, 102, 66, 6, 158, 123, 77, 3, 32, 102, 165, 163, 113, 53, 199, 1, 43, 107, 108, 171, 30, 4, 155, 113, 74, 3, 162, 128, 52, 8, 173, 107, 106, 164, 195, 49, 96, 106, 147, 162, 102, 75, 147, 129, 188, 44, 46, 89, 109, 173, 174, 112, 80, 20, 25, 100, 101, 167, 20, 46, 93, 136, 183, 40, 41, 108, 107, 39, 40, 95, 107, 146, 175, 27, 92, 93, 37, 9, 79};
  int lposi[] = { 41, 45, 105, 106, 190, 148, 103};

  int h1;
  int h2;
  int dist;
  int veri;

  for (int k = 0; k < 6; k++) {



    if (lposi[k] > posi) { //importante---------------------
      h1 = abs(lposi[k] - posi);
      h2 = abs(200 - lposi[k] + posi);


      if (h2 <= h1) {
        digitalWrite(dir, HIGH);
        dist = h2;
        veri = -1;
      }


      if (h1 < h2) {
        dist = h1;
        digitalWrite(dir, LOW);
        veri = 1;
      }

    }

    if (lposi[k] < posi) {                                     //importante -----------------------------
      h1 = abs(posi - lposi[k]);
      h2 = abs(200 - posi + lposi[k]);


      if (h2 <= h1) {
        digitalWrite(dir, LOW);
        dist = h2;
        veri = 1;
      }


      if (h1 < h2) {
        dist = h1;
        digitalWrite(dir, HIGH);
        veri = -1;
      }


    }

    y = 0;


    for (int x = 0; x < dist * 2; x++) {
      digitalWrite(stepp, HIGH);
      delay(23 - y);
      digitalWrite(stepp, LOW);
      delay(23 - y);
      if (y < 19) {
        y = y + 1;
      }
      if (x > ((dist * 2) - 20)) {
        y = y - 2;
      }
    }
    delay(100);
    for (pos = 0; pos < 90; pos++) {
      s.write(150 - pos);
      delay(5);
    }
    delay(100);
    for (int w = 0; w < 4; w++) {
      digitalWrite(stepp, HIGH);
      delay(25);
      digitalWrite(stepp, LOW);
      delay(25);
    }

    if (veri == -1) {
      digitalWrite(dir, LOW);
    }


    if (veri == 1) {
      digitalWrite(dir, HIGH);
    }
    delay(300);

    for (pos = 60; pos < 179; pos++) {
      s.write(pos);
      delay(5);
    }
    delay(300);

    for (int w = 0; w < 4; w++) {
      digitalWrite(stepp, HIGH);
      delay(25);
      digitalWrite(stepp, LOW);
      delay(25);
    }



    posi = lposi[k];


  }

  exit(0);
}

so, that's my code, and thank you for all these links you are sending, I am gonna read it all :slight_smile:

[quote="david_2018, post:5, topic:888653"]
The maximum size of an array on an arduino AVR is 32767 bytes,
[/quote] Sorted.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.