Reading an array with PROGMEM after I mapped it

Hi,

I'm quite new with Arduino and have some issues with a project.

I need to read an array from PROGMEM, that needs to be mapped to certain range in order to control the steps of a stepper motor. Then, I need to compare the increase of the elements in the array to restrict the action of apply the steps to those elements that are equal to the last applied unless they changed.

I made spin the stepper without his library. In the function that makes the steps I have two attributes, one for the number of steps and the other for the RPM speed.

I want to replace the attribute of number of steps by the line that reads the PROGMEM array to apply the steps of that array unless the element are the same that the last one applied.

Something like this:

void loop() {
mover(50, 10); >> Instead of 50, I have to read all the elements of an array with PROGMEM
delay(200);
}


void mover (int numSteps, int rpm) {
if (numSteps >= 0) {
  forward (numSteps, rpm);
} else {
  backward (abs(numSteps), rpm);
}
}

It's okay if I make this?: Map the PROGMEM array and then read with pgm_read the variable now defined?

pwm_map = map(pwm1, 99, 3690, 10, 500);     //pwm1 is the original PROGMEM Array

pgm_read_word_near(pwm_map + n);             //reading the mapped PROGMEM array now defined as pwm_map

Then it's okay if I write something like this?: I'm confusing the increase with the for cycle and the pgm_read...

for (int = 0; i < lenghtArray; i++) {           //Will work the condition if I don't execute the pgm_read here also?

mover(pgm_read_word_near(pwm_map), 10); //Do I need to put the n++ after this sentence?

if (pgm_read_word_near(pwm_map) == n { // Here I need to compare the present element to the last one

I'm kind of confused with the read and increase thing.

Sorry for the little mess!

Thank you so much!

It's okay if I make this?

It's OK by me. It makes no sense, but, hey it's not my program.

Mapping the value that you have read makes sense. Mapping some undefined value to get an address to read from makes not a lick of sense.

I'm kind of confused with the read and increase thing.

When you have a clue what you want to do, come on back, and we'll help you figure out how to do it.

PaulS:
It's OK by me. It makes no sense, but, hey it's not my program.

Mapping the value that you have read makes sense. Mapping some undefined value to get an address to read from makes not a lick of sense.
When you have a clue what you want to do, come on back, and we'll help you figure out how to do it.

I decided to work with a simple array to make the things clearer.

I want to compare the first element of an array with the next one in the same array. If the actual element and his next one are equals, then there's nothing to do but wait 500ms and compare again with the second element and his next. If it's different, then it'll be a movement with the number of steps of that element.

I have this, the whole code for better understanding:
Ignore the functions, I want to put emphasis on the loop section and the array compare.

const int pwm1[] = {200, 100, 400, 800}; //ARRAY TO COMPARE ITSELF
int ndatos = 4;

//STEPPER VARIABLES
const int pinesUnipolar[] = {8, 9, 10, 11};
//int pinesUnipolar[] = {11, 10, 9, 8};   //REVERSE PINS TO MOVE BACKWARD, THAT'S ANOTHER STORY

const int numPines = 4;
int n = 0;
//END STEPPER VARIABLES

void setup() {
  for (int i = 0; i < numPines; i++) {
    pinMode(pinesUnipolar[i], OUTPUT);
    digitalWrite(pinesUnipolar[i], LOW);
  }
}


void loop() {
  int a = pwm1[n];
  if (a != pwm1[n++]) {
    mover(0, 0);
    delay(500);
    a = pwm1[n++];
    if (n > ndatos) {
      n = ndatos - ndatos;
    }
  } else {
    mover(map(pwm1[n], 100, 800, 10, 100), 10);    //this maps
    delay(500);
    n++;
    if (n > ndatos) {
      n = ndatos - ndatos;
    }
  }
}

//FUNCTIONS
void mover(int numSteps, int rpm) {
  if (numSteps >= 0) {
    forward(numSteps, rpm);
  } else {
    backward(abs(numSteps), rpm);
  }
}

void forward(int numSteps, int rpm) {
  for (int i = 0; i < numSteps; i++) {
    digitalWrite(pinesUnipolar[n], HIGH);
    delay(rpm);
    digitalWrite(pinesUnipolar[n], LOW);
    n++;
    if (n == numPines) {
      n = 0;
    }
  }
}

void backward(int numSteps, int rpm) {
  for (int i = 0; i < numSteps; i++) {
    digitalWrite(pinesUnipolar[n], HIGH);
    delay(rpm);
    digitalWrite(pinesUnipolar[n], LOW);
    n--;
    if (n < 0) {
      n = numPines - 1;
    }
  }
}
//END FUNCTIONS

Thank you!!

Print out the values from the array, and n, at appropriate places. It looks to me like you are incrementing n twice as often as you should.

I'll include the Serial.println for the array and the n variable to see what's going on. In the midtime I want to ask where's repeated the incrementing. In the loop I discarded working with a for cycle because the function is a loop itself. For this I needed add the n++ to read the elements of the array one by one.

void loop() {
  int a = pwm1[n];                  //Here "a" it's defined as pwm1's first element
  if (a != pwm1[n++]) {          //Here I compare if the first element is different from the second ("n++" is the index 1, the second of the array right?)
    mover(0, 0);                
    delay(500);
    a = pwm1[n++];               //If the condition it's true, "a" will be the index 1 in the second loop
    if (n > ndatos) {                //This line and the next one are for limiting the lecture of the array
      n = ndatos - ndatos;
    }
  } else {
    mover(map(pwm1[n], 100, 800, 10, 100), 10);    //this maps
    delay(500);
    n++;
    if (n > ndatos) {
      n = ndatos - ndatos;
    }
  }
}

In the midtime I want to ask where's repeated the incrementing.

  if (a != pwm1[n++]) {          //Here I compare if the first element is different from the second ("n++" is the index 1, the second of the array right?)
    mover(0, 0);                
    delay(500);
    a = pwm1[n++];

n is incremented if the values are different and again when a is assigned a new value. I think it should be n++ in the first statement and n in the last statement in that snippet.