I want to shut the valve off if it stays on for ten seconds at the threshold.
The application is using a solenoid to inflate a balloon, but i do not want the balloon to explode obviously.
Below is the code i'm working on.
if (average >= THRESHOLD) //if vibrations are greater than
{
digitalWrite(Solenoid, HIGH); //turn on solenoid, Sets other side low
delay(1);
}
else{
digitalWrite(Solenoid, LOW); //turn off solenoid, sets other side HIGH
}
while(seconds>=10);
{
digitalWrite(Solenoid, LOW); //turn off solenoid, invert other side to HIGH
}
}
}
/*
Created By: Alberto Lopez
Date: 03/23/14
accel sketch
simple sketch to output average values of the x-axes
detects if acceleration goes above threshold
and turns on Solenoid if it goes above threshold
Stop the balloon from inflating after a couple of seconds
To Do's
1. Convert acceleration to Frequencies of an epileptic patient.
*/
unsigned long previousTime = 0;
byte seconds;
const int xPin = 4; // analog input pins
const int THRESHOLD = 500; //threshold frequency
#define Solenoid 7 //solenoid pin
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup()
{
Serial.begin(9600); // note the higher than usual serial speed
pinMode(Solenoid, OUTPUT); //solenoid set as an ouput
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop()
{
// I think using microseconds is even more accurate
if (millis() >= (previousTime))
{
previousTime = previousTime + 1000; // use 100000 for uS
seconds = seconds +1;
if (seconds == 1000) //goes up to 1000 seconds then returns to zero
{
seconds = 0;
}
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(xPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.print("X_avg = "); //print vibrations
Serial.println(average); //take average value of vibrations
delay(1); // delay in between reads for stability
Serial.print("Time = "); //print time in seconds
Serial.println(seconds,DEC); //time in seconds
delay(1);
if (average >= THRESHOLD) //if vibrations are greater than
{
digitalWrite(Solenoid, HIGH); //turn on solenoid, Sets other side low
delay(1);
}
else{
digitalWrite(Solenoid, LOW); //turn off solenoid, sets other side HIGH
}
while(seconds>=10);
{
digitalWrite(Solenoid, LOW); //turn off solenoid, invert other side to HIGH
}
}
}
The code suggests that you expect seconds to take a value between 0 and 999 but it is only a byte value so the maximum it can actually hold is 255. You should probably change that to an int.
All the calls to delay(1) are pointless.
The semicolon at the end of line 86 means that the loop will only contain an empty statement. Since nothing in the loop will cause the loop condition to change, if the condition is true at the start of the loop the loop will run forever, doing nothing. If you get rid of the spurious semicolon then it will run forever calling digitalWrite(). Neither of those options seems useful. I don't know what you intended to achieve there, but until you've worked it out I suggest you get rid of lines 86 .. 89.
It looks to me like you want to monitor some frequencies until they reach a threshold, then turn on something for ten seconds or the frequencies drop below the threshold. Sound right?
monitor frequencies
if flag false and threshold reached, turn on valve, get time, set flag
if flag true and below threshold, turn off valve, reset flag
if flag true and run time has passed, turn off valve, reset flag
rinse, lather, repeat
(If the frequencies of vibration remain for a duration of 10 seconds above the threshold(1000), turn the solenoid on, otherwise turn it off permanently )-this is what i meant, sorry for the confusion
The reason i want to turn it off permanently is because i do not want air to flow through the solenoid after i have inflated something to capacity.
The 1000 minutes is a value i chose to take time, i really only need 5 to ten seconds. I found a code that would take time from 0 to 60 seconds, but changed it to 1000.
The threshold was set to 1000 because i was trying to test the threshold for vibrations.