This might be fairly easy for some of you, but because I'm new to learning programming languages I'm feeling very discouraged.
What I need is for an LED to turn on for 1 second after a pin gets a 5V signal 5 times.
So every 5 times I give the pin 5V, the LED turns on for one second.
From my basic understanding, I should create a for loop?
I am currently using pin#12 as LED output and pin#A0 for an analog input.
What I have so far, but not really what I'm trying to do:
CODE*****
const int buttonPin = A0;
const int ledPin = 12;
int buttonState = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
CODE*****
Thank you guys for your time, I really appreciate it!
From my basic understanding, I should create a for loop?
Wrong. You are already in a loop. loop() is called over and over again so all you need is a counter.
Mark
This should give you an idea. As holmes4 said, you just need a way to keep track of the button presses. There are many ways to do this depending on exactly what you're wanting this to do. As for the example below, you may want to look at a topic called "debounce" so the button presses are kept separate. Also look at the subject of delay with millis, using the delay function as I have used will stop all further function until the delay is over. (Other than interrupts, but that's a different topic.)
const int buttonPin = A0;
const int ledPin = 12;
int buttonState = 0;
int buttonCount = 0; //iterator for keeping count of current button presses
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonCount += 1; //increment iterator by 1
if (buttonCount >= 5) //has the button been pressed at least 5 times?
{
digitalWrite(ledPin, HIGH);
delay(1000); //1000 milliseconds = 1 second
digitalWrite(ledPin, LOW);
buttonCount = 0; // reset buttonCount for next round
}
}
}
Almost …
DustinB:
const int buttonPin = A0;
const int ledPin = 12;
int buttonState = 0;
int buttonCount = 0; //iterator for keeping count of current button presses
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
boolean newButtonState = digitalRead(buttonPin);
if (buttonState == LOW && newButtonState == HIGH) {
buttonCount += 1; //increment iterator by 1
if (buttonCount >= 5) //has the button been pressed at least 5 times?
{
digitalWrite(ledPin, HIGH);
delay(1000); //1000 milliseconds = 1 second
digitalWrite(ledPin, LOW);
buttonCount = 0; // reset buttonCount for next round
}
}
buttonState = newButtonState;
}
Wow. You guys are really awesome. Thank you guys for your time. It is greatly appreciated!!