Simple mapping question [ map() ]

Hello !,

I think I'm missing something here despite mapping and the value being in the mapping range it still outputs it as an unmapped value. :confused:
I looked at the syntax and usage of map in other codes and don't know why it doesn't work here.
I think I'm just missing something obvious.
Thanks in advance! :slight_smile:

https://ibb.co/YTT4Hd9

Here the Code: (the Code is still extremely inefficient)



#include <Wire.h>
#include <Servo.h>
int lepos = 0;
int leposdue = 0;
#define PWM_PIN  8  

unsigned int pwm_value;
 
Servo azimuth9;
Servo elevation12;

const byte numChars = 16;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

float azimuth;
float elevation;




const int MPU_addr=0x68; int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int minVal=265; int maxVal=402;

double x; double y; double z;
 //-------------------------------SETUP-----------------------------------
void setup()
{

 
 pinMode(PWM_PIN, INPUT);
  Serial.begin(9600);




  
  int lepos = 0;

  

Wire.begin(); Wire.beginTransmission(MPU_addr); Wire.write(0x6B);
Wire.write(0); Wire.endTransmission(true); Serial.begin(9600); 




  
azimuth9.attach(9);
elevation12.attach(12);
azimuth9.write(0);
elevation12.write(0);
delay(2000);
  




  
   Serial.println("<Arduino is ready>  Enter Azimuth and Elevation separated with a semicolon (;) ");
}
 //-------------------------------SETUP-----------------------------------
 //-------------------------------LOOP-----------------------------------
void loop()
{
// delay(50);
//--------------- PITCH ------------------------------------------------------------
  
if(y > elevation)
{lepos--;
elevation12.write(lepos);
}
else{}
lepos = constrain(lepos, 0, 180);


if(y < elevation)
{lepos++;
elevation12.write(lepos);}
else{}
lepos = constrain(lepos, 0, 180);
//-------------------------------------------------------------------------------------
 unsigned int pwm_value;
 pwm_value = pulseIn(PWM_PIN, HIGH);
 
 map(pwm_value, 9863, 29532, 0, 360);
 Serial.print("PWM:");
 Serial.println(pwm_value);
 
if(pwm_value > azimuth)
{leposdue--;
azimuth9.write(leposdue);
}
else{}
leposdue = constrain(leposdue, 0, 180);


if(pwm_value < azimuth)
{leposdue++;
azimuth9.write(leposdue);}
else{}
leposdue = constrain(leposdue, 0, 180);

Serial.print("leposdue :");
Serial.println(leposdue);


//--------------------------------------------------------------------------------------

Serial.print("lepos ");
Serial.println(lepos);
Serial.print("elevation ");
Serial.println(elevation);
Serial.print("y ");
Serial.println(y);


 Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read(); AcY=Wire.read()<<8|Wire.read(); AcZ=Wire.read()<<8|Wire.read(); 
int xAng = map(AcX,minVal,maxVal,-90,90); int yAng = map(AcY,minVal,maxVal,-90,90); 
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI); y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI); z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

y= constrain(y, 0, 180);







    



  
   recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
   }
}
//-------------------------------LOOP-----------------------------------

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial.available() > 0 && newData == false)
   {
      rc = Serial.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[16]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ";");  // delimiters, semicolon
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   //Serial.println(index);
   // print all the parts
   /*Serial.println("The Pieces separated by strtok()");
   for (int n = 0; n < index; n++)
   {
      Serial.print("piece ");
      Serial.print(n);
      Serial.print(" = ");
      Serial.println(strings[n]);
   }*/
   // convert string data to float numbers
   azimuth = atof(strings[0]);
   elevation = atof(strings[1]);
   newData = false;
   Serial.print("Azimuth = ");
   Serial.print(azimuth, 3);
   Serial.print("   Elevation = ");
   Serial.println(elevation, 3);
   Serial.println(); // blank line

}

You're not assigning the result of the map to anything.

I looked at the syntax and usage of map in other codes

You did not look hard enough

See map() - Arduino Reference

Thank you

sometimes i just cant see obvious mistakes when I go over the code too much

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