How to get readings from 2 different "anaglogWrite"

I want to get readings two resitors, potentiometer and photosensor, and have them add up but they are overwriting each other

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A1; // Analog input pin that the photosensor is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int sensorValue2 = 0;
int outputValue = 0; // value output to the PWM (analog out)
int outputValue2 = 0;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
sensorValue2 = analogRead(analogInPin2);
// map it to the range of the analog out:
outputValue = map(255-sensorValue, 0, 1023, 0, 255);
outputValue2 = map(255-sensorValue2, 0, 1023, 0, 255);

// change the analog out value:
analogWrite(analogOutPin, outputValue2);
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print(“sensor = " );
Serial.print(sensorValue);
Serial.print(”\t output = ");
Serial.println(outputValue);

Serial.print(“sensor = " );
Serial.print(sensorValue2);
Serial.print(”\t output = ");
Serial.println(outputValue2);

// wait 5 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(5);
}

any help will be useful :slight_smile:

Put some delay between the 2 readings

sensorValue = analogRead(analogInPin); sensorValue2 = analogRead(analogInPin2);

The incoming signals need some time to settle thru the analog mux & get converted.

// change the analog out value:
analogWrite(analogOutPin, outputValue2); 
analogWrite(analogOutPin, outputValue);

I don't know what "add them together" and "overwrites each other" means. However, this section of code means the pin 'analogOutPin' will, most of the time, be outputting a PWM value of 'outputValue.' Writing two different values back to back are just going to result in the last value you write.

First of all: outputValue = map(255-sensorValue, 0, 1023, 0, 255); outputValue2 = map(255-sensorValue2, 0, 1023, 0, 255); has to be: outputValue = map(1023-sensorValue, 0, 1023, 0, 255); outputValue2 = map(1023-sensorValue2, 0, 1023, 0, 255); or outputValue = map(sensorValue, 0, 1023, 255, 0); outputValue2 = map(sensorValue2, 0, 1023, 255, 0);

Second, you are sending two command in sequence on the same output. Of course, last command overwrite previous one, immediately.

analogWrite(analogOutPin, outputValue2); analogWrite(analogOutPin, outputValue);

So, what is your idea, what you try to manage with one output and two inputs?

Thanks, we are still beginners at this and it would be nice if there was an alter way of writing this code that could allow for us to use two different values taken from the two resistors to make our LED dim and brighten. Right now the LED only takes in account for one resistor's readings and dims according to that. We want it to take in account both readings to determine the brightness of the LEDs. Thank you

And YES, We are trying to use 2 inputs and only one single LED output!! Any code for that??

try this:

int output = (sensorValue + sensorValue2) / 2; output = map(output, 0, 1023, 255, 0); analogWrite (analogOutPin, output);

cheers

p