Yeah, it cant get simpler than this but I cant make it work. This is my first program.
When I upload the program to the arduino the motor keeps running regardless of me pushing the button or not. Any help?
"butt" has the value 2.
2 is never less than zero.
2 is never equal to zero.
Reading pin zero is not useful.
Please post code, not screen shots.
Hi AWOL thank you for your reply.
I guess you already know what I'm trying to do.
I changed it like this and I keep getting the same result.
int butt = 2; //push button in pin 2
int motor = 7; //motor in pin 7
void setup () {
pinMode(butt, INPUT); //push button
pinMode(motor, OUTPUT); //motor
}
void loop(){
if (digitalRead(butt == HIGH))
digitalWrite(motor, HIGH);
if (digitalRead(butt == LOW))
digitalWrite(motor, LOW);
}
I think I'm using digitalWrite in the wrong way, but I'm not sure how. Could you drop a hint?
Still pretty much the same problem.
2 is never equal to 1, and as I already said, 2 is not equal to zero.
Pin zero is not the pin you want to read. (That was the hint you wanted)
Look carefully at parentheses in your code.
digitalWrite does not look to be your problem.
Please use code tags when posting code.
AWOL has nailed your problem for you, but how is this motor wired up? Unless it's a very tiny one such as a pager vibration device, it shouldn't be powered from the arduino for fear of damaging it.
Even tiny phone vibras can draw 70+mA.
Don't connect one directly to an Arduino pin.
Sorry, but I'm a total novice (and not english, so sorry if it sounds bad), but I don't see the faliure too, the only thing I see in this code are the missing brackets in the if() statement :
if (digitalRead(butt==HIGH)){
do something;
}
But I don't know if it's correct in only one line.
The brackets (aka parentheses - see my earlier reply) aren't missing.
If they were, the compiler would have complained.
Here is what i found in the digitalRead examples. there is one very similar to what im doing but with a LED.
code:
int butt = 2; //push button in pin 2
int motor = 7; //motor in pin 7
int val = 0;
void setup () {
pinMode(butt, INPUT); //push button
pinMode(motor, OUTPUT); //motor
}
void loop(){
val = digitalRead(butt);
digitalWrite(motor, val);
}
Now it works but it seems to stop after about a second.
Now as AWOL said the motor starts randomly when plugged directly into the Arduino. It seems now I have a hardware problem. What is the better (and safer) approach?
Also how do I put code tags?
Thanks a bunch!!
but it seems to stop after about a second.
That'll be the output stage overheating.
Have a look in the Playground for examples of how to buffer a motor with a transistor.
Alright, I'm on that. Thanks a lot for the help.