Many thanks for your help here. I am beginning to understand I think. Using your example I wrote this bit of code for a very simple project. It tests a single button. If it is pressed twice within the given time, it should perform a certain function. If it is outside of this time it should perform a different function. It works! I now understand functions a little better. But I fear my code is over engineered. Is there a smarter/leaner way of doing this and have I used "functions" in the right way? here is the code:
int switchPin = 2; // digital input pin for a switch
int yellowLedPin = 3; // digital output pin for an LED
int redLedPin = 4; // digital output pin for an LED
int flashPin = 13; // digital output pin for an LED
int switchState = 0; // the state of the switch not needed at the moment
int timerValue = 2000; // allowed duration value of time passed since button was pressed
int flashMe = 0; //why do I need to declare this here as well as within the function?
int switchState1 = 0;
int switchState2 = 0;
//int pin = 7;
unsigned long duration1;
unsigned long duration2;
int durationBetween = 0;
void yellowLedActive(int switchState1) {
// code contained in a function to turn yellowLedPin on and redLedPin off
digitalWrite(yellowLedPin, HIGH); // turn on the yellow LED
digitalWrite(redLedPin, LOW); // turn off the red LED
return;
}
void redLedActive(int switchState2) {
digitalWrite(yellowLedPin, LOW); // turn off the yellow LED
digitalWrite(redLedPin, HIGH); // turn on the red LED
digitalWrite(flashPin, LOW);
duration1 = 0;
duration2 = 0;
durationBetween = 0;
delay(2000);
return;
}
void flashLedActive(int flashMe) {
// code contained in a function to turn flashLedPin on and off using code below
digitalWrite(yellowLedPin, HIGH);
digitalWrite(flashPin, HIGH); // turn on the yellow LED
delay(1000);
digitalWrite(flashPin, LOW); // turn off the flash LED
delay(1000);
digitalWrite(flashPin, HIGH); // turn on the flash LED
delay(1000);
digitalWrite(flashPin, LOW); // turn off the flash LED
delay(1000);
duration1 = 0; // reset all the durations
duration2 = 0;
durationBetween = 0;
return;
}
void setup() {
pinMode(switchPin, INPUT); // set the switch pin to be an input
pinMode(yellowLedPin, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
pinMode(flashPin, OUTPUT); // set the flashing LED on pin 13 to be an output
}
void loop() {
duration1 = pulseIn(switchPin, HIGH); // set duration1 with a time marker when button is pressed
duration2 = pulseIn(switchPin, HIGH); // set duration2 with a time marker when the button is pressed again
durationBetween = (duration1 - duration2); // find the how much time between presses - is negative but doesn't seen to matter?
switchState = digitalRead(switchPin); // not in use at the moment
if (durationBetween > timerValue) {
redLedActive(switchState2);
}
else {
flashLedActive(flashMe);
}
delay(2000);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, LOW);
}