I'm working on a project, and I really need the function mentioned below:
When i push a button that I've connected to my arduino, I want an LED to light up (set output HIGH), and when i hit the same button again, I want the LED to turn off, just like a flashlight.
Does anyone have this sort of code?
Thanks!
The only problem is that the example sketch in the IDE is kinda "buggy". I have to press the button 3-4 times before the LED flash up.
Ive also tried to remove the Serial monitor part, but it haven't helped.
I'm sort of green when it comes to arduino programming. I've tried to compile your code, and fixed some errors (HIGH instead of high, missing parentheses etc.) but when I run the code, nothing happends! (im using the typical button wireing, GND, 5v and a 10k)
But I don't understand the debounce part you was talking about. is that why the scetch isn't working properly?
@AWOL
Your thinking of state change detection, this is the simpler version, debounce. Only I want him to understand why it is not working properly, and see if he can fix it.
Ok.
I figured out why the StageChangeDetection acted buggy. The push counter had should have 2 insted of 4 behind the percentage sign.
I really thing arduino programming is intresting, so I just want to ask; witch function has the percentage sign? I tried to google it, but I didn't get any smarter
Ok.
I figured out why the StageChangeDetection acted buggy. The push counter had should have 2 insted of 4 behind the percentage sign.
I really thing arduino programming is intresting, so I just want to ask; witch function has the percentage sign? I tried to google it, but I didn't get any smarter
Did you read the gray comments, it explains what that %4 means. (% in programing terms is called modulo, kinda like the remainder of a division problem)
%4 = every 4 button presses. if you chage the 4 to a 2 then it becomes every 2 button presses.
The project I've been working os is about lerarning to interface with IR remotes.
I got it set up and working with an IR library, and I'm able to run diffrent function when I press different buttons on the remote
(Code for remote control below):
I want to turn on and turn off an LED with the remote control. The thing is that I have no idea how to combine the button sketch I've modified to work with this IR example. Can anyone help?
const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{ buttonPushCounter++; }
else {}
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0)
{
digitalWrite(ledPin, LOW);
delay(30);
}
else
{
digitalWrite(ledPin, HIGH);
delay(30);
}
}
The thing is that I have no idea how to combine the button sketch I've modified to work with this IR example.
Yes you have. Code is a language, you tell the machine what to do each step of the way, that way you can get it to do anything. You must learn the language and techniques and not go asking about every little thing. For example you have the line:-
else {}
What do you think it does? It does nothing, so why have it in?
So if you want to light up an LED when you get a certian button push then add the commands to do it when you see the button push:-
else if (results.value == 0xFF22DD) // Play_pause button
{
Serial.println("Button: Play HEX: 0xFF22DD");
digitalWrite(ledPin, HIGH);
}
Yup, I got that part. But I want to hit the same button again and turn of the LED. That is the part I think is difficult.
Maybe some sort of "wait until something happens" function is whats needed?