HELP NEEDED

Hi there!!!

I am fairly new to this, but got hooked to try and experiment with the arduino, and now I am stuck in a small project.

I am trying to hook up a sensor to a gauge.
I measured the sensor, and it goes from 1023 to 200, on the analog in pin. 1023 is empty and 200 is full.
This I want to remap (or how it is done) to 175 to 20 on the digital pin. 175 is empty on the gauge and 20 is full. This I measured in the 0 till 255 range.

Is there someone out here that can help me write this sketch? I tried and tried but without succes.

Thanks in advance

If you have read the data from the sensor and checked the output values to drive the gauge you obviously do have some working code. So post your best attempt at putting both things together (read "How to use this forum" first and follow the instructions). Then we'll gladly help you to sort it out.

It sounds like you just need something like:

gaugeOutput = map (sensorInput, 1023,200, 175, 20);

but since I can't see any code I don't know what your variables are called or where the line needs adding.

Steve

Nearly everyone posting on this forum needs help, so please edit your post to have a more descriptive title.

This I want to remap (or how it is done) to 175 to 20 on the digital pin

A digital pin is either LOW or HIGH not between 20 and 175... what do you mean exactly - using PWM ?

If you subtract the reading from 1023

answer = 1023 - reading

then you will get 0 to 823 from 1023 to 200 and do it with ints which are way faster than floats.

If you multiply that by 1000 and then divide by 823, you will end up with 0 to 1000.

answer = ( 1023 - reading ) * 1000 / 823; // use int variables.

This is what i have so far. But i cannot get the right voltage at pin 11. it is at a constant 3,5V

const int sensorPin = A1;
int sensorValue = 0;
int mappedValue = 11;

void setup() {

Serial.begin (9600);
pinMode (sensorPin, INPUT);
pinMode (mappedValue, OUTPUT);
}

void loop() {
// read the value from the sensor
// the range from an analog input is 0 - 1023
sensorValue = analogRead(sensorPin);

mappedValue = map(sensorValue, 1023, 200, 175, 20);
Serial.print("map 262 - 35 to 20 - 175: ");
Serial.println(mappedValue);

analogWrite (mappedValue,map);

}

it is at a constant 3,5V

How are you measuring that?

What is connected to the analog pin? A schematic would be useful, as would seeing your serial output.

int mappedValue = 11;            
 
....

  mappedValue = map(sensorValue, 1023, 200, 175, 20);

....
  analogWrite (mappedValue,map);

You're using the same variable name for the output pin number and the mapped output value. Then you're doing an analogWrite to a pin number that you've set to 175-20. That's not going to work.

Steve

thanks steve!!!

made some changes in the names, and it is working now!!!

const int analogInPin = A1;
const int analogOutPin = 11;

int sensorValue = 0;
int outputValue = 0;

void setup() {
Serial.begin(9600);
}

void loop(){
sensorValue = analogRead(analogInPin);
int val = analogRead(0);
val = map(val, 1023, 200, 175, 20);
Serial.println(val);
analogWrite (analogOutPin, val)

}

You understand that you are not outputting a constant voltage at a certain value but using PWM, right ? (Assuming you are on a uno or similar)

sensorValue = analogRead(analogInPin);

That seems like a useless thing to do, since you never use the value in sensorValue.