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.
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);
}
}
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.
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