mapping command not functioning how i expect?

Was wondering if anyone with a little more know how could help me out with my code?
I am attempting to control a servo via a Bluetooth app and have got as far and making slider values within the app appear in the serial monitor. However I am then trying to map the values from 0, 255 to 0, 179 and it is not working. Can anyone see how i can fix this?

first_servo_try.ino (1.83 KB)

Euan - here is your code Auto Formatted in the IDE and posted here in code tags as recommended in
How to use this forum

Please follow the advice when next posting code

boolean debug = true;

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial BTserial(2, 3);
Servo hand_grip;

const byte numChars = 20;
char receivedChars[numChars];
boolean newData = false;

int val;

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  BTserial.begin(9600);
}

void loop()
{
  if (BTserial.available() > 0)
  {
    recvWithStartEndMarkers();
  }
  if (newData)
  {
    parseData();
  }
}

void parseData()
{
  newData = false;
  if (debug)
  {
    Serial.println("");
    Serial.println( "recived value =" );
    Serial.println( receivedChars );
    val = map(receivedChars, 0, 255, 0, 179);
    Serial.println("mapped value =");
    Serial.println(val);
    Serial.println("");
    hand_grip.write(val);
  }
}

void recvWithStartEndMarkers()
{
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;
  if (BTserial.available() > 0)
  {
    rc = BTserial.read();
    if (recvInProgress == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars)
        {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker)
    {
      recvInProgress = true;
    }
  }
}

receivedChars is declared as an array of chars

    val = map(receivedChars, 0, 255, 0, 179);

What did you expect the map() function to do ?

The map() command works with integers, receivedChars is not an integer.

Steve

It is not clear what you want to do. I'm guessing that what you are receiving from the BT is a serial string representing a slider value. If that is the case then you need to convert that string to an integer then use map to convert it to a different range...but that is only a guess.

You need to provide more details on your requirements/expectations and details about the information and format being sent from the BT.

thanks for the help I will try and be more clear next time

EuanGreenlees1:
thanks for the help I will try and be more clear next time

Will you be updating this thread with either your solution or a fuller description of what you are trying to do ?

yeah well what i think i need to do it make receivedChars in to an integer and i plan to do that using the Serial.parseInt() command but haven't got it working yet as i want to parse other data that isn't servo values in the same code so I cannot just replace the void recvWithStartEndMarkers() sub routine. i can't find many good examples of the Serial.parseInt() command in use so if you understand i'd appreciate help. thanks again for getting me this far :slight_smile:

i plan to do that using the Serial.parseInt() command

Don't do that.

receivedChars is C style string (lowercase s) which is an array of chars terminated by a zero. You can convert such a string to an integer using the atoi() function

ok ill give that a go cheers