i am very new to this.
all i am trying to do is make the led come on when i push a button
here is the code;
const int pin = 2;
const int led = 13;
int state = 0;
void setup() {
// put your setup code here, to run once:
pinMode (pin, INPUT);
pinMode (led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (state = HIGH) {
digitalWrite(led, HIGH);
}
}
system
March 3, 2016, 1:30pm
2
ok im an idiot
Only because you failed to define how the switch is wired. Well, and you failed to post your code correctly.
Well, and because you failed to actually read the pin state AND you are using = where you mean ==.
system
March 3, 2016, 1:31pm
3
(state = HIGH) {
That's an assignment, the result of which is always non-zero, and therefore always true
What you may want is
(state == HIGH) {
(You may also want to turn the LED off at some point)
MarkT
March 3, 2016, 1:36pm
4
Something like this in loop()?
digitalWrite (led, digitalRead (pin)) ;
after posting this of course i found some more information.
i also read the part on how to post.
i will do it in the future.
i will try the suggestions
i will play with this a little more.
thanks all for the help.
alright.
still do not understand what is wrong
the switch supplies 5v to pin 2
still will not turn on or off pin 13 led
TESTPROGRAM.ino (364 Bytes)
hope this is the right way to include the code
const int pin = 2;
const int led = 13;
int state = LOW;
void setup() {
// put your setup code here, to run once:
pinMode (pin, INPUT);
pinMode (led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead (state == HIGH))
{
digitalWrite (led, HIGH);
delay(3000);
}
else
{
digitalWrite (led, LOW);
delay(3000);
}
}
if (digitalRead (state == HIGH))
Wrong
hope this is the right way to include the code
Close, but no cigar. Select the code and click the code tags icon (</>) to add code tags
const int pin = 2;
const int led = 13;
int state = LOW;
void setup() {
// put your setup code here, to run once:
pinMode (pin, INPUT);
pinMode (led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead (state == HIGH))
{
digitalWrite (led, HIGH);
delay(3000);
}
else
{
digitalWrite (led, LOW);
delay(3000);
}
}
if (digitalRead (state == HIGH))
Pointed out already
And also already pointed out:
PaulS:
Only because you failed to define how the switch is wired.
system
March 3, 2016, 3:13pm
11
if (digitalRead (state == HIGH))
should be
if (digitalRead (2) == HIGH)
Even better
if (digitalRead (pin) == HIGH)
or even better with a meaningful name for the pin
if (digitalRead (inputPin) == HIGH)
assuming the switch is wired to take the input HIGH when pressed and that it is help LOW normally.
Just wanted to thank you all for the assistance.
I see the areas i need to pay more attention too.
Thanks,
Bill