Go Down

Topic: Quick question about map( (Read 932 times) previous topic - next topic

PaulS

Quote
When i click the button i need my top line to go to zero.
That will happen only is val goes to 0, and that will happen only if math == num. And, since you reset num to 0 every time, then val will be 0 only if math == 0.
The art of getting good answers lies in asking good questions.

Pepsicoke

Ok let me try to understand this. When you say im putting num to 0 every time do you mean.


  static long currentSample; // current sensor sample.
  static long currentValue;  // current sensor value.
  int num;
  num = 0;   <======(This part)
  currentValue = 0;

I just took that part out and still getting 4009 on my top line and bottom line is reading correct at 5349.

julyjim

Could this clarify the map function ?
You are dealing with three entities
range  - independent value specifying the input "range" - for a sake of argument  0 thru 1023
input  range of interest - related to "range" - lower and upper limit
output range of interest - related to "input range of interest" - lower and upper limit

In this example, modified / taken form Arduino doc 
the "range" 0 thru 1023  is directly mapped to output 0 thru 255.

If you  change the int range = 400 your output will no longer start from 0  but from a point where 400 maps to output range.

You can modify both input and output range of interest also. It gets messy but still follows the basic map function - maps the "range"  according to "input / output ranges" relations.

I am not clear why you have to change both "range" and the lower value of the "input range" (same time). .



Code: [Select]

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 255; i++)
    Serial.println(""); // cleanup monitor screen
  int input, output;
  for (int range = 0; range < 1023; range += 100)
  {
    output = map(range, 0, 1023, 0, 255);
    Serial.print("Input   ");
    Serial.println(range);
    Serial.print("Output  ");
    Serial.println(output);
    // put your setup code here, to run once:
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

Pepsicoke

Ok let's try piece by piece. Math is ran by my for statement. This gets me 10 readings an average of my 1 readings. What I'm trying to do is when I press a button it takes a analogread (I have ads1115 analog to digital converter) and puts it in place of num on the map function. This will in turn make my readings turn to zero if math and num are the same in turns of adc readings. Now the 0-20000 is the min and max that I want it to be mapped/converted to. With how my reading me are coming out right now I get 4000 for my top reading(if everything was working right it should be zero) and I get my analog to digital converter number on the bottom row of about 5349 (which is right) My whole reason for going this route was I was told I could. Is this not going to be possible? I feel like it should be but feelings are not always right lol.

PaulS

Quote
Math is ran by my for statement.
It is NOT "ran" by your for statement. It is VALUED by function calls in the for statement.

Quote
What I'm trying to do is when I press a button it takes a analogread
The snippet you posted does not show any switches. Buttons are for shirts.

Quote
and puts it in place of num on the map function.
Not in the snippet you posted.

Quote
My whole reason for going this route was I was told I could. Is this not going to be possible?
Instead of telling HOW you think it should be done, tell us what you are trying to accomplish. If you just want to print 0 on the top line of the LCD, that is super simple.
Code: [Select]
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("0");


But WHY do you want to do that? If you think that your code is supposed to use one reading as a baseline, and then compare the average of 10 readings to that baseline, you are going about it all wrong.
The art of getting good answers lies in asking good questions.

Pepsicoke

Paul it seems I am a little far off from how to do this. Maybe you have a better way of doing this that you wouldn't mind showing me. I want to take 10 readings from a sensor. Average then. Then use the map function to turn it into psi. I also want to have a momentary switch zero the sensor. That is why I had "num" where is was. Thank you for your patience with my lack of knowledge

PaulS

Quote
I also want to have a momentary switch zero the sensor.
So, whatever the value that is next read from the device is a baseline value. The average of the 10 readings should have the baseline value subtracted. map() is the wrong way to center the values about a baseline.
The art of getting good answers lies in asking good questions.

Pepsicoke

Paul what would you recommend instead of the map function

Delta_G

Paul what would you recommend instead of the map function
The average of the 10 readings should have the baseline value subtracted.
Basic third grade math.
If at first you don't succeed, up - home - sudo - enter.

julyjim

I feel silly, but here is a code of what I understand you are trying to do.

You a have  been told that when your  math = number  the output will be zero AKA the bottom / low level  of input range. You basically moving your point of reference along your value you are trying to convert / map.

( That' math = , not C language for you nitpickers)

You need to quit including the averaging code , map only cares about its variables not how you  got them.



Code: [Select]

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 255; i++)
    Serial.println("");
  int input, output;
  for (int input = 0; input < 1023; input += 10)
  {
    output = map(input, input, 1023, 0, 255);  moving input low level reference point !!
    Serial.print("Input   ");
    Serial.println(input);
    Serial.print("Output  ");
    Serial.println(output);
    // put your setup code here, to run once:
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

Delta_G

#25
Oct 20, 2016, 03:26 am Last Edit: Oct 20, 2016, 03:26 am by Delta_G
Code: [Select]
output = map(input, input, 1023, 0, 255);

Since input == input this gives 0 every time for any value of input. You might as well write:

Code: [Select]
output = 0;
If at first you don't succeed, up - home - sudo - enter.

septillion

I've read it a couple of times but I'm still lost what you want. What I get is you want to map a reading from the ADC to some range ( 0 to 20000). What I don't get is what the "10 reading" or "average of 10 readings" has to do with it...
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up