system
April 15, 2012, 7:56pm
1
Here I have a problem because I'm giving double command to arduino.
Turn LED on when brightness is under 30
Turn LED off when brightness is over 30
When button is pressed, turn off LED
#1 and #3 is in conflict with one another in my current code.
Is there something like "OR"
I tried with else and if, and not too successful.
const int buttonPin = 8;
const int ledPin = 10;
int buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
buttonState = digitalRead(buttonPin);
if(buttonState == LOW || sensorValue >= 30)
{
digitalWrite(ledPin, LOW);
}else if(sensorValue < 30)
{
digitalWrite(ledPin, HIGH);
}
delay(200);
}
cmiyc
April 15, 2012, 8:19pm
2
I'm not sure I follow your issue.
if(buttonState == LOW || sensorValue >= 30) {
digitalWrite(ledPin, HIGH);
} else if(sensorValue < 30)
This code turns the LED on when SensorValue is 30 or greater. Your description of #1 and #2 seems to imply you want the opposite behavior. Which is it?
system
April 15, 2012, 8:26pm
3
My apologies, was working with so many experiments and I copy and pasted the wrong code. Fixed now.
cmiyc
April 15, 2012, 8:31pm
4
blanclait:
My apologies, was working with so many experiments and I copy and pasted the wrong code. Fixed now.
Okay, well now the code matches, I still don't see a problem.
The code says if the button is low (pressed?) OR sensorValue is over 30, the LED gets turned OFF. Otherwise, it gets turned ON. Isn't that what you describe?
Edit: Added a missing "don't"
cmiyc
April 15, 2012, 8:35pm
6
So, then what's your question?
system
April 15, 2012, 8:37pm
7
I thought the command I have written for Arduino was in conflict as it failed to function.
That is the button did not do anything.
cmiyc
April 15, 2012, 8:57pm
8
You didn't enable the internal pull up, are you using an external pull up resistor?
The LED will only be off for as long as the pin is low (e.g. Pressed)
system
April 15, 2012, 9:12pm
9
yes, I do realize that LED will be OFF only when button is being pressed.
(planning on doing 1 button press without holding next)
Yeap, I am using external pull up resistor.
system
April 16, 2012, 12:46am
10
blanclait:
#1 and #3 is in conflict with one another in my current code.
You need to decide which of those rules you want to take precedence, and then write the code so that it does.
system
April 16, 2012, 2:14am
11
I think this will work, assuming the button input goes low when it's pressed:
boolean state = ((sensorValue < 30) && !buttonState);
digitalWrite(ledPin, state);
system
April 16, 2012, 2:19am
12
No, I'm wrong, argh.
if(buttonState = false)
{
state = (sensorState < 30);
digitalWrite(ledPin, state);
}
else digitalWrite(ledPin, false);
...That should work, I think.
system
April 16, 2012, 2:32am
14
Dar god, I hate it when I mess up in that particular way. That's part of why I use a compiler that warns me about those.