Hello guys and girls,
I'm completely new to arduino, but i'm feeling very excited!
i searched a lot for a simple switch code... and didn't find any that I liked... so I tried and tried and made out this:
const int buttonPin = 8;
const int ledPin = 13;
int buttonState = 0;
int antState = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
while(buttonState == 0)
{
buttonState = digitalRead(buttonPin);
}
antState = 1 - antState;
digitalWrite(ledPin,antState);
delay(200);
buttonState = digitalRead(buttonPin);
}
Isn't big deal but made me very happy!! I just wanted to share... =)
thanks a lot!!!
But now, while the button isn't pressed the code is locked in that while loop and can't do anything else. This is blocking code, and it works for doing one thing. But as soon as you want the program to do more than one simple thing it blows up on you.
If you want to see the problem for yourself, try to add a second LED to this. The second LED should just blink, 500ms on and 500ms off. But it should do that all the time, no matter the state of the button.
true... i can put some code inside the while....
but if i put too much code it will get lazy to respond the button click...
anyway... I was just trying to do a simple switch for some basic function... It seems to be working properly... when I start to build bigger things I'll need to search something more stronger...
renanmmagalhaes:
true... i can put some code inside the while....
but if i put too much code it will get lazy to respond the button click...
anyway... I was just trying to do a simple switch for some basic function... It seems to be working properly... when I start to build bigger things I'll need to search something more stronger...
I have not checked, but the execution time for each command is well below 1µS.
That would take quite some code before you will even notice a dealy from a mechanical button.
I'd say you could have all your code in that while loop, and still not get any noticeable delay.
Arduino is a microprocessor, not an early type motordriven mechanical calculator
I have not checked, but the execution time for each command is well below 1µS.
First you should check,
second there are no commands in C/C++ function call yes, statements yes, but no commands.
@op
true... i can put some code inside the while....
Stupid - you don't put more code in the while loop YOU GET RID OF THE WHILE LOOP.
Mark
holmes4:
@op
Stupid - you don't put more code in the while loop YOU GET RID OF THE WHILE LOOP.
Mark
You are a nuisance Mark.
First I found a screaming post from you elsewhere shouting out in fat capital letters, and now here this insulting language.
Try being a bit more civil and gain some respect.
I think he didn't noticed the "Newbie" status... =P
So far in all your posts you have yet to get anything right. Go away, learn , at leat a little. But most of all keep your "help" to your self.
Mark