Programming problems

I am a new Arduino user and am having problems with figuring out some code issues.

My program below works but I would like my valvePin to turn on 2 seconds after my blowerPin turns on and not have the valvePin dependent on the sensorValue. I tried to do it with a if statement like

If (valvePin==HIGH) digitalwright (valvePin , HIGH) But it won’t work.

int sensorValue = A0;

int valvePin = 11;

int blowerPin = 12;

int powerPin = 9;

void setup() {

Serial.begin(9600);

pinMode(blowerPin, OUTPUT);

pinMode(valvePin, OUTPUT);

pinMode(sensorValue, INPUT);

pinMode(powerPin, OUTPUT);

}

void loop() {

int sensorValue = analogRead(sensorValue);

Serial.println(sensorValue);

int sensorReading = analogRead(sensorValue);

digitalWrite(powerPin, HIGH);

if (sensorValue > 500) digitalWrite(blowerPin, HIGH);

delay(2000);

if (sensorValue > 500) digitalWrite (valvePin, HIGH);

if (sensorValue < 200) digitalWrite(blowerPin, LOW);

delay(2000);

if (sensorValue < 200) digitalWrite (valvePin, LOW);

}

valvePin = 11, so will never equal HIGH.

I thought that “int valvePin = 11” redefines pin11 as valvePin so further in the code I don’t have to call it pin11 I can just call it valvePin. Am I wrong about this?

You are wrong!

You’ve defined an int variable called valvePin and assigned the value 11 to it.

This is simply a mistake you have written into your code. It may be that you were trying to use some pseudo code to explain your desired action.

You write that you tried the following:

If (valvePin==HIGH) digitalwright (valvePin , HIGH

When this is compiled if basically comes down to this:

if(11 == HIGH) digitalwrite(valvePin, HIGH)

Because HIGH is defined as the value 1, 11 never equals 1 and this whole if statement is optimized away and hence can’t be executed.

I suspect you meant something along the lines of:

if(digitalRead(blowerPin) == HIGH) digitalWrite(valvePin, HIGH);

I don’t know why you would read the state of the pin and then set it to the exact same state. Further, if you don’t want the valve pin to be reliant upon the sensor value, simply wait the 2 seconds and then match the output state of the blower pin.

int sensorVal = analogRead(sensorPin);
if(sensorVal>500)
{
  digitalWrite(blowerPin,HIGH);
} else if(sensorVal<200) 
{
  digitalWrite(blowerPin,HIGH);
}
delay(2000);
digitalWrite(valvePin,digitalRead(blowerPin));

Also you read the sensor value twice into two different variables, sensorReading and sensorValue. This is unnecessary and actually uses up CPU cycles on the Arduino.

Sonic Solar:
I thought that “int valvePin = 11” redefines pin11 as valvePin so further in the code I don’t have to call it pin11 I can just call it valvePin. Am I wrong about this?

Think about what happens when you set the pin’s mode or do a digitalWrite. The generic form of the former is :

pinMode(pin, mode)

So you tell it what pin number and what mode you want that particular pin to be. In your case these two statements are identical …

pinMode(valvePin, OUTPUT);

pinMode(11, OUTPUT);

… because valvePin = 11. It’s just easier to remember the valvePin controls the valve rather than pin 11 controls the valve. And if you ever have to change which pin controls the valve, changing it once where int valvePin = 11 is much easier than tracking down all the places where 11 might be have been used in some statement.

Thank you everyone. My project now works!