My map values are going over their highs and lows!


i'm mapping 2 ultrasonic sensor readings pwm (255). raw sensor reading between 0 , approximately 15, in centimeters. after readings taken, subtracted pwm value. problem reading on highest limit of map! code below..

code: [select]
#include <newping.h>

#define trigger_pinl  3
#define echo_pinl     2

#define trigger_pinc  5
#define echo_pinc     4

#define trigger_pinr  8
#define echo_pinr     7

#define max_distance 500        // centimeters

newping sensorl(trigger_pinl, echo_pinl, max_distance);
newping sensorc(trigger_pinc, echo_pinc, max_distance);
newping sensorr(trigger_pinr, echo_pinr, max_distance);

const int numreadings = 2;

long readingsl[numreadings];      // readings ping sensors
long readingsr[numreadings];
long readingsc[numreadings];
int index = 0;                    // index of current reading
long totall = 0;                  // running total each sensor
long totalr = 0;
long totalc = 0;
long averagel = 0;                // average 3 sensors
long averager = 0;
long averagec = 0;
int lavcm;                        // average ping sensors in centimeters
int ravcm;
int cavcm;
int right = 9; //this line changed digital pin 12
int left = 10;//this line changed digital pin 13


void setup()
{
 // initialize serial communication computer:
 serial.begin(9600);  
 // motor negative wires low
 analogwrite(6, 0);
 analogwrite(11, 0);
 
 // initialize readings 0:
   for (int thisreadingl = 0; thisreadingl < numreadings; thisreadingl++)
     readingsl[thisreadingl] = 0;          
   for (int thisreadingr = 0; thisreadingr < numreadings; thisreadingr++)
     readingsr[thisreadingr] = 0;  
   for (int thisreadingc = 0; thisreadingc < numreadings; thisreadingc++)
     readingsc[thisreadingc] = 0;
 
   
}

void loop() {
 index = 0;
 for(index=0; index < numreadings; index++){
   // subtract last reading:
   totall = totall - readingsl[index];
   totalr = totalr - readingsr[index];
   totalc = totalc - readingsc[index];
   
   
   // read sensor:  
   readingsl[index] = sensorl.ping();
   readingsr[index] = sensorr.ping();
   readingsc[index] = sensorc.ping();
   
     
   // add reading total:
   totall = totall + readingsl[index];
   totalr = totalr + readingsr[index];
   totalc = totalc + readingsc[index];
 

   // advance next position in array:
 }

 // if we're @ end of array...                        


 // calculate average:
 averagel = totall / numreadings;
 averager = totalr / numreadings;
 averagec = totalc / numreadings;


 // average in centimeters
 lavcm = averagel / 148;  //converts microseconds centimeters
 ravcm = averager / 148;  //converts microseconds centimeters
 cavcm = averagec / 148;  //converts microseconds centimeters
 lavcm = constrain(lavcm, 1, 200); //will not read more 122 centimeters ( 1/3 of max constraint)  
 ravcm = constrain(ravcm, 1, 200); //will not read more 122 centimeters
 cavcm = constrain(cavcm, 1, 255); //will not read more 366 centimeters (max constraint)
 int ravcmbigger = averager / 100;
 int lavcmbigger = averagel / 100;
 int cavcmbigger = averagec / 100;
 
 // use test sensor readings
 serial.print("left reading: ");  //debugging information displays left sensor average in centimeters
 serial.println(lavcm);
 serial.print("center reading: ");  //debugging information displays center sensor average in centimeters
 serial.println(cavcm);
 serial.print("right reading: ");  //debugging information displays right sensor average in centimeters
 serial.println( ravcm);
 serial.println(" ");
//  
 
 // delay between reads stability . . 8ms == 0 .008 seconds
 //delay(1);
 delay(500);
int lx = lavcm - ravcm;
int rx = ravcm - lavcm;
lx = map((lx), 0, 50, 255, 0);
rx = map((rx), 0, 50, 255, 0);
serial.println(rx);
serial.println(lx);
serial.println(" ");
analogwrite(right, lx);
analogwrite(left, rx);

}

if output outside range input well. map not constrain output. use constrain function if want clamp output.


Arduino Forum > Using Arduino > Programming Questions > My map values are going over their highs and lows!


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support