How to map 2 inputs into 1 output?

Hi everyone, I'm very new to coding and just learned the basics. I was hoping someone could show me the best way to take 2 different inputs and produce 1 output from those 2 inputs.

input 1: this number changes constantly between 0-300 read on input pin
input 2: has 3 numbers between 0-255 (user adjustable) example: 75, 245, 110

If the first input is between 0-100, i need to multiply it by in this case 75
If the first input is between 100-200, i need to multiply it by in this case 245
If the first input is between 200-300, i need to multiply it by in this case 110

How would an experienced coder write this?

Thanks much for your help

What are the inputs?
Serial, analog, pulses ?

That makes all the difference in how you evaluate them.

Why do you need to read input 2? The number to use as the multiplicand is not determined by reading input 2 it is determined by the range of values of input 1.

Keeping in mind the questions raised above, you could do if-else if -else statements, like the following pseudocode:

read in input a
read in input b
if (a < 101) {do this action, e.g. multiply a by value of b}
else if (a< 201) {do second thing}
else {do last option}

This is assuming your inputs are integers and will never be above 300 or below 0, and that value b can only be one value at a time.
If you need to store 3 different values for b, then you would either need to store them in an array

2 Likes

Thanks for the reply guys.

I have it written now using switch case, if else seems very similar.

2 Likes

In the spirit of helping others in future, you might consider posting your solution.

Full marks! Can you post your code please?

I did something like this... (this is a much simplified version of my code)
PS im just a beginner, I found that code somewhere online...

  pulseState = map(pulse, 0, 300, 0, 2);
  switch (pulseState) {

    case 0:  // starts at 0 to 100
(code here)
      break;
       
    case 1:  // starts at 100 to 200
(code here)
      break;
      
    case 2:  // starts at 200 to 300
(code here)
      break;

    default:
(outside of 0-300 code here)
      break;

You can use ellipses in a case to represent a range of values.

switch (value)
   {
      case 0 ... 100:
         // multiply by 75
         break;
      case 101 ... 200:
         // multiply by 245
         break;
      case 201 ... 300:
         // multiply by 110
         break;
   }
2 Likes

Oh that very cool, so I don't have to map anything

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