Change state of pin depending on PWM value of different pin

Hi everyone,

Any help is appreciated. I would like to change the value of a pin from high to low based on the output value on another pin without having to physically measure it using the analog pins.

For example:
I am using the analogWrite() command to send a PWM signal to pin 5 on my Uno. I would like Pin 6 to go high if pin 5 is sending >=150, and Low if it is <150.

I managed to get this working by connecting pin 5 to A0 and using the analogRead() command. But I would rather use only code if possible, not additional wiring.

Any ideas?

Thanks!

mgale31:
Any ideas?

Thanks!

analog read wont work as you expect on a pin outputting PWM.

Just write to the digital pin contingent on what value you pass to the analog pin...

var myInput = getSomeValue();
analogWrite(somePin, myInput);
myInput >= 150? digitalWrite(someOtherPin, HIGH) : digitalWrite(someOtherPin, LOW);

Thanks @BulldogLowell!

I can't seem to get it to work. Do you mind having a look at my code? The If statement isn't working. No matter what I set rightGreen to, rightBlack will not change state.

 int rightGreen = 5;
 int rightBlack = 7;
 
 const int upperThreshold = 100; //Threshold values for the black wire logic
 const int lowerThreshold = 50;
 
 
 void setup() {
    pinMode(rightBlack, OUTPUT);
    pinMode(rightGreen, OUTPUT);
}

void loop() {


analogWrite(rightGreen, 90);


//check condition of right green wire
  if (rightGreen > upperThreshold || rightGreen < lowerThreshold) 
  {
    digitalWrite(rightBlack, HIGH);
  } 
  else 
  {
    digitalWrite(rightBlack, LOW);
  }
}

rightGreen is the output PIN not the value to be tested...

you set it here:

analogWrite(rightGreen, 90);

why are you then immediately comparing it here:

  if (rightGreen > upperThreshold || rightGreen < lowerThreshold) // you KNOW it is 90!!

??

rightGreen is 5...

So your test comes out to:

if (5> 100 || 5 < 50)

5 is always less than 50, so the if statement is always true. You probably wanted to test the duty cycle you set the pin to, not the number of the pin.

You want:

 int rightGreen = 5;
 int rightBlack = 7;
 
 const int upperThreshold = 100; //Threshold values for the black wire logic
 const int lowerThreshold = 50;
 
 
 void setup() {
    pinMode(rightBlack, OUTPUT);
    pinMode(rightGreen, OUTPUT);
}

void loop() {


analogWrite(rightGreen, 90);


//check condition of right green wire
  if (90 > upperThreshold || 90 < lowerThreshold) 
  {
    digitalWrite(rightBlack, HIGH);
  } 
  else 
  {
    digitalWrite(rightBlack, LOW);
  }
}

[/quote]

Thanks guys!
I should have been more clear. the code I attached above is just testing a theory. Eventually I will have a potentiometer connected to the circuit that will be changing the value of "rightGreen", and I want rightBlack to respond accordingly. I was just using this code to manually change the value rightGreen to see if rightBlack responded appropriately.

You guys got me sorted out.

I just created another variable to run the if statement. This does exactly what I want. Hopefully this is correct practice?

 int rightGreen = 5;
 int rightBlack = 7;
 int rightGreenVal = 0;
 
 const int upperThreshold = 100; //Threshold values for the black wire logic
 const int lowerThreshold = 50;
 
 
 void setup() {
    pinMode(rightBlack, OUTPUT);
    pinMode(rightGreen, OUTPUT);
    rightGreenVal = 84;
}

void loop() {


analogWrite(rightGreen, rightGreenVal);


//check condition of right green wire
  if (rightGreenVal > upperThreshold || rightGreenVal < lowerThreshold) 
  {
    digitalWrite(rightBlack, HIGH);
  } 
  else 
  {
    digitalWrite(rightBlack, LOW);
  }

}

Thanks again for the guidance guys. Much appreciated

I don't understand why you have thresholds

if (rightGreenVal > upperThreshold || rightGreenVal < lowerThreshold)

a PWM pin driven by analogWrite() will be either HIGH or LOW with nothing in between.

If you want a second external item to see the same value as is on the PWM pin my first question is why not just connect the two things to the PWM pin?

...R