piezzo-test.ino 870 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#define  YELLOW_LED_PIN   8
#define  RED_LED_PIN      13
 
#define  RED_THRESHOLD         50

#define  YELLOW_MIN_THRESHOLD  6
#define  YELLOW_MAX_THRESHOLD  10


int pv;  // piezzo value 

unsigned long previousMillis = 0;
int led_state=LOW;

void setup()
{
    Serial.begin(9600);
    pinMode(RED_LED_PIN, OUTPUT);
    pinMode(YELLOW_LED_PIN, OUTPUT);
}

void loop() 
{
    pv = analogRead(0);    

    if (pv >= YELLOW_MIN_THRESHOLD && pv <= YELLOW_MAX_THRESHOLD)
    {
        digitalWrite(YELLOW_LED_PIN, HIGH);
        Serial.println(pv);
    }    
    else
    {
        digitalWrite(YELLOW_LED_PIN, LOW);
    }

    unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis >= 500)
    {
        previousMillis = currentMillis;
        led_state = (led_state == LOW) ? HIGH : LOW;
        digitalWrite(RED_LED_PIN, led_state);
    }

}