There a many different ways to accomplish this. The method shown below sets a "flag" whenever the pot value goes high. The flag is then reset when the pot goes low.
The flash lights() function is only called when the potValue goes above the threshold and the flag is set. Once the lights() function is called the flag is changed and needs to be reset before the function can be called again.
Here is a video of the circuit and the sketch is below...
VIDEO
SKETCH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Shows the use of a user-defined function call from loop(). | |
The sketch also uses a flag variable to keep track of the | |
current position state of the potentiometer. | |
The potentiometer is used to provide and analog signal to pin | |
A0. When the value of the analog read variable potVal goes | |
from a value that is below 400 to a value that is above | |
the threshold value of 600 the function lights() is called to | |
flash the green and red LEDs. The function is not called again | |
until the pot is again cycle below 400 and then above 600. This | |
check is made using the variable flag. | |
The initial state of the potentiometer and thus the flag value is | |
determine in the setup() by calling the user-defined function | |
potState() which returns the boolean value of the flag. */ | |
// LED pins | |
const int ledGreen = 2; | |
const int ledRed = 3; | |
// Potentiometer | |
const int potPin = A0; | |
int potVal = 0; | |
// Variable to hold state of potentiometer | |
boolean flag = 0; | |
void setup(){ | |
// Set led pins as outputs | |
pinMode(ledGreen, OUTPUT); | |
pinMode(ledRed, OUTPUT); | |
// Turn leds off | |
digitalWrite(ledGreen, LOW); | |
digitalWrite(ledRed, LOW); | |
// Initialize serial communication for debugging | |
Serial.begin(9600); | |
// Call user defined function potState() to | |
// read initial state of potentiometer and set flag | |
flag = potState(); | |
} | |
void loop(){ | |
// Read value of potentiometer | |
potVal = analogRead(potPin); | |
// Check if potentiometer has correct value and that | |
// the flag has changed state, if TRUE, call user- | |
// defined function lights(). | |
if (potVal > 600 && flag == 0){ | |
lights(); | |
flag = 1; | |
} | |
// Flag gets reset to FALSE (0) when the potentiometer | |
// is turned low. | |
// The 50 units between 500 and 550 provides a buffer | |
// that prevents unintended resets of flag. | |
if (potVal < 500){ | |
flag = 0; | |
} | |
} | |
// user-defined function to check the state of the potentiometer | |
boolean potState(){ | |
boolean check = 0; | |
potVal = analogRead(potPin); | |
if (potVal > 512){ | |
check = 1; | |
} | |
else{ | |
check = 0; | |
} | |
return check; | |
} | |
// user-defined function to flash the LED's | |
void lights(){ | |
for (int i = 0; i < 10; i++){ | |
digitalWrite(ledGreen, HIGH); | |
delay(50); | |
digitalWrite(ledGreen, LOW); | |
delay(50); | |
digitalWrite(ledRed, HIGH); | |
delay(50); | |
digitalWrite(ledRed, LOW); | |
delay(50); | |
} | |
} |
No comments:
Post a Comment