Thanks mowcius,
Your idea runs well. However, it only runs one time.
Thanks also to TeamMCS for your inputs and link.
My idea is to made a measurement in a period. Form example, every five minutes, to measure the number of knocks in 30 seconds; as well to know the measurement from other sensor, for example temperature, but in that case just only punctual measurement at the end of the 30 seconds.
Here is a pseudocode of what i want to do:
// knocks measurement
Declare the pins
led pin
knock pin
Declare the variables
knockcounter
Procedure: knocks measurement
Reset the knockcounter to 0
Start a period of time (30 seconds, for example)
if the pin read something higher than the therehold,
increase 1 the knockscounter
change the state of the led
at the end of the period of time, know the number of total knocks
prodecure: for example read the temperature with other sensor
void setup()
start the serial
void loop()
start the procedure countknocks
start the procedure read temperature
show the total number of knocks on serial
show the temperature on serial
delay for 5 minutes
and here is the code that i have right now. I only show the procedure related to knocks. The temperature or any other sensor is not a problem. However, it doesn´t work, because the serial even show knocks=0 and the led don´t change at all...
// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = 0; // the piezo is connected to analog pin 0
const int threshold = 1; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
unsigned long knockcount=0; // start the knock counter
unsigned long millis();
unsigned long readknocks(){
sensorReading = analogRead(knockSensor);
if (millis()<30000){
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
knockcount=knockcount+1;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
}
}
return knockcount;
}
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
readknocks(); //start the measuring time
Serial.print("Knocks: "); // prepare to print the results
Serial.println(knockcount,DEC); // print the number of knocks
//in the future here i will also read a temperature sensor and show it
knockcount=0; //reset the knocks counter
delay(3000); //in the future it will be 5 minutes or more...
}
Any idea about how could i improve the code?
On the other hand, do you think that it is better to use the procedure used in the example "blinkwithoutdelay" included in arduino IDE?
Thanks for your ideas!!