invalid conversion from 'int*' to 'long int'

I’m stumped here. I got some help with this code to do a little bit of math to control motor speed using a rotary encoder. I have a target encoder position that I set, and the arduino does the math for the motor velocity and sends it there. When I just have one number it works fine. When I try to implement a set of numbers using an array for Arduino to cycle through, I get the error:

invalid conversion from ‘int*’ to long int’

MFA_Code__after_baker_fix_.ino: In function ‘void loop()’:
MFA_Code__after_baker_fix_:164: error: invalid conversion from ‘int*’ to ‘long int’
MFA_Code__after_baker_fix_:164: error: initializing argument 3 of ‘long int map(long int, long int, long int, long int, long int)’
MFA_Code__after_baker_fix_:178: error: invalid operands of types ‘int [4]’ and ‘int’ to binary ‘operator/’

Any ideas? This is a bit beyond my skill set.

#include <PWM.h>


const int encoderPin  = 2;       // set to the Arduino's internal interrupter.  Cannont be changed
const int pwmPin    = 9;        // pwm signal sent to the H-Bridge that controls the motor speed
const int motorDir = 10;          // pin for setting direction of motor.  analogWrite 0/255
int motorSpeed = 0;

int32_t frequency = 30000; //frequency (in Hz)  (this quiets the motor noise)


int maxVelocity = 255;              // represents the top speed of the motor
int minVelocity = 0;                // represents the starting speed of the motor




const int numPatterns = 4;
int currentPatternIndex = 0;

int targetEncoderPos[numPatterns] = {        // represents the distance the motor has to travel.  This will be the end point
  30, 60, 80, 40};                              // stay under 90.  The limit switch is at 97

int peakForwardWait[numPatterns] = {
  1000, 2000, 3000, 4000 };

int peakBackwardWait[numPatterns] = {
  1000, 2000, 3000, 4000 };

int waitForForward[numPatterns] = {
  1000, 2000, 3000, 4000 };

int waitForBackward[numPatterns] = {
  3000, 4000, 5000, 4000 };


const int NUM_STATES = 8;

const int WAITING_FOR_FORWARD  = 0;
const int FORWARD_0            = 1;
const int FORWARD_WAIT         = 2;
const int FORWARD_1            = 3;
const int WAITING_FOR_BACKWARD = 4;
const int BACKWARD_0           = 5;
const int BACKWARD_WAIT        = 6;
const int BACKWARD_1           = 7;

int directionStatus = WAITING_FOR_FORWARD;

long nextEvent = 0;

long encoderKickStart = 300;        // since we're relying on the encoder to move to determine timing actions, we need to tell the arduino that 
// it has moved before we start or it will never move.  

volatile int encoderPos = encoderKickStart;


void setup() {

  Serial.begin(9600);


  InitTimersSafe(); 

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(pwmPin, frequency);

  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);    
  }



  pinMode(encoderPin, INPUT);
  //turn on pullup resistor
  digitalWrite(encoderPin, HIGH);
  //encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoder, RISING);


}


void loop() {

  long now = millis();



          if(directionStatus == WAITING_FOR_FORWARD) {
            if(now > nextEvent) {                  //implement array values in here.  setting the value of all peaks/waves for sequence.  Change the array index here
              currentPatternIndex = (currentPatternIndex + 1) % numPatterns;
              directionStatus = FORWARD_0;
              Serial.println("FORWARD_0");
            } 
            else {
              Serial.println("WAITING_FOR_FORWARD");
            }
          } 
          else if(directionStatus == WAITING_FOR_BACKWARD) {
            if(now > nextEvent) {
              directionStatus = BACKWARD_0;
              Serial.println("BACKWARD_0");
            } 
            else {
              Serial.println("WAITING_FOR_BACKWARD");
            }
          } 
          else if(directionStatus == FORWARD_WAIT) {
            if(now > nextEvent) {
              directionStatus = FORWARD_1;
              encoderPos = targetEncoderPos[currentPatternIndex]/2; 
              Serial.println("FORWARD_1");
            } 
            else {
              Serial.println("FORWARD_WAIT");
            }
          } 
          else if(directionStatus == BACKWARD_WAIT) {
            if(now > nextEvent) {
              directionStatus = BACKWARD_1;
              encoderPos = targetEncoderPos[currentPatternIndex]/2; 
              Serial.println("BACKWARD_1");
            } 
            else {
              Serial.println("BACKWARD_WAIT");
            }
          } 
          else {
            // where are we on the slope?    
            if(encoderPos >= targetEncoderPos[currentPatternIndex] - encoderKickStart) {
              if(directionStatus == FORWARD_1) {
                nextEvent = now + waitForBackward[currentPatternIndex];  
                directionStatus = WAITING_FOR_BACKWARD;
                Serial.println("WAITING_FOR_BACKWARD");

                encoderPos = encoderKickStart;
              } 
              else if(directionStatus == BACKWARD_1) {
                nextEvent = now + waitForForward[currentPatternIndex];
                directionStatus = WAITING_FOR_FORWARD;
                Serial.println("WAITING_FOR_FORWARD");
                encoderPos = encoderKickStart;
              } 
              else {
                Serial.println("XXXX problem");
              }
            } 
            else if(encoderPos > targetEncoderPos[currentPatternIndex] / 2 - encoderKickStart) {
              if(directionStatus == FORWARD_0) {
                nextEvent = now + peakForwardWait[currentPatternIndex];
                directionStatus = FORWARD_WAIT;
                Serial.println("FORWARD_WAIT");
              } 
              else if(directionStatus == BACKWARD_0) {
                nextEvent = now + peakBackwardWait[currentPatternIndex];
                directionStatus = BACKWARD_WAIT;
                Serial.println("BACKWARD_WAIT");
              } 
              else {
                //  the down slope
                long velocity = map(encoderPos,targetEncoderPos[currentPatternIndex]/2,targetEncoderPos,maxVelocity,minVelocity);
                if(directionStatus == BACKWARD_1) {
                  backward(velocity);
                } 
                else if(directionStatus == FORWARD_1) {
                  forward(velocity);
                } 
                else {
                  Serial.println("!!!!!!");
                }
              }
            } 
            else {
              // the up slope
              long velocity = map(encoderPos,0,targetEncoderPos / 2,minVelocity,maxVelocity);
              if(directionStatus == BACKWARD_0) {
                backward(velocity);
              } 
              else if(directionStatus == FORWARD_0) {
                forward(velocity);
              } 
              else {
                Serial.println("?????????");
              }
            }
          } 

          //  
          //  Serial.print(encoderPos);
          //  Serial.print(" / ");
          //  Serial.print(targetEncoderPos);
          //  Serial.print(" = ");
          //  Serial.print(normPosition);
          //  Serial.print(" : ");
          //  Serial.print(mappedNormPosition);
          //  Serial.print(" : ");
          //  Serial.println(velocity);

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




void doEncoder() {
  encoderPos++;
}

void forward(int val) {
  analogWrite(motorDir, 255);
 //   pwmWrite(pwmPin, val);
  analogWrite(pwmPin, val);
}

void backward(int val) {
  analogWrite(motorDir, 0);
  // pwmWrite(pwmPin, val);
  analogWrite(pwmPin, val);


}

[code]

[/code]

Here:

 long velocity = map(encoderPos,targetEncoderPos[currentPatternIndex]/2,targetEncoderPos,maxVelocity,minVelocity);

and here:

 long velocity = map(encoderPos,0,targetEncoderPos / 2,minVelocity,maxVelocity);

(the lines are those shown in the error log) you use the array targetEncoderPos instead of a particular element of it (e.g., targetEncoderPos[0]).

Please don’t forget to enclose your code within [ code ] tags.

Not quite sure I understand you correctly. This line:

long velocity = map(encoderPos,targetEncoderPos[currentPatternIndex]/2,targetEncoderPos,maxVelocity,minVelocity);

is generating the errors as well; I thought I was calling for each element in the array? I'm incrementing the array in the beginning of the sketch:

currentPatternIndex = (currentPatternIndex + 1) % numPatterns;

The second occurrence* of targetEncoderPos does not specify the [element_in_the_array]. Consider that from the compiler's point of view an array of int is just an int pointer, it is not something like a set or a list in other languages that may be passed whole letting the called function iterate over them.

The map function "scales" a value from a source range to a target range. So a value of 3 on a scale from 0 to 5 corresponds to a value of 6 on a scale from 0 to 10. All the values must be (long) integers.

[edit] * here: long velocity = map(encoderPos,targetEncoderPos[currentPatternIndex]/2,targetEncoderPos,maxVelocity,minVelocity);

Ahhh! Thanks for the help! It was asking for the second array to be called for. Thanks so much, I’ve been staring at that for too long!