Mapping and Global Variables

Hi I need help with a little problem I’m having. I have never posted on a forum before so bare with me. I have sensors going to the analog inputs of an arduino mini pro. I am using the mapping function to change the values to 0 to 255. The problem is that when I go to access this variable it doesn’t seem to keep the reading going. Like if I wanted to print the new mapped values in the main loop it doesn’t show the values.
I have screwed around with it a lot and was able to see the values if I stay in my calibrate function and do serial.print there. But once I left that function and tried to access the new mapped variable it doesn’t work even though I declared the variable as global. Here is the code please help if you can.

#include "pitches.h"


//Calibration Variables

long A0Max;
long A1Max;
long A2Max;
long A3Max;
long A4Max;
long A0Min = 150;
long A1Min = 150;
long A2Min = 150;
long A3Min = 150;
long A4Min = 150;

long sensorA0 = analogRead(A0);
long sensorA1 = analogRead(A1);
long sensorA2 = analogRead(A2);
long sensorA3 = analogRead(A3);
long sensorA4 = analogRead(A4);





//Motor Pins


const int a1 = 5;
const int a2 = 9;
const int a3 = 6;
const int a4 = 3;


//Debounce Variables


int lastButtonState = LOW;
int buttonState;
long lastDebounceTime = 0;
long debounceDelay = 50;




//Sound Variables

int note = NOTE_E6;




void setup(){

  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  pinMode(A4,INPUT);
  pinMode(3,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(2,INPUT);

  Serial.begin(9600);
}




//Main Program

void loop(){



  if(debounce()==HIGH){

    playSound2(200);
       calibrate();
   
    
    while(1){
    


      Serial.println(sensorA0);
      Serial.println(sensorA1);
      Serial.println(sensorA2);
      Serial.println(sensorA3);
      Serial.println(sensorA4);
      delay(400);

    
    }

  }


}


//Calibration


long calibrate(){

  long time = millis();
  int lineLocation;






  while(millis()-time<5000){

    if(analogRead(A0) > A0Max){
      A0Max = analogRead(A0);
    }
    if(analogRead(A0) < A0Min && analogRead(A0) >20){
      A0Min = analogRead(A0);
    }
    if(analogRead(A1) > A1Max){
      A1Max = analogRead(A1);
    }
    if(analogRead(A1) < A1Min && analogRead(A1) >20){
      A1Min = analogRead(A1);
    }
    if(analogRead(A2) > A2Max){
      A2Max = analogRead(A2);
    }
    if(analogRead(A2) < A2Min && analogRead(A2) >20){
      A2Min = analogRead(A2);
    }
    if(analogRead(A3) > A3Max){
      A3Max = analogRead(A2);
    }
    if(analogRead(A3) < A3Min  && analogRead(A3) >20){
      A3Min = analogRead(A3);
    }
    if(analogRead(A4) > A4Max){
      A4Max = analogRead(A4);
    }
    if(analogRead(A4) < A4Min  && analogRead(A4) >20){
      A4Min = analogRead(A4);
    }

  }



  sensorA0 = map(sensorA0, A0Min, A0Max, 0, 255);
  sensorA0 = constrain(sensorA0, 0, 255);
  sensorA1 = map(sensorA1, A1Min, A1Max, 0, 255);
  sensorA1 = constrain(sensorA1, 0, 255);
  sensorA2 = map(sensorA2, A2Min, A2Max, 0, 255);
  sensorA2 = constrain(sensorA2, 0, 255);
  sensorA3 = map(sensorA3, A3Min, A3Max, 0, 255);
  sensorA3 = constrain(sensorA3, 0, 255);
  sensorA4 = map(sensorA4, A4Min, A4Max, 0, 255);
  sensorA4 = constrain(sensorA4, 0, 255);





}


//Sounds

void playSound2(int x){


  tone(8, note,100);
  delay(x);
  noTone(8);
}


//Debounce

int debounce(){


  int reading = digitalRead(2);

  if (reading != lastButtonState){

    lastDebounceTime = millis();

  }

  if((millis() - lastDebounceTime) > debounceDelay) {

    buttonState = reading;
    return buttonState;
  }

  lastButtonState = reading;
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

To get the new value of the analog input you have to do another analogRead(). You set sensorA0 (et al) at initialization time and never set it again.

Note: 'int' is plenty long for an analogRead() (10 bits value). Using 'long' is a waste of space and time.

Time values should be 'unsigned long', not just 'long'.

    while(1){
      Serial.println(sensorA0);
      Serial.println(sensorA1);
      Serial.println(sensorA2);
      Serial.println(sensorA3);
      Serial.println(sensorA4);
      delay(400);
    }

while(1) means when the code gets to this point, don't do anything else but this until the end of time. The values will never change because you've told them you don't want them to change, just to get printed every 4 tenths of a second.

Both very informative replies thank you.

But now my question is how to have these values by constantly updating within a loop WITH the new mapped values. I just don’t understand. Does this mean I have to re-map everytime I want to print the values on the screen? The variable sensorA0 represents AnalogRead(A0) and it is a global variable does that not mean that when I declared sensorA0 as a new mapped value should that just be printing the new mapped value when I say Serial.println(SensorA0).

Thank you again for your time.

The variable sensorA0 represents AnalogRead(A0) and it is a global variable does that not mean that when I declared sensorA0 as a new mapped value should that just be printing the new mapped value when I say Serial.println(SensorA0).

No, the variable sensorA0 does NOT represent analogRead(A0). At some point, sensorA0 was assigned the value returned by analogRead(A0). But, that does NOT mean that every time you use sensorA0 later that a new read will occur. The global assignment is not a binding operation as in C#.